Hari Hari - 3 months ago 8
Python Question

Can't convert a Python List to timeSeries

I tried to resolve this problem for more than a day. I have this type of list:

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),)]


I would like to converti it as a Pandas Time Series.
I found this useful function: pd.to_datetime. But when I try
to convert it:

Dates = pd.to_datetime(TempList)


I got This Error:

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


But TempList is a list! I can't understand how to resolve this. How can I convert my list?
Thank you!

Answer

You have a list of tuples. Try converting that into a plain list, and then using pd.to_datetime. Something like (I do not have python right now)

tl2 = [ ti[0] for ti in TempList ]
Dates = pd.to_datetime(tl2)

PS1: What else do you have in TempList? (you only show the first 5 elements).

PS2: You might need to use the format argument, see http://stackoverflow.com/a/26763793/2707864.