[英]Using zoneinfo with pandas.date_range
I am trying to use zoneinfo
instead of pytz
.我正在尝试使用
zoneinfo
而不是pytz
。 I am running into a problem using zoneinfo
to initiate dates and passing it on to pd.date_range
.我在使用
zoneinfo
启动日期并将其传递给pd.date_range
时遇到问题。
Below is an example of doing the exact same thing with pytz
and with zoneinfo
.下面是一个用
pytz
和zoneinfo
做同样事情的例子。 But, while passing it to pd.date_range
getting an error with the latter.但是,在将它传递给
pd.date_range
时,后者出现错误。
pytz example:皮茨示例:
start_date = datetime(2021, 1, 1, 0, 0, 0)end_date = datetime(2024, 1, 1, 0, 0, 0) # exclusive end range
pt = pytz.timezone('Canada/Pacific')start_date = pt.localize(start_date)end_date = pt.localize(end_date)
pd.date_range(start_date, end_date-timedelta(days=1), freq='d')
zoneinfo example:区域信息示例:
start_date1 = '2021-01-01 00:00:00
start_date1 = datetime.strptime(start_date1, '%Y-%m-%d %H:%M:%S').replace(microsecond=0, second=0, minute=0, tzinfo=ZoneInfo("America/Vancouver"))end_date1 = start_date1 + relativedelta(years=3)
pd.date_range(start_date1, end_date1-timedelta(days=1), freq='d')
Yet, when using zoneinfo
I get the following error:但是,在使用
zoneinfo
时出现以下错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~/Documents/GitHub/virtual/lib/python3.9/site-packages/pandas/_libs/tslibs/timezones.pyx in pandas._libs.tslibs.timezones.get_dst_info()
AttributeError: 'NoneType' object has no attribute 'total_seconds'
Exception ignored in: 'pandas._libs.tslibs.tzconversion.tz_convert_from_utc_single'
Traceback (most recent call last):
File "pandas/_libs/tslibs/timezones.pyx", line 266, in pandas._libs.tslibs.timezones.get_dst_info
AttributeError: 'NoneType' object has no attribute 'total_seconds'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~/Documents/GitHub/virtual/lib/python3.9/site-packages/pandas/_libs/tslibs/timezones.pyx in pandas._libs.tslibs.timezones.get_dst_info()
AttributeError: 'NoneType' object has no attribute 'total_seconds'
Exception ignored in: 'pandas._libs.tslibs.tzconversion.tz_convert_from_utc_single'
Traceback (most recent call last):
File "pandas/_libs/tslibs/timezones.pyx", line 266, in pandas._libs.tslibs.timezones.get_dst_info
AttributeError: 'NoneType' object has no attribute 'total_seconds'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/var/folders/vp/7ptlp5l934vdh1lvmpgk4qyc0000gn/T/ipykernel_67190/3566591779.py in <module>
5 end_date1 = start_date1 + relativedelta(years=3)
6
----> 7 pd.date_range(start_date1, end_date1-timedelta(days=1), freq='d')
8
9 # Because certain distributions will be a result of combined distributions,
~/Documents/GitHub/virtual/lib/python3.9/site-packages/pandas/core/indexes/datetimes.py in date_range(start, end, periods, freq, tz, normalize, name, closed, **kwargs)
1095 freq = "D"
1096
-> 1097 dtarr = DatetimeArray._generate_range(
1098 start=start,
1099 end=end,
~/Documents/GitHub/virtual/lib/python3.9/site-packages/pandas/core/arrays/datetimes.py in _generate_range(cls, start, end, periods, freq, tz, normalize, ambiguous, nonexistent, closed)
450
451 if tz is not None and index.tz is None:
--> 452 arr = tzconversion.tz_localize_to_utc(
453 index.asi8, tz, ambiguous=ambiguous, nonexistent=nonexistent
454 )
~/Documents/GitHub/virtual/lib/python3.9/site-packages/pandas/_libs/tslibs/tzconversion.pyx in pandas._libs.tslibs.tzconversion.tz_localize_to_utc()
~/Documents/GitHub/virtual/lib/python3.9/site-packages/pandas/_libs/tslibs/timezones.pyx in pandas._libs.tslibs.timezones.get_dst_info()
AttributeError: 'NoneType' object has no attribute 'total_seconds'
Testing the parameters:测试参数:
start_date==start_date1
and和
end_date==end_date1
Both tests result in True
. end_date==end_date1
两个测试的结果都是True
。
if understanding correctly you want to create a date range (1D freq) using ZoneInfo…if correct I see a few things going on with your code.如果理解正确,您想使用 ZoneInfo 创建一个日期范围(一维频率)……如果正确,我会看到您的代码发生了一些变化。
#1 When dealing with datetimes be sure the object is in the correct dtype. #1 在处理日期时间时,确保 object 的数据类型正确。 I believe datetime64 format will work better.
我相信 datetime64 格式会更好用。
#2 From the provide code I don't think 'strptime' or 'replace' are needed. #2 从提供的代码来看,我认为不需要“strptime”或“replace”。 To access "America/Vancouver" within ZoneInfo you can make it work if you parse start_date1 into years, months, days, hours and minutes.
要在 ZoneInfo 中访问“America/Vancouver”,如果将 start_date1 解析为年、月、日、小时和分钟,则可以使其正常工作。
#3 When start_date1 is parsed, you can add 3 to years (or another number) to create the end date. #3 解析 start_date1 时,您可以将年份(或其他数字)加 3 以创建结束日期。
The above will create a DatetimeIndex over the specified range.以上将在指定范围内创建 DatetimeIndex。
Datetimes are always tricky.日期时间总是很棘手。 As always you can get to the same destination using different paths…this is just one of them.
与往常一样,您可以使用不同的路径到达同一目的地……这只是其中之一。
start_date_str = '2021-01-01 00:00:00'
start_date_datetime64 = pd.to_datetime(start_date_str) # change dtype to datetime64
year = start_date_datetime64.year
month = start_date_datetime64.month
day = start_date_datetime64.day
hour = start_date_datetime64.hour
minute = start_date_datetime64.minute
start_date_formatted = dt.datetime(year, month, day, hour, minute, tzinfo=ZoneInfo("America/Vancouver"))
end_date_formatted = dt.datetime(year + 3, month, day, hour, minute, tzinfo=ZoneInfo("America/Vancouver"))
result = pd.date_range(start_date_formatted, end_date_formatted-pd.Timedelta(days=1), freq='d')
OUTPUT- DatetimeIndex, dtype='datetime64[ns, America/Vancouver]', length=1095, freq='D')输出- DatetimeIndex, dtype='datetime64[ns, America/Vancouver]', length=1095, freq='D')
This error was a result of compatibility between the pandas
version and the nbformat
version.此错误是
pandas
版本与nbformat
版本之间兼容的结果。 Once I updated both to the newest version, the code worked with no error.一旦我将两者都更新到最新版本,代码就可以正常工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.