英文:
pandas how to override attibute error from timezome localization
问题
我有一个 **idaydf.index**,我正在尝试本地化时区
DatetimeIndex(['2022-10-24 16:00:00', '2022-10-24 16:15:00',
...
'2023-06-16 21:58:00', '2023-06-16 22:00:00'],
dtype='datetime64[ns]', name='DateTime', length=9012, freq=None)
使用
idaydf.index = idaydf.index.tz_localize(LOCAL_TZ)
其中LOCAL_TZ是
_PytzShimTimezone(zoneinfo.ZoneInfo(key='Europe/London'), 'Europe/London')
我收到以下错误:
*** AttributeError: 'NoneType' object has no attribute 'total_seconds'
我有以下版本:
python3-3.11.3
pandas-1.5.3
pytz-2023.3-1
tzlocal 4.2
如何修复?
英文:
I have a idaydf.index that I am trying to localize timezone
DatetimeIndex(['2022-10-24 16:00:00', '2022-10-24 16:15:00',
...
'2023-06-16 21:58:00', '2023-06-16 22:00:00'],
dtype='datetime64[ns]', name='DateTime', length=9012, freq=None)
with
idaydf.index = idaydf.index.tz_localize(LOCAL_TZ)
where LOCAL_TZ is
_PytzShimTimezone(zoneinfo.ZoneInfo(key='Europe/London'), 'Europe/London')
I get this error:
*** AttributeError: 'NoneType' object has no attribute 'total_seconds'
I have these versions:
python3-3.11.3
pandas-1.5.3
pytz-2023.3-1
tzlocal 4.2
How to fix?
答案1
得分: 0
你可以使用时区对象的字符串表示来解耦。这样做的好处是:
- pandas 可以选择任何库来处理时区(他们预计在 2023 年仍然从 pytz 切换到 zoneinfo)
- 生成
LOCAL_TZ
的库可以在不崩溃您的代码的情况下切换到 zoneinfo - 您可以避免使用
try/except
例如:
from zoneinfo import ZoneInfo
import pytz_deprecation_shim as pds
import pandas as pd
LOCAL_TZ = pds.timezone('Europe/London')
print(repr(LOCAL_TZ))
# _PytzShimTimezone(zoneinfo.ZoneInfo(key='Europe/London'), 'Europe/London')
LOCAL_TZ_new = ZoneInfo('Europe/London')
print(repr(LOCAL_TZ_new))
# zoneinfo.ZoneInfo(key='Europe/London')
dti = pd.date_range('2022-10-24 16:00:00', '2023-06-16 22:00:00')
dti0 = dti.tz_localize(str(LOCAL_TZ))
dti1 = dti.tz_localize(str(LOCAL_TZ_new))
assert (dti0 == dti1).all()
另外,tzlocal
提供了一种直接获取 IANA 时区名称作为字符串的方式,这可能会使事情更容易,如下所示:
from tzlocal import get_localzone_name
get_localzone_name()
# "Europe/Berlin" # 在我的机器上...
这在 tzlocal 版本 4.2 中已经可用。
英文:
You could use the string representation of the timezone object to decouple. That way,
- pandas can chose whatever library it wants to handle the timezone (they're supposed to switch from pytz to zoneinfo still in 2023)
- the library that generates
LOCAL_TZ
can switch to zoneinfo without crashing your code - you can avoid
try/except
s
Ex:
from zoneinfo import ZoneInfo
import pytz_deprecation_shim as pds
import pandas as pd
LOCAL_TZ = pds.timezone('Europe/London')
print(repr(LOCAL_TZ))
# _PytzShimTimezone(zoneinfo.ZoneInfo(key='Europe/London'), 'Europe/London')
LOCAL_TZ_new = ZoneInfo('Europe/London')
print(repr(LOCAL_TZ_new))
# zoneinfo.ZoneInfo(key='Europe/London')
dti = pd.date_range('2022-10-24 16:00:00', '2023-06-16 22:00:00')
dti0 = dti.tz_localize(str(LOCAL_TZ))
dti1 = dti.tz_localize(str(LOCAL_TZ_new))
assert (dti0 == dti1).all()
Btw. tzlocal
offers a way to directly obtain the IANA tz name as a string, which might make things even easier here -
from tzlocal import get_localzone_name
get_localzone_name()
# "Europe/Berlin" # on my machine...
This works already with tzlocal version 4.2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论