I am facing a strange behavior when applying using the tz_localize function from datetime.
The peace of code is the following
infer_dst = np.array([False]*df.shape[0])
try:
df.index=df.index.tz_localize(self.tzs,ambiguous=infer_dst)
except pytz.NonExistentTimeError as e:
self.logger.warning(f"DST between {t_ini} and {t_end} in host {ctrl_inf['host']}")
self.logger.warning(str(e))
Here where I leave we should be entering the daylight saving period but this year we are not going to have it.
The weird thing is that when I run this code in a server (Windows Server 2016) I receive a lot of NonExistentTimeError messages. But if I run the same code on my laptop for the same period of time it works fine.
I thought that maybe the cause could be that the auto daylight saving time is adjusted to automatic in the server but it is also set to ON in my laptop (this is something set by the IT company and I was not able to turn it off) Just to contextualize the situation a little bit more. The dataframe df holds historical data from a PIMS system.
Does anyone have ever seen something like that? Thanks
UPDATE
Definetelly the problem is something in the Window Server. The problem happens with data in the interval from 0h to 1h (24h format) of Nov 7th 2021. As I was not able to fix the server I come up with a not very beautiful solution.
infer_dst = np.array([False]*df1.shape[0])
try:
df1.index=df1.index.tz_localize(self.tzs,ambiguous=infer_dst)
except pytz.NonExistentTimeError as e:
self.logger.warning(f'DST between {t_ini} and {t_end} in host {host}')
self.logger.warning(str(e))
#try to remove the period with problem
remove_index=[]
for k in range(len(df1)-1):
try:
df1.index[k].tz_localize(self.tzs)
except:
remove_index.append(k)
df1.drop(df1.index[remove_index],inplace=True)
infer_dst = np.array([False]*df1.shape[0])
df1.index=df1.index.tz_localize(self.tzs,ambiguous=infer_dst)