Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use timedelta with pandas df.query()?

I would like to be able to pass in text from a user or file to filter pandas, which seems like query is the best way to handle it. However, I have a datetime index and can't seem to figure out a way to use timedeltas. I know I can filter dates with > or < like

query_string = 'index < datetime.datetime(2020, 2, 20, 11, 8, 19, 615268)'
df.query(queryString)

and

date = datetime.datetime.now()
query_string = 'index < @date'
df.query(queryString)

What I want to do is get a relative date range like getting the last 10 seconds of entries

date = datetime.now()
query_string = 'index > @date - datetime.timedelta(seconds=10)'
df.query(query_string)

This fails, and I can't seem to find a way to do something like filter anything relative to a timestamp. Is there any other way to format it so I can add/subtract a time from a date using df.query()?

like image 811
bltpyro Avatar asked Feb 20 '20 18:02

bltpyro


People also ask

What is Timedelta in pandas?

Timedelta. Represents a duration, the difference between two dates or times. Timedelta is the pandas equivalent of python's datetime. timedelta and is interchangeable with it in most cases.

Is Timedelta a data type?

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.


1 Answers

Query does not support timedelta (checked in version Pandas 1.0.5). You can work around it using:

df[df['index'] >= datetime.now() - datetime.timedelta(10, 'S')]
like image 118
e1i45 Avatar answered Oct 18 '22 22:10

e1i45