python - -753 在 `pd.Timestamp` 中的时区意味着什么

标签 python pandas timestamp

tz = pytz.timezone('America/Los_Angeles')
t1 = pd.Timestamp(datetime.datetime(2019, 2, 6, 17, 0, 0, tzinfo=tz))
t1

输出:

Timestamp('2019-02-06 17:00:00-0753', tz='America/Los_Angeles')

为什么是-0753?

更新: 经过一些研究,这种方式似乎有效: 我可能没有以正确的方式进行操作,请参见下文

tz = pytz.timezone('America/Los_Angeles')
t1 = datetime.datetime(2019, 2, 6, 17, 0, 0)
t1 = tz.localize(t1)
t1 = pd.Timestamp(t1)
t1

输出:

Timestamp('2019-02-06 17:00:00-0800', tz='America/Los_Angeles')

那么tzinfo传递给datetime.datetime应该是一个什么样的对象呢?

最佳答案

我不知道为什么,但是这个奇怪的时区偏移量来自 pytz。见下面的代码:

>>>print(datetime(2019, 5, 10, tzinfo=pytz.timezone('America/Los_Angeles')))
2019-05-10 00:00:00-07:53

>>>print(pytz.timezone('America/Los_Angeles').localize(datetime(2019, 5, 10)))
2019-05-10 00:00:00-07:00

因此,如果您尝试创建日期时间并提供 tzinfo,它将创建此偏移量。

更新。

我检查过 pytz docs并找到下一个:

Unfortunately using the tzinfo argument of the standard datetime constructors ‘’does not work’’ with pytz for many timezones.

...

It is safe for timezones without daylight saving transitions though, such as UTC.

好吧,他们说了,但不要指出原因。让我们试着找到它。在 pytz 资源中,我找到了 IANA database 的版本他们使用:

OLSON_VERSION = '2019a'

在文件“northamerica”中下载并解压该数据库后,我发现了下一个:

# Zone  NAME        GMTOFF  RULES   FORMAT  [UNTIL]
Zone America/Los_Angeles -7:52:58 - LMT 1883 Nov 18 12:07:02
            -8:00   US  P%sT    1946
            -8:00   CA  P%sT    1967
            -8:00   US  P%sT

-7:52:58 非常接近我们得到的 -07:53

结论: 在 pytz 的某个地方有一个包含所有已知时区偏移量的数据库。当我们将 tzinfo 传递给 datetime 的构造函数时,它会获得第一个已知时区,并在调用 replace() 的本地化方法时使用它。并传递 tzinfo,以某种方式获得正确的时区偏移量。

为了检查它,我在同一个文件中找到了另一个时区:

# Zone  NAME        GMTOFF  RULES   FORMAT  [UNTIL]
Zone America/Toronto    -5:17:32 -  LMT 1895
            -5:00   Canada  E%sT    1919
            -5:00   Toronto E%sT    1942 Feb  9  2:00s
            -5:00   Canada  E%sT    1946
            -5:00   Toronto E%sT    1974
            -5:00   Canada  E%sT

然后我启动了下一个代码:

>>>print(datetime(2019, 5, 10, tzinfo=pytz.timezone('America/Toronto')))
2019-05-10 00:00:00-05:18

如您所见,结果是一样的。它使用 -5:17:32,这是列表的第一个偏移量。

关于python - -753 在 `pd.Timestamp` 中的时区意味着什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56081666/

相关文章:

python - Pyspark 将 NaN 替换为 NULL

python - Pandas :拆分数据框行并重新排列列值

python - 如何在 Python 中构建朴素贝叶斯模型时使用时间戳数据

php - 在 php 中获取给定月份和年份的开始和结束 unix 时间戳

java - JPA CriteriaQuery 比较时间戳忽略时间部分

php - "Casting"DATETIME 列的 MySQL TIMESTAMP 值 : any gotchas?

python - Groupby 序列计数和序列持续时间

在任意位置插入列的 Pythonic 方式

Python imaplib 选择文件夹

python - 将数据帧分成 10 个相等的部分,并在循环中一次选取一个部分后合并 9 个部分