Convert Pandas TimeDelta to Integer

Suppose I have a Pandas series that contains TimeDelta data. In fact, it was generated taking into account the difference in DateTimeIndex with a shifted version of itself, which gives a delta between successive timestamps.

It looks something like

timestamp
2015-02-01 00:00:04   00:00:04
2015-02-01 00:00:08   00:00:04
2015-02-01 00:00:12   00:00:04
....
Name: timestamp, dtype: timedelta64[ns]

The values ​​are obviously numpy.timedelta64, but I need to get them in seconds. The same questions were asked about this, but the answers that I have not yet seen that relate to Pandas 0.16.1.

I tried:

ts.apply(lambda x: x.seconds)

What gives an error

AttributeError: object "numpy.timedelta64" does not have a second attribute

Then tried

numpy.int64(ts)

But that gives me an array. Now I know that I can convert this back to a series, but is there no other way to do this in one Pandas call or match function?

+4
1

:

In [24]:

t="""index,timestamp
2015-02-01 00:00:04,00:00:04
2015-02-01 00:00:08,00:00:04
2015-02-01 00:00:12,00:00:04"""
s = pd.read_csv(io.StringIO(t),parse_dates=[0,1], squeeze=True, index_col=[0])
In [26]:

s.dt.second
Out[26]:
index
2015-02-01 00:00:04    4
2015-02-01 00:00:08    4
2015-02-01 00:00:12    4
dtype: int64

dtyetime dtype dt, seconds.

+5

All Articles