user2866103
user2866103

Reputation: 10555

Setting value in pandas mulitindex fails

I have a mulitindexed dataframe, where i wish to se som values:

import pandas as pd
import itertools
a = pd.period_range('2013Q1','2013Q4', freq='Q')
i = (1111, 2222, 3333)
idx = pd.MultiIndex.from_tuples(list(itertools.product(a, i)),
                            names=('Periode', 'CVR'))
df = pd.DataFrame(index=idx,
              columns=('OMS', 'OMK','RES','DRIFT_IND','OEVRIG_IND','FIN_IND','VARE_UD','LOEN_UD','FIN_UD'))

Using this to acces a value works:

df.loc[('2013Q1',1111),'OMS']

However this fails:

df.loc[('2013Q1',1111),'OMS']=1

with the following errror:

 ValueError: can only call with other PeriodIndex-ed objects

Does anyone know that the problem is?

Upvotes: 1

Views: 589

Answers (1)

joris
joris

Reputation: 139222

This works for both accessing and assigning if you use a Period object explicitely instead of a string:

In [9]: df.loc[(pd.Period('2013Q1'),1111),'OMS']
Out[9]: nan

In [10]: df.loc[(pd.Period('2013Q1'),1111),'OMS'] = 1

In [11]: df.loc[(pd.Period('2013Q1'),1111),'OMS']
Out[11]: 1

But I filed an issue for this: https://github.com/pydata/pandas/issues/9892

Upvotes: 2

Related Questions