Skip to content

support Ellipsis (...) in pandas indexing #10956

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
shoyer opened this issue Aug 31, 2015 · 5 comments · Fixed by #37750
Closed

support Ellipsis (...) in pandas indexing #10956

shoyer opened this issue Aug 31, 2015 · 5 comments · Fixed by #37750
Labels
Enhancement Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@shoyer
Copy link
Member

shoyer commented Aug 31, 2015

Indexing like x[..., 0] is a convenient way to indexing the last dimension of an array with NumPy. ... gets turned into an Ellipsis object, which NumPy expands to the appropriate number of full slice objects.

It would be nice to support this in pandas, too. Currently, attempting to do this throws KeyError:

>>> df[...]
KeyError: Ellipsis
@shoyer shoyer added Indexing Related to indexing on series/frames, not to indexes themselves Difficulty Intermediate labels Aug 31, 2015
@jorisvandenbossche
Copy link
Member

Can you give some usecases?

Because for direct getitem indexing on a dataframe, with a single item it already does index in the last dimension, so df[..., 'col'] doesn't seem very useful.

I could imagine it being useful in multi-indexing, eg with multiple levels of row indices, being able to easily select in the last level: df.loc[pd.IndexSlice[..., 'label'], :]

@shoyer
Copy link
Member Author

shoyer commented Sep 1, 2015

The most compelling use case for me is with .iloc and .loc, e.g., df.iloc[..., 0], for users who are writing indexing code that they want to work on data structures of any dimensionality.

I agree this is not so common/useful with pandas itself but it would be nice for cross-compatibility with NumPy and xray.

sgratzl added a commit to phovea/phovea_server that referenced this issue Mar 2, 2017
@flipdazed
Copy link

A workaround is to reset the index and reshuffle the order such that the relevant indices are first in the order

@JoElfner
Copy link
Contributor

JoElfner commented May 29, 2020

Will #33616 enable ellipsis indexing also for .loc?
My current workaround is:

df.loc[:, (*(df.columns.nlevels - 1) * [slice(None)], 'key')]

I know that this is a really cumbersome workaround, but perhaps it will help someone until the current v1.1 ETA. :)

Where I need it: Inplace operations such as *= or overwriting specific values with nan, when applying these operations only to a subset of columns in a MultiIndex DataFrame. Of course I can select these subsets with f.i. df.xs('key', axis=1, level=mysubsetlevel), but assigning values like this is not possible. Is there also a subtle way to do this for assigning values?

@jreback
Copy link
Contributor

jreback commented May 29, 2020

@JoElfner this is an open issue; if you want it fixed then a PR would do it

@jreback jreback added this to the 1.2 milestone Nov 14, 2020
@jorisvandenbossche jorisvandenbossche modified the milestones: 1.2, Contributions Welcome Nov 16, 2020
@jreback jreback modified the milestones: Contributions Welcome, 1.4 Oct 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants