Recently I have been working on a time series data set and have written a script to automate some plotting. Using the pd.to_datetime function (provided with a specific format), I assumed would automatically convert every time entry to the appropriate format.
The raw data follows this format:
%d/%m/%YYYY HH:MM (HH:MM is irrelevant in this case so don't worry about it as we are only interested in the daily average)
However, it seems Python intermittently changes the 'raw timestamps' and changes the format to: %d-%m-%YYYY
Why is this the case and how can I make sure Python doesn't do this?
I have received the below error and struggle to work out why this is the case. I have looked at the following SO but I don't have the same issue. time data does not match format
The data itself is provided in the following CSV and is all in the %d/%m/%Y format.
My code for my function is attached in case there are any errors with how I've converted the timestamps.
def plotFunction(dataframe):
for i in wellNames:
my_list = dataframe["Date"].values
DatesRev = []
for j in my_list:
a=j[0:10]
DatesRev.append(a)
#We now need to re-add the dates to our data frame
df2 = pd.DataFrame(data= DatesRev)
df2.columns = ["DatesRev"]
dataframe["DatesRev"] = df2["DatesRev"]
# print (dataframe)
# #df2= pd.DataFrame(DatesRev)
# #df2.columns = ['DatesRev']
# #dataframe['DatesRev'] = df2['DatesRev']
wellID = dataframe[dataframe['Well']==i]
wellID['DatesRev'] = pd.to_datetime(wellID['DatesRev'], format='%d/%m/%Y')
print (i)
# ax = wellID.set_index('DatesRev').plot()
# xfmt = mdates.DateFormatter('%d-%m-%Y')
# ax.xaxis.set_major_formatter(xfmt)
# plt.xticks(rotation=90)
# ax.legend(bbox_to_anchor=(1.04,1), loc="upper left")
# plt.title(i)
# plt.show()
# plt.savefig(i + ".jpg", bbox_inches='tight')