为什么此日期时字符串未转换为Pandas数据帧中的日期时间对象?

时间:2016-01-14 03:46:12

标签: python datetime pandas

我正在尝试将数据框中的列从object转换为datetime64[ns]。我使用to_datetime来完成此操作,但在我的代码末尾,该列仍然是object

import pandas as pd
from StringIO import StringIO

DATA = StringIO("""id;Date of Event
3574;2015-12-12 22:03:28Z
0657;2015-08-25 17:48:03Z
0408;2015-10-13 12:01:32Z
3043;2015-09-08 16:55:43Z
9397;2015-09-09 09:33:31Z
9291;2015-07-15 08:13:48Z
4263;2015-12-30 09:25:55Z
0200;2015-10-25 13:38:35Z
8576;2015-09-01 02:01:47Z
6023;2015-08-29 20:47:20Z
9975;2015-10-05 15:11:32Z
5202;2015-12-21 23:44:10Z
9278;2015-12-22 05:56:03Z
8520;2015-09-05 01:27:07Z
9048;2015-08-29 18:38:26Z
9624;2015-12-09 01:49:15Z
2659;2015-10-03 01:43:50Z
6230;2015-10-16 11:43:40Z
2272;2015-11-18 14:15:52Z
""")

df = pd.DataFrame.from_csv(DATA, sep=";")
pd.to_datetime(df['Date of Event'], format="%Y-%m-%d %H:%M:%SZ")
print df['Date of Event'].dtype

最终的印刷品显示:

object

df.info()会返回此信息:

Int64Index: 19 entries, 3574 to 2272
Data columns (total 1 columns):
Date of Event    19 non-null object
dtypes: object(1)
memory usage: 304.0+ bytes

为什么我的pd.to_datetime(df['Date of Event'], format="%Y-%m-%d %H:%M:%SZ")无法将列转换为datetime个对象,我该如何更正呢?

格式有效,我可以使用datetime库测试:

>>> import datetime
>>> s = "2015-11-18 14:15:52Z"
>>> dt = datetime.datetime.strptime(s, "%Y-%m-%d %H:%M:%SZ")
>>> dt
datetime.datetime(2015, 11, 18, 14, 15, 52)

为什么整个Pandas列的转换失败?

1 个答案:

答案 0 :(得分:5)

to_datetime返回一个新结果,它不会修改其参数。重新分配:

>>> df['Date of Event'] = pd.to_datetime(df['Date of Event'], format="%Y-%m-%d %H:%M:%SZ")
>>> df.dtypes
Date of Event    datetime64[ns] 
dtype: object

或者使用parse_dates并在开始时对其进行转换(请注意,使用read_csvpd.DataFrame.from_csv更常见):

>>> df = pd.read_csv(DATA, sep=";", parse_dates=["Date of Event"])
>>> df.dtypes
id                        int64
Date of Event    datetime64[ns]
dtype: object