pandas 从整数创建 tz 感知的熊猫时间戳对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30668665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:26:16  来源:igfitidea点击:

Creating tz aware pandas timestamp objects from an integer

numpypandasdatetime64

提问by Michael WS

I have a integer that is the number of microseconds after the unix epoch. (in GMT)

我有一个整数,它是 unix 时代之后的微秒数。(格林威治标准时间)

How can I convert 1349863207154117 using astype to a pandas.Timestamp("2012-10-10T06:00:07.154117", tz=¨UTC¨)? The documentation on astype is not very thorough. I have tried the following.

如何使用 astype 将 1349863207154117 转换为 pandas.Timestamp("2012-10-10T06:00:07.154117", tz=¨UTC¨)?关于 astype 的文档不是很详尽。我尝试了以下方法。

x = 1349863207154117
dt64 = np.int64(x).astype("M8[us]")
print dt64

returns:

返回:

np.datetime64("2012-10-10T06:00:07.154117-0400")
  1. if I only want seconds, this works:

    time = pd.Timestamp(datetime.datetime.fromtimestamp(int(x / 1e6)), tz=¨UTC¨)
    
  1. 如果我只想要几秒钟,这有效:

    time = pd.Timestamp(datetime.datetime.fromtimestamp(int(x / 1e6)), tz=¨UTC¨)
    

回答by Jeff

I would say this documentation herepretty much covers this.

我想说这个文件在这里几乎涵盖了这一点。

In [2]: pd.to_datetime(1349863207154117,unit='us')
Out[2]: Timestamp('2012-10-10 10:00:07.154117')

If you want this in a local timezone

如果你想在本地时区

In [6]: pd.to_datetime(1349863207154117,unit='us').tz_localize('US/Eastern')
Out[6]: Timestamp('2012-10-10 10:00:07.154117-0400', tz='US/Eastern')

If you time is in UTC, but you want it in another tz.

如果您的时间是 UTC,但您希望它是另一个 tz。

In [9]: pd.to_datetime(1349863207154117,unit='us').tz_localize('UTC').tz_convert('US/Eastern')
Out[9]: Timestamp('2012-10-10 06:00:07.154117-0400', tz='US/Eastern')

Or this

或这个

In [10]: pd.to_datetime(1349863207154117,unit='us',utc=True).tz_convert('US/Eastern')
Out[10]: Timestamp('2012-10-10 06:00:07.154117-0400', tz='US/Eastern')