Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get python dictionaries into a pandas time series dataframe where key is date object

Tags:

python

pandas

I have a python dictionaries where the key is a dateobject and the value is the timeseires.

   timeseries = {datetime.datetime(2013, 3, 17, 18, 19): {'t2': 400, 't1': 1000},
                 datetime.datetime(2013, 3, 17, 18, 20): {'t2': 300, 't1': 3000}
                }

How to I get this time series into a pandas dataframe?

like image 293
Tampa Avatar asked Dec 08 '22 16:12

Tampa


1 Answers

use DataFrame.from_dict:

import pandas as pd
import datetime
timeseries = {datetime.datetime(2013, 3, 17, 18, 19): {'t2': 400, 't1': 1000},
                 datetime.datetime(2013, 3, 17, 18, 20): {'t2': 300, 't1': 3000}
                }
print pd.DataFrame.from_dict(timeseries, orient="index")

output:

                      t2    t1
2013-03-17 18:19:00  400  1000
2013-03-17 18:20:00  300  3000
like image 53
HYRY Avatar answered Dec 11 '22 09:12

HYRY