Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib, pandas, how to generate a histogram of timedeltas?

I have a pandas.DataFrame df with that contains the following series:

Time
2182447     0 days 05:44:00
2182447     0 days 05:49:00
3129563     0 days 22:09:00
13341029    0 days 16:49:00
13341029    0 days 16:58:00
25622668    0 days 08:24:00
25622668    0 days 08:28:00
30077018   24 days 15:01:00
30077018   24 days 15:09:00
20131954    0 days 06:18:00

I would like to plot a histogram of the timedeltas. However:

hist(df)
df.Time.hist()
# both functions give the same error
>>> TypeError: Cannot cast ufunc less input from dtype('float64') to dtype('<m8[ns]') with casting rule 'same_kind'
like image 593
Soren Avatar asked Dec 08 '25 21:12

Soren


1 Answers

The following works:

hist(df.Time.astype('timedelta64[h]'))

You can use different units in the astype argument. Here I use ´h´ hours.

More detailed description can be found here.

like image 130
Soren Avatar answered Dec 12 '25 08:12

Soren



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!