使用 pandas 转换字符串时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25693930/
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
Using pandas to convert string timestamps
提问by Paul Jtheitroademan
I've been attempting to use pandas.to_datetimeto convert between timestamp formats in my code base, however when provided with a string input sometimes pandasdoes not seem to extract the UTC offset correctly:
我一直在尝试使用pandas.to_datetime在我的代码库中的时间戳格式之间进行转换,但是当提供字符串输入时,有时pandas似乎无法正确提取 UTC 偏移量:
Here is a correct conversion, the UTC offset is correctly captured as reflected in the Timestamp object:
这是一个正确的转换,UTC 偏移量被正确捕获,反映在 Timestamp 对象中:
In[76]: pd.to_datetime('2014-04-09T15:29:59.999993-0500', utc=True)
Out[76]: Timestamp('2014-04-09 20:29:59.999993+0000', tz='UTC')
Here is an alternate string representation which is still a valid ISO 8601datetime strings but the UTC offset of -0500seems to be ignored:
这是一个替代字符串表示,它仍然是有效的ISO 8601日期时间字符串,但 UTC 偏移量-0500似乎被忽略:
In[77]: pd.to_datetime('2014-04-09T152959.999993-0500', utc=True)
Out[77]: Timestamp('2014-04-09 15:29:59.999993+0000', tz='UTC')
On the other hand the dateutilpackage handles things fine:
另一方面,dateutil包可以很好地处理事情:
In[78]: dateutil.parser.parse('2014-04-09T152959.999993-0500')
Out[78]: datetime.datetime(2014, 4, 9, 15, 29, 59, 999993, tzinfo=tzoffset(None, -18000))
I could certainly use dateutilbut is there some reason that pandas.to_datetimedoes not handle different ISO date time strings correctly. Am I doing something wrong here?
我当然可以使用,dateutil但是否有某种原因pandas.to_datetime不能正确处理不同的 ISO 日期时间字符串。我在这里做错了吗?
Using Python 2.7.6 and pandas 0.13.1
使用 Python 2.7.6 和 pandas 0.13.1
采纳答案by unutbu
Using pandas 0.14.0: both calls to pd.to_datetimereturn the correct, timezone-aware Timestamp:
使用 pandas 0.14.0:两个调用都pd.to_datetime返回正确的时区感知时间戳:
In [72]: pd.__version__
Out[72]: '0.14.0'
In [69]: pd.to_datetime('2014-04-09T152959.999993-0500', utc=True)
Out[69]: Timestamp('2014-04-09 20:29:59.999993+0000', tz='UTC')
In [70]: pd.to_datetime('2014-04-09T15:29:59.999993-0500', utc=True)
Out[70]: Timestamp('2014-04-09 20:29:59.999993+0000', tz='UTC')
In [71]: dateutil.parser.parse('2014-04-09T152959.999993-0500').astimezone(pytz.utc)
Out[71]: datetime.datetime(2014, 4, 9, 20, 29, 59, 999993, tzinfo=<UTC>)

