pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.24k stars 17.79k forks source link

Tuple-Retrieving vs. -Setting on non-MultiIndex DataFrame #10175

Closed fabboe closed 6 hours ago

fabboe commented 9 years ago
df = pd.DataFrame({'a':[55,66,77],'b':[100,111,112],'ch':['x','y','z']})
df.index = pd.Index([10,11,12])

In [6]: df
Out[6]: 
     a    b ch
10  55  100  x
11  66  111  y
12  77  112  z

In [7]: df.loc[10,('b','ch')]
....
ValueError: No axis named 1 for object type <class 'pandas.core.series.Series'>

In [8]: df.ix[10,('b','ch')]
Out[8]: 100

more or less understandable, probably both should not work though.

but then, using the same indexing, the setting operation works, unpacking the tuple into the correct columns, as one would use the correct list selector df.ix[10,['b','ch']] or df.loc[10,['b','ch']]

In [10]: df.ix[10,('b','ch')] = (200,'i')

In [11]: df
Out[11]: 
     a    b ch
10  55  200  i
11  66  111  y
12  77  112  z

In [12]: df.loc[10,('b','ch')] = (300,'j')

In [13]: df
Out[13]: 
     a    b ch
10  55  300  j
11  66  111  y
12  77  112  z

For .ix setting should throw the same error as for retrieving? For .loc setting should throw an error as you should expect to set a tuple to a single column?

Is this wanted behavior?

P.S.: New to advanced / multi-index slicing, my searches didn't bring up that very issue despite the fact that all issues around this indexing seem somehow related.

jreback commented 9 years ago

can u post your sample frame construction at the top of the issue (so it's easily copy-past able)

jreback commented 9 years ago

@fabboe yeh all of these should not be allowed. However, the detection code is a bit tricky. As it will try to figure out if df.ix[x,y] actually means df.ix[(x,y)] (in the general case). IOW, if someone really means a tuple and just didn't pass it way (its a bit TOO flexible IMHO), but this is a bit tricky to change.

These would need some tests in tests/test_indexing.py

mroeschke commented 6 hours ago

ix has been removed in the meantime so I'm not sure if there any alignment issues here anymore so closing