TypeError: strptime() argument 1 must be string, not float
To plot the dataframe in question using matplotlib, you may convert the column in question to datetime using pandas.to_datetime
first.
u = u"""i date value_1 event other_event
37 07/02/2015 265.09 07/02/2015 NaN
38 08/02/2015 278.59 08/02/2015 NaN
156 06/06/2015 146.07 06/06/2015 NaN
180 30/06/2015 133.56 30/06/2015 NaN
243 01/09/2015 280.27 01/09/2015 01/09/2015"""
import io
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv(io.StringIO(u), delim_whitespace=True)
df["event"] = pd.to_datetime(df["event"], format="%d/%m/%Y")
plt.plot(df["event"], df["value_1"])
plt.gcf().autofmt_xdate()
plt.show()
ailsa_naismith
Physical volcanologist in training at the University of Bristol. I study the activity of Volcan de Fuego in Guatemala and the relevant experiences of surrounding communities. My main research interests are triggers of paroxysmal events at Fuego, ground-based and satellite infrared remote sensing, and the link between lava effusion and paroxysm at Fuego. Pretty much the Polonius of coding.
Updated on June 04, 2022Comments
-
ailsa_naismith almost 2 years
Good morning!
I have a series of events with associated dates. The event dates are stored as a series of string values in a column within a dataframe that I have loaded into Python. The dataframe contains other columns with values. I would like to convert the values in column "event" into datetime objects, store them in a list, and then plot that list with matplotlib to create a timeseries.
My dataframe looks like this:
date value_1 event other_event 37 07/02/2015 265.09 07/02/2015 NaN 38 08/02/2015 278.59 08/02/2015 NaN 156 06/06/2015 146.07 06/06/2015 NaN 180 30/06/2015 133.56 30/06/2015 NaN 243 01/09/2015 280.27 01/09/2015 01/09/2015
Python tells me that the column data is
Name: event, dtype: object
, which I assume means it contains string values. I also have the linedf.event.apply(str)
in my code, which I think will convert the values in my event column into string values.Then I have this code:
FMT = '%d/%m/%Y' event_list = [] for i in range(0, len(event)): event_list.append(datetime.datetime.strptime(event[i], FMT))
However, this line returns an error:
Traceback (most recent call last): File "<ipython-input-39-e778a465e858>", line 2, in <module> event_list.append(datetime.datetime.strptime(event[i], FMT)) TypeError: strptime() argument 1 must be string, not float
Any advice as to where I'm going wrong will be gratefully received.