Skip to content

.ix[] inconsistently assigns right hand side #4508

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
etyurin opened this issue Aug 7, 2013 · 5 comments · Fixed by #4509
Closed

.ix[] inconsistently assigns right hand side #4508

etyurin opened this issue Aug 7, 2013 · 5 comments · Fixed by #4509

Comments

@etyurin
Copy link

etyurin commented Aug 7, 2013

This is with pandas 0.12.0.dev-be25266:

In [1]: import pandas as pd

In [2]: df = pd.DataFrame({'a':[1,2,3],'b':[0,1,2]})

In [3]: df.ix[[0,2,],'b'] = [100,-100] # Works as intended

In [4]: print df
a b
0 1 100
1 2 1
2 3 -100

In [6]: df = pd.DataFrame({'a':range(4)})

In [7]: df['b']=pd.np.nan

In [8]: df.ix[[1,3],'b'] = [100,-100] # Both rows get assigned 100

In [9]: print df
a b
0 0 NaN
1 1 100
2 2 NaN
3 3 100

In [10]: df['b'].ix[[1,3]] = [100,-100] # Works as intended

In [11]: print df
a b
0 0 NaN
1 1 100
2 2 NaN
3 3 -100

@jreback
Copy link
Contributor

jreback commented Aug 7, 2013

you have a list on the rhs, so it MUST be assigned by position, you are MUCH better off using a series here (which exactly specify what the values align to)

I'll look and see if there is a bug in any event

@jreback
Copy link
Contributor

jreback commented Aug 7, 2013

This is from current master (FYI I did push a change about an hour ago, but had to do with Panel alignment)

Looks ok to me

In [45]: df = pd.DataFrame({'a':[1,2,3],'b':[0,1,2]})

In [46]: df['b']=pd.np.nan

In [47]: df
Out[47]: 
   a   b
0  1 NaN
1  2 NaN
2  3 NaN

In [48]: df.ix[[0,2,],'b'] = [100,-100]

In [49]: df
Out[49]: 
   a    b
0  1  100
1  2  NaN
2  3 -100

In [50]: df = pd.DataFrame({'a':range(4)})

In [51]: df['b']=pd.np.nan

In [52]: df
Out[52]: 
   a   b
0  0 NaN
1  1 NaN
2  2 NaN
3  3 NaN

In [53]: df.ix[[1,3],'b'] = [100,-100]

In [54]: df
Out[54]: 
   a    b
0  0  NaN
1  1  100
2  2  NaN
3  3 -100

In [55]: pd.__version__
Out[55]: '0.12.0-141-g2e09fcc'

Also your very last assignment
df['b'].ix[[1,3]] = [100,-100]

You should NEVER do this; it may accidently work, but its not a guarantee (and its very hard to warn aboutt his); this is a chained assignment

@etyurin
Copy link
Author

etyurin commented Aug 7, 2013

Thank you very much - I guess it was a version-specific issue and it's best to update to the latest.

I started doing "chained assignment" when I noticed the simple/combined assignment was not working properly. In my ignorance I actually thought it'd be more straightforward.

@jreback
Copy link
Contributor

jreback commented Aug 7, 2013

np

the chained assignment is a very touchy issue...everyone wants it to work, but python basically doesn't let you do in a nice clean way (well it doesn, but it doesn't let you have control of it!)

and its insidous because it can work (I may have completely fixed it, but not 100% sure, so that we dont' official allow it) - and neither does numpy....

@jreback
Copy link
Contributor

jreback commented Aug 7, 2013

closing as already in master (tests are in #4509)

@jreback jreback closed this as completed Aug 7, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants