user10177566
user10177566

Reputation:

getting all the rows for the last minute in pandas

How I can get all the rows added in the last minute in pandas. enter image description here IF there is any value of date is within the last minute i should get it else not.

I am using the below code and its not giving me any error but its returning the expected value.

import datetime
created_time = datetime.datetime.now() - datetime.timedelta(minutes=1)
old_objects = data[data['Date'] > created_time]

I am getting the below values enter image description here

but i should only get the below :- enter image description here

Upvotes: 2

Views: 1738

Answers (3)

ak3191
ak3191

Reputation: 601

created_time = datetime.datetime.utcnow() - datetime.timedelta(minutes=1)

data = data[(data['Date'] > created_time) & (data['Date'] <datetime.datetime.utcnow())]

Upvotes: 1

jpp
jpp

Reputation: 164623

You should use consistent timezones. And you should use Pandas datetime objects with Pandas data. You can ensure consistency by using pd.to_datetime with tz_convert and pd.Timestamp.utcnow:-

So try this:

created_time = pd.Timestamp.utcnow() - pd.DateOffset(minutes=1)
data['Date'] = pd.to_datetime(data['Date']).tz_convert('UTC')

Upvotes: 0

andre.brito
andre.brito

Reputation: 86

Try to limit the range of dates using:

old_objects = data[(data['Date'] > created_time) & (data['Date'] < datetime.datetime.now())]

I remade your tests and it worked correctly here.

Upvotes: 1

Related Questions