QQ1821
QQ1821

Reputation: 143

TypeError: float() argument must be a string or a number, not 'datetime.time'

I have a DataFrame that looks like this:

df_11=

    dataReceived time_diff3 time
0           1   76.356133   15:00:56.052554
1           1   68.195077   15:02:18.764644
2           1   141.308039  15:03:38.039307
3           1   48.674365   15:06:22.955319
4           1   113.261287  15:07:57.382506
... ... ... ... ... ...
6050        1   18.000000   02:43:10
6051        1   18.000000   02:43:28
6052        1   19.000000   02:43:46
6053        1   18.000000   02:44:05
6054        1   0.000000    02:44:23

I want to plot :

ax1 = df_11.plot(kind='scatter', x='time', y='time_diff3', color='r')    

print(ax1)

And I got the following error:

TypeError: float() argument must be a string or a number, not 'datetime.time'

I dont understand why I cant plot this graph, since column time is datetime.time. How am I supposed to convert datetime.time to datetime64?

Upvotes: 1

Views: 2605

Answers (1)

user7864386
user7864386

Reputation:

You could coerce "time" column to datetime using to_datetime and extract the time component using dt.strftime. You may want to rotate the labels:

df_11['time'] = pd.to_datetime(df_11['time']).dt.strftime('%H:%M%S')
ax1 = df_11.plot(kind='scatter', x='time', y='time_diff3', color='r')
ax1.xaxis.set_ticks(df['time'])
ax1.set_xticklabels(df['time'], rotation=45);

enter image description here

Upvotes: 1

Related Questions