Am going bonkers over comparing two dates - one as pd_todatetime object and another as python datetime object. Both have same date and timezone, still on comparison it shows inequality. Below are two objects:
Timestamp('2022-02-17 09:15:00 0530', tz='Asia/Kolkata')
datetime.datetime(2022, 2, 17, 9, 15, tzinfo=<DstTzInfo 'Asia/Kolkata' LMT 5:53:00 STD>)
First one is pd.to_datetime index, while the latter is python datetime object.
Following is raw data:
from pytz import timezone
IST = timezone('Asia/Kolkata')
data = {'date': datetime.datetime(2022, 2, 17, 9, 15, tzinfo=tzoffset(None, 19800)), 'open': 150, 'high': 203.75, 'low': 150, 'close': 191.45, 'volume': 185050, 'oi': 1735100}
df = pd.DataFrame(dat)
if df.date.dt.tz == None:
df['date'] = df.date.dt.tz_localize(IST)
else:
df['date'] = df.date.dt.tz_convert(IST)
df.set_index("date", inplace=True)
start = datetime(2022,2,17,9,15,0, tzinfo=IST)
if df.index.min() == start:
do_something()
This is the place where the if statement is evaluated as False
CodePudding user response:
Can't you use pandas.Timestamp
, like explained in:
Timestamp is the pandas equivalent of python’s Datetime and is interchangeable with it in most cases. It’s the type used for the entries that make up a DatetimeIndex, and other timeseries oriented data structures in pandas.
https://pandas.pydata.org/docs/reference/api/pandas.Timestamp.html
CodePudding user response:
I've never used pytz before but I think I've replicated your issue:
import pytz
a = pd.Timestamp('2022-02-17 09:15:00', tz='Asia/Kolkata')
b = datetime.datetime(2022, 2, 17, 9, 15, tzinfo=pytz.timezone('Asia/Kolkata'))
assert(a == b) # False
Note sure why these don't equal. It might be related to this note in the documentation.
Can you use the new built in zoneinfo module (introduced in version Python 3.9)?
from zoneinfo import ZoneInfo
c = datetime.datetime(2022, 2, 17, 9, 15, tzinfo=ZoneInfo('Asia/Kolkata'))
assert(a == c) # True