Filtering negative timedeltas

2024/9/30 21:34:19

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

> time_deltas499900   -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:32dtype: 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.
Answer

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)
https://en.xdnf.cn/q/71040.html

Related Q&A

What is the Matlab equivalent of the yield keyword in Python?

I need to generate multiple results but one at a time, as opposed to everything at once in an array.How do I do that in Matlab with a generator like syntax as in Python?

Convert from CMYK to RGB

Im having trouble converting a single page pdf (CMYK) to a jpg (RGB). When I use the code below, the colors in the jpg image are garish. Ive tried reading through the Wand docs, but havent found anythi…

TopologicalError: The operation GEOSIntersection_r could not be performed

Hi Guys, I am trying to map the district shapefile into assembly constituencies. I have shape files for [Both].Basically I have to map all the variables given at district level in census data to assemb…

Plot a function during debugging in Python

I used to work in Matlab and it is really convenient (when working with big arrays/matrices and nested functions) to visualize intermediate results during debugging using plot function. In Python I can…

getting last n items from queue

everything I see is about lists but this is about events = queue.queue() which is a queue with objects that I want to extract, but how would I go about getting the last N elements from that queue?

Paramiko ValueError p must be exactly 1024, 2048, or 3072 bits long

I am trying to connect SFTP using Python script. Im unable to connect due to "p error".import paramiko client = paramiko.SSHClient() client.load_system_host_keys() client.connect(####.com, us…

WindowsError: [Error 5] Access is denied using urllib2

Im getting a "WindowsError: [Error 5] Access is denied" message when reading a website with urllib2. from urllib2 import urlopen, Request from bs4 import BeautifulSouphdr = {User-Agent: Mozil…

Send key combination with python

I want to be able to send the key combination SHIFT + CTRL + . (dot) using the following code:import win32com.client as comclt wsh= comclt.Dispatch("WScript.Shell") wsh.SendKeys() So far I wa…

Taking data from drop-down menu using flask

Im completely new to flask, and really am completely lost with how to approach this. Ive looked into other SO questions but I cant seem to get this working regardless. I have a form as such: <form…

How to get list_blobs to behave like gsutil

I would like to only get the first level of a fake folder structure on GCS.If I run e.g.:gsutil ls gs://gcp-public-data-sentinel-2/tiles/I get a list like this:gs://gcp-public-data-sentinel-2/tiles/01/…