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?
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
I think there is problem in Series
constructor are date
s convert to datetime
s and then values are not matched with python date
s - get NaN
s.
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 datetime
s to timestamp
s 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With