python timestamp argument error

Asked

Viewed 39 times

-1

I’m converting a set of dates and trying to plot a chart with the converted values but I’m having the error following, someone knows what can be?

dates = data['data']
date_format = [pd.to_datetime(d) for d in dates]

rolling_average_days = 7
data['nuovi_positivi_moving'] = data['nuovi_positivi'].rolling(window=rolling_average_days).mean()
variable = 'nuovi_positivi_moving'
fig, ax = plt.subplots(figsize=(12, 5))
ax.grid()
ax.scatter(date_format,data[variable])
ax.set(xlabel="Date",ylabel=variable,title='NOVOS CASOS POSITIVOS')
date_form = DateFormatter("%d-%m")
ax.xaxis.set_major_formatter(date_form)
ax.xaxis.set_major_locator(mdates.DayLocator(interval = 3))
fig.savefig(variable + '.png')
plt.show()

and the error that returns is as follows

Typeerror: float() argument must be a string or a number, not 'Timestamp'

1 answer

0

The problem is in the transformation of the pd.to_datetime(d) creates a Timestamp object , however in your case it would be more valid a Datetime object so for this just you: import the datetime

from datetime import datetime

and change this transformation line:

date_format = [datetime.strptime(d, '%d/%m/%y')for d in dates]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.