Skip to content

inconsistent indexing behaviour with multi index #5725

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
behzadnouri opened this issue Dec 18, 2013 · 7 comments · Fixed by #5730
Closed

inconsistent indexing behaviour with multi index #5725

behzadnouri opened this issue Dec 18, 2013 · 7 comments · Fixed by #5730
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Milestone

Comments

@behzadnouri
Copy link
Contributor

i have a dataframe with multi-index as

>>> df
              val  diffs
tag day                 
A   26   0.208105    NaN
    37   2.709899    NaN
    57   0.933981    NaN
B   75   0.624029    NaN
C   0   -1.645489    NaN
    27  -1.749618    NaN
    27  -0.725497    NaN
    37  -0.381485    NaN
    67   0.110426    NaN
    82   1.253857    NaN

[10 rows x 2 columns]

however

>>> df.val[ 'A' ]
A   NaN
Name: val, dtype: float64
>>> df.val[ 'X' ]
X   NaN
Name: val, dtype: float64



>>> pd.__version__
'0.13.0rc1-92-gf6fd509'
@jreback
Copy link
Contributor

jreback commented Dec 18, 2013

believe it or not, this is ony under PY3, when you happend to use 'A' as a column label (because that is a valid Timestamp....wierd but true)

@jreback
Copy link
Contributor

jreback commented Dec 18, 2013

pls update to master and give a try..thanks for the report

@behzadnouri
Copy link
Contributor Author

this works:

>>> pd.__version__
'0.13.0rc1-99-gbac7c6e'
>>> df.val[ 'A' ]
day
26     0.208105
37     2.709899
57     0.933981
Name: val, dtype: float64

but i do not understand below, and why it does not throw KeyError. there is no X in df:

>>> df.val[ 'X' ]
X   NaN
Name: val, dtype: float64

@jreback
Copy link
Contributor

jreback commented Dec 19, 2013

PY3 does such weird things with exceptions. I think they were trying to make it better, but just is more confusing. #5737 should fix this. This seems very simple, except that since pandas allows indexing with all kinds of things, several things needs to be tried in order to index properly, e.g. imagine that the first level of the index was not 'A','B','C' but a dates, then s['2011'] should work! (which is IMHO somewhat confusing, but a convenience)

@jreback
Copy link
Contributor

jreback commented Dec 19, 2013

ok...master is update....give it a go

@jreback
Copy link
Contributor

jreback commented Dec 19, 2013

You might find these other access methods useful

This is the recommended actually

In [7]: df.loc['D','val']
Out[7]: 
day
26    -0.269229
37    -0.330613
57    -0.654839
Name: val, dtype: float64

Specify a tuple for each element you want (could be a list of tuples too)

In [8]: df.loc[('D',57),'val']
Out[8]: 
tag  day
D    57    -0.654839
Name: val, dtype: float64

Cross sectional

In [9]: df.xs(57,level=1)
Out[9]: 
          val
tag          
D   -0.654839

[1 rows x 1 columns]

@behzadnouri
Copy link
Contributor Author

confirmed it raises KeyError,

thanks for quick fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants