3

I have a python program in which i read a csv file onto a Pandas dataframe. Then I want to plot my sensor value with datetime in a clean format. My code gives error ValueError: time data ' 2017/02/17' does not match format '%Y/%m/%d %H:%M:%S.%f. My Code and some rows from dataframe is below:

Code:

import pandas as pd
    from datetime import datetime
    import csv
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    headers = ['Sensor Value','Date','Time']
    df = pd.read_csv('C:/Users\Lala Rushan\Downloads\DataLog.CSV', parse_dates= {'Datetime': [1,2]},names=headers)

    print (df)

    df['Datetime'] = df['Datetime'].map(lambda x: datetime.strptime(str(x), "%y/%m/%d , %H:%M:%S.%f"))
    #datetime.strptime(df['Datetime'],"%Y/%m/%d %H:%M:%S.%f")
    x = df['Datetime']
    y = df['Sensor Value']

    # plot
    plt.plot(x,y)
    # beautify the x-labels
    plt.gcf().autofmt_xdate()

    plt.show()

DataFrame:

                      Datetime  Sensor Value
0     2017/02/17  19:06:17.188             2
1     2017/02/17  19:06:22.360            72
2     2017/02/17  19:06:27.348            72
3     2017/02/17  19:06:32.482            72
4     2017/02/17  19:06:37.515            74
5     2017/02/17  19:06:42.580            70
6     2017/02/17  19:06:47.660            72

How can i solve this error? I'm newvie to python so pardon any basic mistakes.

3
  • It's telling you that your date doesn't match the format string. The question is why the error says the date string is "2017/02/17" but your posted dataframe looks like it should be "2017/02/17 19:06:17.188" (also note no comma) or similar. Commented Feb 21, 2017 at 23:37
  • yes thats what i am thinking and stuck at. can you tinker the code to work? I am new to python. Commented Feb 21, 2017 at 23:41
  • Just use pandas's datetime parser: df['Datetime'] = pd.to_datetime(df['Datetime']) insteading of trying to use map to parse each row value. This should parse the entire field into datetime if the format is consistent all throughout. Commented Feb 21, 2017 at 23:41

1 Answer 1

3

You don't need to split date and time. Below code works well for me.

import pandas as pd
from datetime import datetime
import csv
import matplotlib.pyplot as plt

df = pd.read_csv('test.txt')
print (df)

df['Datetime'] = df['Datetime'].map(lambda x: datetime.strptime(str(x), "%Y/%m/%d %H:%M:%S.%f"))

x = df['Datetime']
y = df['Sensor Value']

# plot
plt.plot(x,y)

# beautify the x-labels
plt.gcf().autofmt_xdate()
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

this code gives the same error for me. maybe theres the difference in the read_csv part where i am parsing the date and time into one column. do you think there might be a problem there?
How can we modify the code so that the part after seconds are not shown (all those zeroes). Also, how can we make it to show until minutes let's say?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.