Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?!?!

like image 335
npross Avatar asked Oct 23 '18 01:10

npross


People also ask

What is a Timedelta?

Timedeltas are differences in times, expressed in difference units, e.g. days, hours, minutes, seconds. They can be both positive and negative. Timedelta is a subclass of datetime.

What does PD Timedelta do?

Timedelta. Represents a duration, the difference between two dates or times. Timedelta is the pandas equivalent of python's datetime.


1 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]
like image 124
sacuL Avatar answered Sep 20 '22 22:09

sacuL