nom-mon-ir
nom-mon-ir

Reputation: 3918

Python pandas read_csv like functionality from list to a DataFrame?

I have a list with values like the following:

[['2013-04-02 19:42:00.474', '1'],
['2013-04-02 19:42:00.529', '2'],
['2013-04-02 19:42:00.543', '3'],
['2013-04-02 19:42:00.592', '4'],
['2013-04-02 19:42:16.671', '5'],
['2013-04-02 19:42:16.686', '6'],
['2013-04-02 19:42:16.708', '7'],
['2013-04-02 19:42:16.912', '8'],
['2013-04-02 19:42:16.941', '9'],
['2013-04-02 19:42:19.721', '10'],
['2013-04-02 19:42:22.826', '11'],
['2013-04-02 19:42:25.609', '8'],
['2013-04-02 19:42:58.225', '5']]

I know if this were in a csv file, I could read it into a DataFrame with the Date and Timestamps put into index to make the DataFrame a time series.

How to achieve this without saving the list to a csv file?

I tried df=pd.DataFrame(tlist, columns=['date_time', 'count']) and then df=df.set_index('date_time')

But the index values are coming out as objects, rather than TimeStamps:

df.index

Index([2013-04-02 19:42:00.474, 2013-04-02 19:42:00.529, 2013-04-02 19:42:00.543, ............], dtype=object)

Upvotes: 0

Views: 637

Answers (1)

waitingkuo
waitingkuo

Reputation: 93854

In [40]: df.index = df.index.to_datetime()

In [41]: df.index
Out[41]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-04-02 19:42:00.474000, ..., 2013-04-02 19:42:58.225000]
Length: 13, Freq: None, Timezone: None

Upvotes: 3

Related Questions