I am creating a date_range
:
dates = pd.date_range(start="2022-01-01", end='2022-06-25', freq="30min")
I want to set a Time Zone following the instructions of this tutorial :
dates.tz_localize('Europe/Warsaw')
---------------------------------------------------------------------------
NonExistentTimeError Traceback (most recent call last)
/var/folders/24/tg28vxls25l9mjvqrnh0plc80000gn/T/ipykernel_39654/1790666542.py in <module>
----> 1 dates.tz_localize('Europe/Warsaw')
~/miniforge3/lib/python3.9/site-packages/pandas/core/indexes/datetimes.py in tz_localize(self, tz, ambiguous, nonexistent)
277 @doc(DatetimeArray.tz_localize)
278 def tz_localize(self, tz, ambiguous="raise", nonexistent="raise") -> DatetimeIndex:
--> 279 arr = self._data.tz_localize(tz, ambiguous, nonexistent)
280 return type(self)._simple_new(arr, name=self.name)
281
~/miniforge3/lib/python3.9/site-packages/pandas/core/arrays/_mixins.py in method(self, *args, **kwargs)
56 def method(self, *args, **kwargs):
57 if self.ndim == 1:
---> 58 return meth(self, *args, **kwargs)
59
60 flags = self._ndarray.flags
~/miniforge3/lib/python3.9/site-packages/pandas/core/arrays/datetimes.py in tz_localize(self, tz, ambiguous, nonexistent)
1022 # Convert to UTC
1023
-> 1024 new_dates = tzconversion.tz_localize_to_utc(
1025 self.asi8, tz, ambiguous=ambiguous, nonexistent=nonexistent
1026 )
~/miniforge3/lib/python3.9/site-packages/pandas/_libs/tslibs/tzconversion.pyx in pandas._libs.tslibs.tzconversion.tz_localize_to_utc()
NonExistentTimeError: 2022-03-27 02:00:00
From provided link https://pandas.pydata.org/docs/reference/api/pandas.DatetimeIndex.tz_localize.html
nonexistentstr, default 'raise' A nonexistent time does not exist in a particular timezone where clocks moved forward due to DST. Valid values are:
- 'shift_forward' will shift the nonexistent time forward to the closest existing time
- 'shift_backward' will shift the nonexistent time backward to the closest existing time
- 'NaT' will return NaT where there are nonexistent times
- timedelta objects will shift nonexistent times by the timedelta 'raise' will raise an NonExistentTimeError if there are nonexistent times.
So, your code will work with such params:
e tc as written in the documentation
dates = pd.date_range(start="2022-01-01", end='2022-06-25', freq="30min")
dates.tz_localize('Europe/Warsaw',nonexistent='NaT')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.