Skip to content

DOC: Basic indexing on axis with Multiindex #39683

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

Open
sroener opened this issue Feb 8, 2021 · 1 comment
Open

DOC: Basic indexing on axis with Multiindex #39683

sroener opened this issue Feb 8, 2021 · 1 comment
Labels
Docs Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex

Comments

@sroener
Copy link

sroener commented Feb 8, 2021

Location of the documentation

Basic indexing on axis with MultiIndex

Documentation problem

The following example seems not to work:

arrays = [
         np.array(["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"]),
         np.array(["one", "two", "one", "two", "one", "two", "one", "two"]),
     ]
    
df = pd.DataFrame(np.random.randn(8, 4), index=arrays)

df["bar"]

The code throws an error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-13-6ea2f6413a49> in <module>
----> 1 df["bar"]

~/anaconda3/envs/jupyter/lib/python3.8/site-packages/pandas/core/frame.py in __getitem__(self, key)
   3022             if self.columns.nlevels > 1:
   3023                 return self._getitem_multilevel(key)
-> 3024             indexer = self.columns.get_loc(key)
   3025             if is_integer(indexer):
   3026                 indexer = [indexer]

~/anaconda3/envs/jupyter/lib/python3.8/site-packages/pandas/core/indexes/range.py in get_loc(self, key, method, tolerance)
    352                 except ValueError as err:
    353                     raise KeyError(key) from err
--> 354             raise KeyError(key)
    355         return super().get_loc(key, method=method, tolerance=tolerance)
    356 

KeyError: 'bar'

When asking for help, it is suggested to use df.loc["bar"] instead with as shown in another part of the documentation (advanced indexing with hierarchical index).

Suggested fix for documentation

An explanation for when partial indexing without the usage of .loc[] is applicable would be helpful or choosing an example that works in the current version. Trying to reproduce documentation code that does not work leads to confusion.

@sroener sroener added Docs Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 8, 2021
@attack68
Copy link
Contributor

linking to #39775, this is about a different issue but I agree behaviour should be clearer.

@jbrockmendel jbrockmendel added MultiIndex Indexing Related to indexing on series/frames, not to indexes themselves and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Projects
None yet
Development

No branches or pull requests

3 participants