Skip to content

Assignment using .ix[] raises ValueError #5928

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
twiecki opened this issue Jan 13, 2014 · 2 comments · Fixed by #5930
Closed

Assignment using .ix[] raises ValueError #5928

twiecki opened this issue Jan 13, 2014 · 2 comments · Fixed by #5930
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@twiecki
Copy link
Contributor

twiecki commented Jan 13, 2014

This works with 0.12 but not with master:

In [8]: import pandas as pd
In [10]: x = pd.DataFrame({'a': [1, 2, 3]})
In [13]: x['a'].ix[[0, 1, 2]] = -x['a'].ix[[0, 1, 2]]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-30fcfd21bb22> in <module>()
----> 1 x['a'].ix[[0, 1, 2]] = -x['a'].ix[[0, 1, 2]]

/home/wiecki/envs/hddm/local/lib/python2.7/site-packages/pandas/core/indexing.py in __setitem__(self, key, value)
     94             indexer = self._convert_to_indexer(key, is_setter=True)
     95 
---> 96         self._setitem_with_indexer(indexer, value)
     97 
     98     def _has_valid_type(self, k, axis):

/home/wiecki/envs/hddm/local/lib/python2.7/site-packages/pandas/core/indexing.py in _setitem_with_indexer(self, indexer, value)
    409 
    410             if isinstance(value, ABCSeries):
--> 411                 value = self._align_series(indexer, value)
    412 
    413             elif isinstance(value, ABCDataFrame):

/home/wiecki/envs/hddm/local/lib/python2.7/site-packages/pandas/core/indexing.py in _align_series(self, indexer, ser)
    516             return ser.reindex(ax).values
    517 
--> 518         raise ValueError('Incompatible indexer with Series')
    519 
    520     def _align_frame(self, indexer, df):

ValueError: Incompatible indexer with Series
> /home/wiecki/envs/hddm/local/lib/python2.7/site-packages/pandas/core/indexing.py(518)_align_series()
    517 
--> 518         raise ValueError('Incompatible indexer with Series')
    519 

This is where it showed up: hddm-devs/hddm#35

@jreback
Copy link
Contributor

jreback commented Jan 13, 2014

@twiecki I fixed this, but you really should do this like

df.ix[[0,1,2],'a'] = -df.ix[[0,1,2],'a']

your method will in general fail when assigning a non-trivial rhs as you would be assigning to a copy (this specific does work as its a complete assignment within a signle dtype)

the proper ix usage will always work

this is chained assignment, see here: http://pandas.pydata.org/pandas-docs/dev/indexing.html#indexing-view-versus-copy

@twiecki
Copy link
Contributor Author

twiecki commented Jan 14, 2014

Thanks, fixed with hddm-devs/hddm@fa4e949.

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