可以理解和预期(tz-aware):
import datetime
import numpy as np
import pandas as pd
aware = pd.DatetimeIndex(["2024-11-21", "2024-11-21 12:00"], tz="UTC")
eod = datetime.datetime.combine(aware[-1].date(), datetime.time.max, aware.tz)
aware, eod, np.concat([aware, [eod]])
退货
(DatetimeIndex(['2024-11-21 00:00:00+00:00', '2024-11-21 12:00:00+00:00'],
dtype='datetime64[ns, UTC]', freq=None),
datetime.datetime(2024, 11, 21, 23, 59, 59, 999999,
tzinfo=datetime.timezone.utc),
array([Timestamp('2024-11-21 00:00:00+0000', tz='UTC'),
Timestamp('2024-11-21 12:00:00+0000', tz='UTC'),
datetime.datetime(2024, 11, 21, 23, 59, 59, 999999,
tzinfo=datetime.timezone.utc)],
dtype=object))
注意 datetime
的返回值中的
时间戳(和
np.concat
)。
意外(tz-天真):
naive = pd.DatetimeIndex(["2024-11-21", "2024-11-21 12:00"])
eod = datetime.datetime.combine(naive[-1].date(), datetime.time.max, aware.tz)
naive, eod, np.concat([naive, [eod]])
退货
(DatetimeIndex(['2024-11-21 00:00:00', '2024-11-21 12:00:00'],
dtype='datetime64[ns]', freq=None),
datetime.datetime(2024, 11, 21, 23, 59, 59, 999999),
array([1732147200000000000, 1732190400000000000,
datetime.datetime(2024, 11, 21, 23, 59, 59, 999999)], dtype=object))
注意datetime
的返回值中的
intergers(和
np.concat
)。
DatetimeIndex
?PS。有趣的是,在
numpy
级别,索引是相同的:
np.testing.assert_array_equal(aware.values, naive.values)
这是所有项目都包含时区信息的唯一准确表示。换句话说,返回一个原始日期作为 Unix 时间戳可以使其在 UTC 时区上被识别。
也可以通过连接有意识的和朴素的对象来看到
print(np.concat([aware, naive]))
结果
[Timestamp('2024-11-21 00:00:00-0500', tz='America/New_York')
Timestamp('2024-11-21 12:00:00-0500', tz='America/New_York')
1732147200000000000 1732190400000000000]
TZ='UTC' date -d @1732190400 --iso=seconds
2024-11-21T12:00:00+00:00
Python 3.9 熊猫 2.2.2