Skip to content

Documentation/behaviour error for selecting by label #7999

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
ruidc opened this issue Aug 12, 2014 · 9 comments · Fixed by #8003
Closed

Documentation/behaviour error for selecting by label #7999

ruidc opened this issue Aug 12, 2014 · 9 comments · Fixed by #8003
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@ruidc
Copy link
Contributor

ruidc commented Aug 12, 2014

df = pandas.DataFrame([['a'], ['b']], index=[1,2], columns=['x'])
df.loc[[3]] # does not raise KeyError as documented

"ALL of the labels for which you ask, must be in the index or a KeyError will be raised!"
http://pandas.pydata.org/pandas-docs/stable/indexing.html#selection-by-label
and
".loc is strictly label based, will raise KeyError when the items are not found":
http://pandas.pydata.org/pandas-docs/stable/indexing.html#different-choices-for-indexing-loc-iloc-and-ix

also, possibly separate bug:

df.loc[pandas.Index(['a', 'k', 'b']), : ]

DOES fail, so we have an inconsistency between .loc[values,:] and .loc[values]

@jreback jreback added this to the 0.15.0 milestone Aug 12, 2014
@jreback
Copy link
Contributor

jreback commented Aug 12, 2014

yeh, let me see if I can fix this inconsistency. indexing code has so many paths.

@jreback
Copy link
Contributor

jreback commented Aug 12, 2014

@ruidc ok, this cleaned up the indexing a bit (well a little).

pls have a look over the PR, the release notes should explain.

@ruidc
Copy link
Contributor Author

ruidc commented Aug 12, 2014

if i understand correctly,

The rule is that at least 1 indexer must be found when using a list-like and .loc
so if I looked for 2 labels, one that was present and one that is not, it would not raise?


From: jreback [email protected]
To: pydata/pandas [email protected]
Cc: ruidc [email protected]
Sent: Tuesday, 12 August 2014, 17:23
Subject: Re: [pandas] Documentation/behaviour error for selecting by label (#7999)

@ruidc ok, this cleaned up the indexing a bit (well a little).
pls have a look over the PR, the release notes should explain.

Reply to this email directly or view it on GitHub.

@jreback
Copy link
Contributor

jreback commented Aug 12, 2014

@ruidc right, you would get a reindex (with the missing) set to np.nan. In realitiy nothing much has changed, fyi, just a slight inconsistency

This is unchanged from 0.14.1 (and from the .ix always worked). It actually is pretty useful
as say you have a big list of labels all their except for 1.

In [1]: df = DataFrame([['a'],['b']],index=[1,2])

In [2]: df.loc[[1,4]]
Out[2]: 
     0
1    a
4  NaN

you can of course

In [3]: df.loc[df.index.isin([1,4])]
Out[3]: 
   0
1  a

@ruidc
Copy link
Contributor Author

ruidc commented Aug 12, 2014

Sure it's primarily that it is not as presently documented

.

@jreback
Copy link
Contributor

jreback commented Aug 12, 2014

@ruidc ahh, can you post what you think should be then?

@ruidc
Copy link
Contributor Author

ruidc commented Aug 12, 2014

"at least one of the labels for which you ask must be in the index..."
instead of

"ALL of the labels for which you ask, must be in the index..."

if that is the correct behavior.


From: jreback [email protected]
To: pydata/pandas [email protected]
Cc: ruidc [email protected]
Sent: Tuesday, 12 August 2014, 19:29
Subject: Re: [pandas] Documentation/behaviour error for selecting by label (#7999)

@ruidc ahh, can you post what you think should be then?

Reply to this email directly or view it on GitHub.

@jreback
Copy link
Contributor

jreback commented Aug 12, 2014

we just had a conv about that, see #8003

@jorisvandenbossche
Copy link
Member

@ruidc yes, @jreback, already made a comment about it in the whatsnew in the PR, but as you noted here, the docs should also be updated (as they are wrong now)

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
Projects
None yet
3 participants