Skip to content

BUG: AttributeError when trying to assign a value to an element of a MultiIndex DataFrame #5216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
glyg opened this issue Oct 14, 2013 · 1 comment · Fixed by #5217
Closed
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@glyg
Copy link
Contributor

glyg commented Oct 14, 2013

Code snipet producing the (unexpected) ValueError (versions of pandas and numpy printed after the traceback)

import numpy as np
import pandas as pd
print('pandas : %s' % pd.__version__)
print('numpy : %s' % np.__version__)
a = np.random.rand(10, 3)
df = pd.DataFrame(a, columns=['x', 'y', 'z'])
tuples = [(i, j) for i in range(5) for j in range(2)]
index = pd.MultiIndex.from_tuples(tuples)
df.index = index
df.loc[0]['z'].iloc[0] = 1.

output:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-8152e4c5a53b> in <module>()
      8 index = pd.MultiIndex.from_tuples(tuples)
      9 df.index = index
---> 10 df.loc[0]['z'].iloc[0] = 1.

/usr/local/lib/python2.7/dist-packages/pandas-0.12.0_852_gb76b265-py2.7-linux-i686.egg/pandas/core/indexing.pyc in __setitem__(self, key, value)
     92             indexer = self._convert_to_indexer(key, is_setter=True)
     93 
---> 94         self._setitem_with_indexer(indexer, value)
     95 
     96     def _has_valid_type(self, k, axis):

/usr/local/lib/python2.7/dist-packages/pandas-0.12.0_852_gb76b265-py2.7-linux-i686.egg/pandas/core/indexing.pyc in _setitem_with_indexer(self, indexer, value)
    364 
    365             self.obj._data = self.obj._data.setitem(indexer,value)
--> 366             self.obj._maybe_update_cacher(clear=True)
    367 
    368     def _align_series(self, indexer, ser):

/usr/local/lib/python2.7/dist-packages/pandas-0.12.0_852_gb76b265-py2.7-linux-i686.egg/pandas/core/generic.pyc in _maybe_update_cacher(self, clear)
    944         cacher = getattr(self,'_cacher',None)
    945         if cacher is not None:
--> 946             cacher[1]()._maybe_cache_changed(cacher[0],self)
    947         if clear:
    948             self._clear_item_cache()

AttributeError: 'NoneType' object has no attribute '_maybe_cache_changed'

pandas : 0.12.0-852-gb76b265
numpy : 1.8.0.dev-d310678
@jreback
Copy link
Contributor

jreback commented Oct 14, 2013

In [40]: pd.__version__
Out[40]: '0.12.0-852-gb76b265'

That is an invalid assignment because its chained, see here

In [34]: df2
Out[34]: 
            x         y         z
0 0  0.286564  0.050018  0.111550
  1  0.964252  0.691074  0.336067
1 0  0.536750  0.368807  0.199002
  1  0.826987  0.316490  0.574972
2 0  0.547737  0.663395  0.312058
  1  0.925936  0.279883  0.926590
3 0  0.701851  0.320985  0.462495
  1  0.062629  0.471302  0.438546
4 0  0.156002  0.314738  0.756440
  1  0.763509  0.858044  0.403415

In [35]: df2.loc[(0,0),'z'] = 1

In [38]: df2.loc[0,'z'].iloc[1] = 2

In [39]: df2
Out[39]: 
            x         y         z
0 0  0.286564  0.050018  1.000000
  1  0.964252  0.691074  2.000000
1 0  0.536750  0.368807  0.199002
  1  0.826987  0.316490  0.574972
2 0  0.547737  0.663395  0.312058
  1  0.925936  0.279883  0.926590
3 0  0.701851  0.320985  0.462495
  1  0.062629  0.471302  0.438546
4 0  0.156002  0.314738  0.756440
  1  0.763509  0.858044  0.403415

35 is the way to do this guaranteed; 38 works, but is chained (though happens to work)

Separately I will looks at the exception....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants