Skip to content

COMPAT: restore shape for 'invalid' Index with nd array #27818

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Plotting
^^^^^^^^

- Added a pandas_plotting_backends entrypoint group for registering plot backends. See :ref:`extending.plotting-backends` for more (:issue:`26747`).
-
- Fix compatibility issue with matplotlib when passing a pandas ``Index`` to a plot call (:issue:`27775`).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that this is vague about the underlying issue. I don't want people relying on Index.shape being n-d.

-

Groupby/resample/rolling
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5600,7 +5600,10 @@ def shape(self):
"""
Return a tuple of the shape of the underlying data.
"""
return (len(self),)
# not using "(len(self), )" to return "correct" shape if the values
# consists of a >1 D array (see GH-27775)
# overridden in MultiIndex.shape to avoid materializing the values
return self._values.shape


Index._add_numeric_methods_disabled()
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,15 @@ def _values(self):
# We override here, since our parent uses _data, which we dont' use.
return self.values

@property
def shape(self):
"""
Return a tuple of the shape of the underlying data.
"""
# overriding the base Index.shape definition to avoid materializing
# the values (GH-27384, GH-27775)
return (len(self),)

@property
def array(self):
"""
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2805,3 +2805,17 @@ def test_deprecated_fastpath():

expected = pd.CategoricalIndex(["a", "b", "c"], name="test")
tm.assert_index_equal(idx, expected)


def test_shape_of_invalid_index():
# Currently, it is possible to create "invalid" index objects backed by
# a multi-dimensional array (see https://github.com/pandas-dev/pandas/issues/27125
# about this). However, as long as this is not solved in general,this test ensures
# that the returned shape is consistent with this underlying array for
# compat with matplotlib (see https://github.com/pandas-dev/pandas/issues/27775)
a = np.arange(8).reshape(2, 2, 2)
idx = pd.Index(a)
assert idx.shape == a.shape

idx = pd.Index([0, 1, 2, 3])
assert idx[:, None].shape == (4, 1)