2

I am trying to compare pandas datetime to datetime fetch from DB

pandas datetime column

           review_updatedAt
0 2021-01-01 00:31:14+00:00
1 2021-01-01 01:55:31+00:00
2 2021-01-01 02:43:28+00:00
3 2021-01-01 07:23:11+00:00
4 2021-01-01 05:38:10+00:00

And there's datetime value fetch from DB

result = '2021-01-01 04:51:53'

I want to filter the Dataframe values greater than result

My Efforts:

df['review_updatedAt'] = pd.to_datetime(df['review_updatedAt'], format='%Y-%m-%d %H:%M:%S', exact=False)

df = df[df['review_updatedAt'] > result]

I'm getting an Error

TypeError: Invalid comparison between dtype=datetime64[ns, UTC] and datetime

I also referred the existing questions Comparison between datetime and datetime64[ns] in pandas

Sociopath
  • 13,068
  • 19
  • 47
  • 75
  • the problem is you're comparing aware datetime (UTC; indicated by '+00:00') to naive datetime ('2021-01-01 04:51:53'). You can simply make `result` aware too, `result = pd.Timestamp('2021-01-01 04:51:53Z')`. – FObersteiner Apr 19 '21 at 11:58

1 Answers1

2

Your code works perfectly fine for me, but simply copying your code result is a string not a datetime for me. Since your TypeError implies it is datetime for you, maybe this cleanup is sufficient:

result = pd.Timestamp(result,tz='UTC')
Nic Moetsch
  • 846
  • 6
  • 9