Reputation: 343
I am new to Python and programming and would appreciate help on a small problem! I have a CSV file with three columns of data that I would like to plot: the first, with dates, I would like to plot as my x-axis; the second and third columns hold data corresponding to those dates.
I would like to plot these data. I have written a script that should read the CSV file, convert the dates column values from string data to timestamps, and then plot values from the other columns against timestamps to make a timeseries.
When I try to run the script, it returns the error message:
" ValueError: time data 'Date' does not match format '%d/%m/%y' ".
However, when opening my CSV file, all date values are stored in the format e.g. 31/01/16, which seems to match the format I have specified in Python.
I share my script below for any possible help. Thanks in advance!
import numpy as np
import pandas as pd
import datetime
import matplotlib.pyplot as plt
from matplotlib.pyplot import style
style.use('ggplot')
df = pd.read_csv('Fuego_mirova_vs_rsam.csv', delimiter=',')
np.shape(df)
dates=['Date']
vrp=['VRP (Watt)']
rsam=['rsam']
for indexr, row in df.iterrows():
dates.append(row.values[0])
vrp.append(row.values[1])
rsam.append(row.values[2])
FMT = '%d/%m/%y'
dtlist = []
for i in range(0, len(dates)):
dtlist.append(datetime.datetime.strptime(dates[i], FMT))
fig, ax = plt.subplots(figsize = (10,10))
ax.plot(dtlist, vrp, label='Radiative power [W]')
ax.plot(dtlist, rsam, label='Average daily seismic amplitude')
plt.legend
plt.show()
Upvotes: 2
Views: 2449
Reputation: 294218
You have the string value of 'Date'
in your column that is throwing the error.
Consider the dataframe df
import pandas as pd
from io import StringIO
txt = """Date,value
31/01/16,1
31/01/16,1
Date,1
31/01/16,1
31/01/16,1"""
df = pd.read_csv(StringIO(txt))
df
Date value
0 31/01/16 1
1 31/01/16 1
2 Date 1
3 31/01/16 1
4 31/01/16 1
If I attempt the convert this to Timestamp
df.Data = pd.to_datetime(df.Date, format='%d/%m/%y')
ValueError: time data 'Date' does not match format '%d/%m/%y' (match)
You can either fix your data or you can convert that to NaT
(Not a Time[stamp]) with the errors
parameter
df.Date = pd.to_datetime(df.Date, format='%d/%m/%y', errors='coerce')
df
Date value
0 2016-01-31 1
1 2016-01-31 1
2 NaT 1
3 2016-01-31 1
4 2016-01-31 1
Upvotes: 1
Reputation: 13078
you can convert the entire date
column into datetime
objects with
df[0] = pd.to_datetime(df[0])
that way you don't need the call to strptime
at all, pandas
should be able to figure out what the date format is
Upvotes: 1