Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么read_csv()不解析我的日期?_Python_Pandas - Fatal编程技术网

Python 为什么read_csv()不解析我的日期?

Python 为什么read_csv()不解析我的日期?,python,pandas,Python,Pandas,我正在运行熊猫0.7.0。我有一个文件,其中的行如下: 2014-01-12T00:00:00+00:00, 0.210079 当我读到这些的时候 data = pd.read_csv('xx', names=["t", "p"], parse_dates=[0]) 最后第一列是字符串。为什么它们不被解析成日期时间 print data.head() t p 0 2014-01-12T00:00:00+00:00 0.

我正在运行熊猫0.7.0。我有一个文件,其中的行如下:

2014-01-12T00:00:00+00:00, 0.210079
当我读到这些的时候

data = pd.read_csv('xx', names=["t", "p"], parse_dates=[0])
最后第一列是字符串。为什么它们不被解析成日期时间

print data.head()
                           t         p
0  2014-01-12T00:00:00+00:00  0.210079
1  2014-01-12T00:00:00+00:00  0.078217
2  2014-01-12T00:00:00+00:00  0.342977
3  2014-01-12T00:00:00+00:00  0.346713
4  2014-01-12T00:00:00+00:00  0.224601
正在正确解析它:

您会注意到,您在数据中提到的日期,我根据您的问题使用了一个示例,它是一个时间戳obhect,似乎表示解析的数据

更新:

在使用dir对Python shell进行了更多的研究之后,我发现了更多的证据,证明在本例中它被正确地解析为日期/时间对象时间戳:

>>> data.to_dict()["t"][0]
Timestamp('2014-01-12 00:00:00', tz=None)
>>> dir(data.to_dict()["t"][0])
['__add__', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__pyx_vtable__', '__qualname__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rsub__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__weakref__', '_get_field', '_repr_base', 'asm8', 'astimezone', 'combine', 'ctime', 'date', 'day', 'dayofweek', 'dayofyear', 'dst', 'freq', 'freqstr', 'fromordinal', 'fromtimestamp', 'hour', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'microsecond', 'min', 'minute', 'month', 'nanosecond', 'now', 'offset', 'quarter', 'replace', 'resolution', 'second', 'strftime', 'strptime', 'time', 'timetuple', 'timetz', 'to_datetime', 'to_period', 'to_pydatetime', 'today', 'toordinal', 'tz', 'tz_convert', 'tz_localize', 'tzinfo', 'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset', 'utctimetuple', 'value', 'week', 'weekday', 'weekofyear', 'year']
>>> data.to_dict()["t"][0].timetuple()
time.struct_time(tm_year=2014, tm_mon=1, tm_mday=12, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=12, tm_isdst=-1)
>>>

顺便说一下。。。如果您打印data.head,这是打印以筛选data.head返回的对象的表示形式。这可能是你困惑的根源,但它只是一种表现。请参阅:

不要使用打印检查数据类型。请检查data.dtypes的输出,如果t的类型与datetime64[ns]不同,请在此处发布您使用的pandas的哪个版本?如上所述,我正在运行pandas 0.7.0。这不是我得到的结果。>>>data=pd.read_csvxx1,names=[t,p],parse_dates=[0]>>data.to_dict{'p':{0:0.210078999999999},'t':{0:'2014-01-12T00:00:00+00:00'}>>data.to_dict[t][0]。时间元组回溯最近的调用:文件,第1行,在AttributeError中:“str”对象没有属性“timetuple”@RoySmith,我建议您升级您的pandas版本。我用最新的版本进行了测试,效果很好。我刚刚升级到0.12.0哇,我没有意识到我在版本上落后了这么多。现在工作。谢谢
>>> data.to_dict()["t"][0]
Timestamp('2014-01-12 00:00:00', tz=None)
>>> dir(data.to_dict()["t"][0])
['__add__', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__pyx_vtable__', '__qualname__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rsub__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__weakref__', '_get_field', '_repr_base', 'asm8', 'astimezone', 'combine', 'ctime', 'date', 'day', 'dayofweek', 'dayofyear', 'dst', 'freq', 'freqstr', 'fromordinal', 'fromtimestamp', 'hour', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'microsecond', 'min', 'minute', 'month', 'nanosecond', 'now', 'offset', 'quarter', 'replace', 'resolution', 'second', 'strftime', 'strptime', 'time', 'timetuple', 'timetz', 'to_datetime', 'to_period', 'to_pydatetime', 'today', 'toordinal', 'tz', 'tz_convert', 'tz_localize', 'tzinfo', 'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset', 'utctimetuple', 'value', 'week', 'weekday', 'weekofyear', 'year']
>>> data.to_dict()["t"][0].timetuple()
time.struct_time(tm_year=2014, tm_mon=1, tm_mday=12, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=12, tm_isdst=-1)
>>>