Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
topper-123 committed Apr 17, 2020
1 parent dd72723 commit 4dff9c3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ Other API changes
Backwards incompatible API changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- :meth:`DataFrame.swaplevels` now raises a ``TypeError`` if the axis is not a :class:`MultiIndex`.
Previously a ``AttributeError`` was raised (:issue:`31126`)
Previously an ``AttributeError`` was raised (:issue:`31126`)
- :meth:`DataFrame.xs` now raises a ``TypeError`` if a ``level`` keyword is supplied and the axis is not a :class:`MultiIndex`.
Previously an ``AttributeError`` was raised (:issue:`33610`)
- :meth:`DataFrameGroupby.mean` and :meth:`SeriesGroupby.mean` (and similarly for :meth:`~DataFrameGroupby.median`, :meth:`~DataFrameGroupby.std` and :meth:`~DataFrameGroupby.var`)
now raise a ``TypeError`` if a not-accepted keyword argument is passed into it.
Previously a ``UnsupportedFunctionCall`` was raised (``AssertionError`` if ``min_count`` passed into :meth:`~DataFrameGroupby.median`) (:issue:`31485`)
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,8 @@ def swapaxes(self: FrameOrSeries, axis1, axis2, copy=True) -> FrameOrSeries:
if copy:
new_values = new_values.copy()

# ignore needed because of NDFrame constructor is different than
# DataFrame/Series constructors.
return self._constructor(new_values, *new_axes).__finalize__( # type: ignore
self, method="swapaxes"
)
Expand Down Expand Up @@ -3490,7 +3492,8 @@ class animal locomotion
axis = self._get_axis_number(axis)
labels = self._get_axis(axis)
if level is not None:
assert isinstance(labels, MultiIndex), type(labels)
if not isinstance(labels, MultiIndex):
raise TypeError("Index must be a MultiIndex")
loc, new_ax = labels.get_loc_level(key, level=level, drop_level=drop_level)

# create the tuple of the indexer
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/indexing/multiindex/test_xs.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,15 @@ def test_series_getitem_multiindex_xs_by_label():

result = s.xs("one", level="L2")
tm.assert_series_equal(result, expected)


def test_xs_levels_raises():
df = DataFrame({"A": [1, 2, 3]})

msg = "Index must be a MultiIndex"
with pytest.raises(TypeError, match=msg):
df.xs(0, level="as")

s = df.A
with pytest.raises(TypeError, match=msg):
s.xs(0, level="as")

0 comments on commit 4dff9c3

Please sign in to comment.