W01f
W01f

Reputation: 33

Pandas does not show the desired result on the y-axis with timedelta

I would like to plot a timedelta with pandas 0.22.0 . Unfortunately, the y-axis only goes up to just over 3. Why is that?

import pandas as pd
df = pd.DataFrame({'date': ['2017-07-01', '2017-07-02', '2017-07-03', '2017-07-04', '2017-07-05'], 'minutes': [195, 69, 76, 25, 540]})
df.index = pd.to_datetime(df['date'])
series = pd.Series(data=pd.to_timedelta(df['minutes'], 'm'))

With series.describe it shows me everything correctly:

series.describe()

Out[6]: 
count                         5
mean            0 days 03:01:00
std      0 days 03:30:20.768597
min             0 days 00:25:00
25%             0 days 01:09:00
50%             0 days 01:16:00
75%             0 days 03:15:00
max             0 days 09:00:00
Name: minutes, dtype: object

Picture of the plot: enter image description here

Upvotes: 1

Views: 142

Answers (1)

DJK
DJK

Reputation: 9274

The timedeltas are being set to timedelta64[ns] by default so your seeing data in nanoseconds. However, when you run describe, your stats are appearing in days. If you convert to seconds it becomes much clearer

series.dt.seconds.describe()

count        5.000000
mean     10860.000000
std      12620.768598
min       1500.000000
25%       4140.000000
50%       4560.000000
75%      11700.000000
max      32400.000000

An we can see the max at 32400, which appears to be correct on your plot and data. However, you're also plotting in nanoseconds, which you will see if you hover the mouse over the values and check your y's. You may want to construct your plot like so

series.dt.seconds.plot()

Upvotes: 1

Related Questions