Skip to content

Can't plot pd.Timedelta (or numpy.timedelta64) versus time #9154

Closed
@s-celles

Description

@s-celles

Hello,

I did

import numpy as np
import pandas as pd
from matplotlib.pyplot as plt

idx = pd.date_range('20140101', '20140201')
df = pd.DataFrame(index=idx)
df['col0'] = np.random.randn(len(idx))
s_idx = pd.Series(idx, index=idx) # need to do this because we can't shift index
diff_idx = (s_idx-s_idx.shift(1)).fillna(pd.Timedelta(0))
df['diff_dt'] = diff_idx
df['diff_dt'].plot()

but it raises Empty 'Series': no numeric data to plot

In [78]: df.dtypes
Out[78]:
col0               float64
diff_dt    timedelta64[ns]
dtype: object


In [79]: type(df['diff_dt'][1])
Out[79]: pandas.tslib.Timedelta

I don't understand if data inside diff_dt columns are numpy.timedelta64 or pd.Timedelta

df['diff_dt'].map(lambda x: x.value)

raises AttributeError: 'numpy.timedelta64' object has no attribute 'value'

but it seems that I can get valuefor a given data (let's say row 10)

In [97]: df['diff_dt'][10].value
Out[97]: 86400000000000

I don't understand why...

But I also don't understand how I could plot diff_dt column without doing an uggly:

df['diff_dt'].map(lambda x: x/np.timedelta64(1, 'ns')).plot()

But I don't know how I could automatically get this np.timedelta64(1, 'ns')

Maybe Pandas could plot Timedelta (or np.timedelta64) out of the box ?
because that's interesting to know for example if sampling period is constant.

Kind regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions