Pandas and DateTime TypeError: cannot compare a TimedeltaIndex with type float

Viewed 11338

I have a pandas DataFrame Series time differences that looks like::

 print(delta_t)

 1       0 days 00:00:59
 3       0 days 00:04:22
 6       0 days 00:00:56
 8       0 days 00:01:21
 19      0 days 00:01:09
 22      0 days 00:00:36
 ...

(the full DataFrame had a bunch of NaNs which I dropped).

I'd like to know which delta_t's are less than 1 day, 1 hour, 1 minute, so I tried:

delta_t_lt1day = delta_t[np.where(delta_t < 30.)]

but then got a:

TypeError: cannot compare a TimedeltaIndex with type float

Little help?!?!

2 Answers

Assuming your Series is in timedelta format, you can skip the np.where, and index using something like this, where you compare your actual values to other timedeltas, using the appropriate units:

delta_t_lt1day = delta_t[delta_t < pd.Timedelta(1,'D')]

delta_t_lt1hour = delta_t[delta_t < pd.Timedelta(1,'h')]

delta_t_lt1minute = delta_t[delta_t < pd.Timedelta(1,'m')]

You'll get the following series:

>>> delta_t_lt1day
0
1    00:00:59
3    00:04:22
6    00:00:56
8    00:01:21
19   00:01:09
22   00:00:36
Name: 1, dtype: timedelta64[ns]
>>> delta_t_lt1hour
0
1    00:00:59
3    00:04:22
6    00:00:56
8    00:01:21
19   00:01:09
22   00:00:36
Name: 1, dtype: timedelta64[ns]
>>> delta_t_lt1minute
0
1    00:00:59
6    00:00:56
22   00:00:36
Name: 1, dtype: timedelta64[ns]

You could use the TimeDelta class:

import pandas as pd

deltas = pd.to_timedelta(['0 days 00:00:59',
                          '0 days 00:04:22',
                          '0 days 00:00:56',
                          '0 days 00:01:21',
                          '0 days 00:01:09',
                          '0 days 00:31:09',
                          '0 days 00:00:36'])

for e in deltas[deltas < pd.Timedelta(value=30, unit='m')]:
    print(e)

Output

0 days 00:00:59
0 days 00:04:22
0 days 00:00:56
0 days 00:01:21
0 days 00:01:09
0 days 00:00:36

Note that this filter outs '0 days 00:31:09' as expected. The expression pd.Timedelta(value=30, unit='m') creates a time delta of 30 minutes.

Related