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()?
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.
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.
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')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With