[英]Python, Pandas: tz_localize AmbiguousTimeError: Cannot infer dst time with non DST dates
[英]Cannot `. tz_localize()` to an European Time Zone
我正在創建一個date_range
:
dates = pd.date_range(start="2022-01-01", end='2022-06-25', freq="30min")
我想按照本教程的說明設置時區:
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
從提供的鏈接https://pandas.pydata.org/docs/reference/api/pandas.DatetimeIndex.tz_localize.html
nonexistentstr, default 'raise'不存在的時間在特定時區不存在,由於 DST,時鍾向前移動。 有效值為:
- 'shift_forward' 將不存在的時間向前移動到最接近的現有時間
- 'shift_backward' 將不存在的時間向后移動到最接近的現有時間
- 'NaT' 將返回不存在時間的 NaT
- timedelta 對象將不存在的時間移動 timedelta 如果有不存在的時間,“raise”將引發 NonExistentTimeError。
因此,您的代碼將使用以下參數:
e tc 如文檔中所寫
dates = pd.date_range(start="2022-01-01", end='2022-06-25', freq="30min")
dates.tz_localize('Europe/Warsaw',nonexistent='NaT')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.