Repository: pandas-dev/pandas

Issue:
assignment in mixed-type dataframes not working?
```
from pandas import DataFrame
df=DataFrame({'a':{1:'aaa',2:'bbb',3:'ccc'},'b':{1:111,2:222,3:333}})
#this works, new column is created correctly
df['test']=df['a'].apply(lambda x: '_' if x=='aaa' else x)
#this does not work, ie column test is not changed
idx=df['test']=='_'
temp=df.ix[idx,'a'].apply(lambda x: '-----' if x=='aaa' else x)
df.ix[idx,'test']=temp
#this works, column b is changed
temp=df.ix[idx,'a'].apply(lambda x: 99999 if x=='aaa' else x)
df.ix[idx,'b']=temp
#this does not work
temp=df.ix[idx,'a'].apply(lambda x: 'zzzzzz' if x=='aaa' else x)
df.ix[idx,'a']=temp
```

code works correctly on 0.9, not on 0.11



Discussion:
It appears that adding the column counts, since the following works as expected:

```
from pandas import DataFrame
df=DataFrame({'a':{1:'aaa',2:'bbb',3:'ccc'},'b':{1:111,2:222,3:333}})
idx=df['a']=='aaa'
temp=df.ix[idx,'a'].apply(lambda x: 99999 if x=='aaa' else x)
df.ix[idx,'b']=temp
temp=df.ix[idx,'a'].apply(lambda x: 'zzzzzz' if x=='aaa' else x)
df.ix[idx,'a']=temp
```

this works for me on 0.11, what exactly is the problem?

FYI lot has changed since 0.9....in particular a column assignment that create a different dtype should work (I am not sure if it did/did not in 0.9)

the first example should change df.iloc[0,0] to 'zzzzzz' while it remains set at 'aaa', df.iloc[0,2] should change from '_' to '-----'. On the other hand, df.iloc[0,1] is correctly modified from 111 to 99999. As mentioned, on a different computer with 0.9 it works, on my development computer where I just installed 0.11 it does not work.

Also, I would add that results are inconsistent if I run the above script "interactively", i.e. one command at a time. For example, if I only run the first 8 lines:

from pandas import DataFrame
df=DataFrame({'a':{1:'aaa',2:'bbb',3:'ccc'},'b':{1:111,2:222,3:333}})
#this works, new column is created correctly
df['test']=df['a'].apply(lambda x: '_' if x=='aaa' else x)
#this does not work, ie column test is not changed
idx=df['test']=='_'
temp=df.ix[idx,'a'].apply(lambda x: '-----' if x=='aaa' else x)
df.ix[idx,'test']=temp

if I look at df, then element [0,2] equals '_'. If instead I type df.ix[idx,'test'], I get '-----', finally by typing df.iloc[0,2] I get '_'.

So either there is something wrong with my installation, or this looks like a rather nasty bug.

hmmm...wonder if the boolean indexer is somehow getting interpreted as a position indexer (e.g. 0's and 1's)....

the other thing is that there is caching going on (e.g. when you type df['test'] it first checks the cache). it is possible that the result you are getting is a cache it (when it should have been invalidated).....

in fact the result changes after some time. so it looks like the correct value is written to some cache, but is then not applied to the original variable.

ok...will mark as a bug..thanks


Resolving patch:
diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py
--- a/pandas/core/indexing.py
+++ b/pandas/core/indexing.py
@@ -124,8 +124,7 @@ def setter(item, v):
                 values = data.values
                 if np.prod(values.shape):
                     result, changed = com._maybe_upcast_indexer(values,plane_indexer,v,dtype=getattr(data,'dtype',None))
-                    if changed:
-                        self.obj[item] = result
+                    self.obj[item] = result
 
             labels = item_labels[het_idx]
 


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
@@ -784,6 +784,21 @@ def test_dups_fancy_indexing(self):
 
         assert_frame_equal(df,result)
 
+    def test_indexing_mixed_frame_bug(self):
+
+        # GH3492
+        df=DataFrame({'a':{1:'aaa',2:'bbb',3:'ccc'},'b':{1:111,2:222,3:333}})
+
+        # this works, new column is created correctly
+        df['test']=df['a'].apply(lambda x: '_' if x=='aaa' else x)
+
+        # this does not work, ie column test is not changed
+        idx=df['test']=='_'
+        temp=df.ix[idx,'a'].apply(lambda x: '-----' if x=='aaa' else x)
+        df.ix[idx,'test']=temp
+        self.assert_(df.iloc[0,2] == '-----')
+
+        #if I look at df, then element [0,2] equals '_'. If instead I type df.ix[idx,'test'], I get '-----', finally by typing df.iloc[0,2] I get '_'.
 
 if __name__ == '__main__':
     import nose