Skip to content

Failed to assign new value to masked serie #5683

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
omar-masmoudi opened this issue Dec 12, 2013 · 1 comment
Closed

Failed to assign new value to masked serie #5683

omar-masmoudi opened this issue Dec 12, 2013 · 1 comment
Labels
Bug Duplicate Report Duplicate issue or pull request Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@omar-masmoudi
Copy link

Hello,

#!/bin/env python
import pandas as pd
import numpy as np
import sys
print sys.version
print "NUMPY:", np.__version__
print "PANDAS:", pd.__version__

df = pd.DataFrame([[0, None], [4, 3]], columns=['a','b'])
print df

df["c"] = 1.0
#print df #uncomment and result change

df["c"][~df.all(axis=1)] = 2
print df
print df.c

gives

2.7.2 (default, Jul 30 2012, 17:32:47)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)]
NUMPY: 1.7.1
PANDAS: 0.12.0
   a   b
0  0 NaN
1  4   3
   a   b  c
0  0 NaN  1
1  4   3  1
0    2
1    1
Name: c, dtype: float64

As you can see, I try to change value of the c columns to "2" for the element where one of the column is null and it does not work.

The serie is however modified.
What is even stranger is that adding an intermediate print (uncomment above) changes the behavior.

I was not able to find any duplicate of this issue.
Thanks in advance for having a look.

@jreback
Copy link
Contributor

jreback commented Dec 12, 2013

this is a duplicate/related #5216. their are several issues that address various aspects of this.
In any event, this is fixed in 0.13.

the proper method of setting is

df.loc[~df.all(1),'c'] = 2 which works fine in 0.12

doing df['c'][df.all(1)]` is a chained assignment and can work at times, but is dependent on whether you have a mixed dtype frame or not and what exactly you are updating hence lways better to use ''.loc''

see here for a more detailed discussion

@jreback jreback closed this as completed Dec 12, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

2 participants