Repository: pandas-dev/pandas

Issue:
Assignment using .ix[] raises ValueError
This works with `0.12` but not with master:

``` python
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: https://github.com/hddm-devs/hddm/issues/35



Discussion:

Resolving patch:
diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py
--- a/pandas/core/indexing.py
+++ b/pandas/core/indexing.py
@@ -421,7 +421,7 @@ def can_do_equal_len():
 
     def _align_series(self, indexer, ser):
         # indexer to assign Series can be tuple, slice, scalar
-        if isinstance(indexer, slice):
+        if isinstance(indexer, (slice, np.ndarray, list)):
             indexer = tuple([indexer])
 
         if isinstance(indexer, tuple):
@@ -453,8 +453,12 @@ def _align_series(self, indexer, ser):
                     all([com._is_sequence(_) for _ in indexer])):
                 ser = ser.reindex(obj.axes[0][indexer[0].ravel()],
                                   copy=True).values
-                l = len(indexer[1].ravel())
-                ser = np.tile(ser, l).reshape(l, -1).T
+
+                # single indexer
+                if len(indexer) > 1:
+                    l = len(indexer[1].ravel())
+                    ser = np.tile(ser, l).reshape(l, -1).T
+
                 return ser
 
             for i, idx in enumerate(indexer):


Test patch:
diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py
--- a/pandas/tests/test_indexing.py
+++ b/pandas/tests/test_indexing.py
@@ -476,6 +476,20 @@ def test_loc_setitem(self):
         expected = Series([1,1,0],index=[4,5,6])
         assert_series_equal(s, expected)
 
+        # GH 5928
+        # chained indexing assignment
+        df = DataFrame({'a' : [0,1,2] })
+        expected = df.copy()
+        expected.ix[[0,1,2],'a'] = -expected.ix[[0,1,2],'a']
+
+        df['a'].ix[[0,1,2]] = -df['a'].ix[[0,1,2]]
+        assert_frame_equal(df,expected)
+
+        df = DataFrame({'a' : [0,1,2], 'b' :[0,1,2] })
+        df['a'].ix[[0,1,2]] = -df['a'].ix[[0,1,2]].astype('float64') + 0.5
+        expected = DataFrame({'a' : [0.5,-0.5,-1.5], 'b' : [0,1,2] })
+        assert_frame_equal(df,expected)
+
     def test_loc_getitem_int(self):
 
         # int label