Sociopath
Sociopath

Reputation: 13426

Not able to compare pandas datetime column to datetime fetch from DB

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

Upvotes: 2

Views: 812

Answers (1)

Nic Moetsch
Nic Moetsch

Reputation: 926

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')

Upvotes: 2

Related Questions