TempList[:5]
[(datetime.datetime(2015, 11, 25, 7, 29, 28, 337000),), (datetime.datetime(2015, 9, 8, 7, 44, 55, 53000),), (datetime.datetime(2015, 9, 10, 5, 44, 51, 867000),), (datetime.datetime(2015, 9, 10, 1, 42, 15, 740000),), (datetime.datetime(2015, 9, 2, 1, 7, 09, 687000),)]
我想将它转换为Pandas Time Series.
我发现这很有用function: pd.to_datetime.但是当我尝试的时候
转换它:
Dates = pd.to_datetime(TempList)
我收到此错误:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pandas/util/decorators.py", line 91, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/pandas/tseries/tools.py", line 291, in to_datetime
unit=unit, infer_datetime_format=infer_datetime_format)
File "/usr/local/lib/python2.7/dist-packages/pandas/tseries/tools.py", line 427, in _to_datetime
return _convert_listlike(arg, Box, format)
File "/usr/local/lib/python2.7/dist-packages/pandas/tseries/tools.py", line 341, in _convert_listlike
raise TypeError('arg must be a string, datetime, list, tuple, '
TypeError: arg must be a string, datetime, list, tuple, 1-d array, or Series
但是TempList是一个清单!我无法理解如何解决这个问题.我怎样才能转换我的清单?
谢谢!
解决方法:
你有一个元组列表.尝试将其转换为普通列表,然后使用pd.to_datetime.
像(我现在没有python)
tl2 = [ ti[0] for ti in TempList ]
Dates = pd.to_datetime(tl2)
PS1:你在TempList中还有什么? (你只显示前5个元素).
PS2:您可能需要使用format参数,请参阅https://stackoverflow.com/a/26763793/2707864.