Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering negative timedeltas

Consider a series holding timedelta64[ns] that measures at the time difference between two events A and B:

> time_deltas

499900   -1 days +23:45:13
499916   -1 days +23:50:57
499917            00:03:27
499919            00:17:45
499920            00:16:56
499921   -1 days +23:59:26
499922            00:16:34
499923            00:15:46
499928            00:12:56
499929            00:05:54
                ...       
499970            00:00:48
499971   -1 days +23:58:32

dtype: timedelta64[ns]

How can I identify negative deltas? (e.g. A happening before B).

This does not work:

> time_deltas[time_deltas<0]
TypeError: invalid type comparison

Also consider the following:

# Negative  time delta example:
> time_deltas.iloc[-1]
Timedelta('-1 days +23:58:32')

# Values seem to have integer representation in ns
> time_deltas.iloc[-1].value
-88000000000

# Positive time delta example:
> time_deltas.iloc[-2]
Timedelta('0 days 00:00:48')

# Again, values seem to have integer representation in ns
> time_deltas.iloc[-1].value
48000000000

But then:

# Trying to use the internal representation fails
> time_deltas.apply(lambda x: x.value>0)
AttributeError: 'numpy.timedelta64' object has no attribute 'value'

# Same with
> time_deltas.apply(lambda x: x['value']>0)
IndexError: invalid index to scalar variable.
like image 336
Amelio Vazquez-Reina Avatar asked Jan 14 '16 15:01

Amelio Vazquez-Reina


People also ask

Can Timedelta be negative?

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.

How do I remove days from Timedelta?

You can subtract a day from a python date using the timedelta object. You need to create a timedelta object with the amount of time you want to subtract. Then subtract it from the date.

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.

How do you convert Timedelta to seconds?

To get the Total seconds in the duration from the Timedelta object, use the timedelta. total_seconds() method.


1 Answers

Compare it against pd.Timedelta(0):

In [60]: time_deltas = pd.to_timedelta(np.random.randint(-10**6, 10**6, size=10))

In [61]: time_deltas
Out[61]: 
TimedeltaIndex([         '00:00:00.000809', '-1 days +23:59:59.999034',
                '-1 days +23:59:59.999456', '-1 days +23:59:59.999156',
                '-1 days +23:59:59.999053', '-1 days +23:59:59.999723',
                '-1 days +23:59:59.999523',          '00:00:00.000349',
                         '00:00:00.000051',          '00:00:00.000774'],
               dtype='timedelta64[ns]', freq=None)

In [62]: time_deltas < pd.Timedelta(0)
Out[62]: array([False,  True,  True,  True,  True,  True,  True, False, False, False], dtype=bool)
like image 197
unutbu Avatar answered Oct 20 '22 11:10

unutbu