Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas series from dictionary

Tags:

pandas

Here's a fragment of my data. I would like to convert this dictionary into a series with a multiindex. This seems aspect seems to work, however all values in the series are NaN?

d = {(datetime.date(2017, 10, 19), 1026972): 24.91, (datetime.date(2017, 10, 19), 1026973): 10.68, (datetime.date(2017, 10, 19), 1026974): 654.7}

I have tried pd.Series(d)

Any hints for me?

like image 704
tschm Avatar asked Oct 30 '17 10:10

tschm


2 Answers

Use multi index from tuples and set it as index, then pass the dict values to series.

s = pd.Series(list(d.values()),index=pd.MultiIndex.from_tuples(d.keys()))

Output :

2017-10-19  1026974    654.70
            1026973     10.68
            1026972     24.91
dtype: float64
like image 81
Bharath Avatar answered Oct 29 '22 21:10

Bharath


I think there is problem in Series constructor are dates convert to datetimes and then values are not matched with python dates - get NaNs.

s = pd.Series(d)
print (s.index.get_level_values(0))
DatetimeIndex(['2017-10-19', '2017-10-19', '2017-10-19'], dtype='datetime64[ns]', freq=None)

Solution is convert datetimes to timestamps or create Series separately like in Bharath answer:

d = {(pd.Timestamp(k[0]), k[1]):v for k, v in d.items()}

print (d)
{(Timestamp('2017-10-19 00:00:00'), 1026973): 10.68, 
 (Timestamp('2017-10-19 00:00:00'), 1026974): 654.7, 
 (Timestamp('2017-10-19 00:00:00'), 1026972): 24.91}

s = pd.Series(d)
print (s)

2017-10-19  1026972     24.91
            1026973     10.68
            1026974    654.70
dtype: float64
like image 22
jezrael Avatar answered Oct 29 '22 22:10

jezrael