Reputation:
How I can get all the rows added in the last minute in pandas. 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]
but i should only get the below :-
Upvotes: 2
Views: 1738
Reputation: 601
created_time = datetime.datetime.utcnow() - datetime.timedelta(minutes=1)
data = data[(data['Date'] > created_time) & (data['Date'] <datetime.datetime.utcnow())]
Upvotes: 1
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
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