Skip to content

Commit 92d6894

Browse files
committed
changes ac. to comments
1 parent 7388996 commit 92d6894

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

doc/source/whatsnew/v1.0.0.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,8 @@ Other API changes
686686
- When testing pandas, the new minimum required version of pytest is 5.0.1 (:issue:`29664`)
687687
- :meth:`Series.str.__iter__` was deprecated and will be removed in future releases (:issue:`28277`).
688688
- Added ``<NA>`` to the list of default NA values for :meth:`read_csv` (:issue:`30821`)
689-
689+
- a ``TypeError`` is now raised if attempting to call :meth:`DataFrame.swaplevels` and the axis is not
690+
a :class:`MultiIndex`. Previously a ``AttributeError`` was raised (:issue:`31126`)
690691

691692
.. _whatsnew_100.api.documentation:
692693

pandas/core/frame.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ class DataFrame(NDFrame):
396396
2 7 8 9
397397
"""
398398

399+
_internal_names_set = {"columns", "index"} | NDFrame._internal_names_set
399400
_typ = "dataframe"
400401

401402
@property
@@ -5292,6 +5293,10 @@ def swaplevel(self, i=-2, j=-1, axis=0) -> "DataFrame":
52925293
result = self.copy()
52935294

52945295
axis = self._get_axis_number(axis)
5296+
5297+
if not isinstance(result._get_axis(axis), ABCMultiIndex): # pragma: no cover
5298+
raise TypeError("Can only swap levels on a hierarchical axis.")
5299+
52955300
if axis == 0:
52965301
assert isinstance(result.index, ABCMultiIndex)
52975302
result.index = result.index.swaplevel(i, j)
@@ -8497,11 +8502,9 @@ def isin(self, values) -> "DataFrame":
84978502
index: "Index" = properties.AxisProperty(
84988503
axis=1, doc="The index (row labels) of the DataFrame."
84998504
)
8500-
NDFrame._internal_names_set.add("index")
85018505
columns: "Index" = properties.AxisProperty(
85028506
axis=0, doc="The column labels of the DataFrame."
85038507
)
8504-
NDFrame._internal_names_set.add("columns")
85058508

85068509
# ----------------------------------------------------------------------
85078510
# Add plotting methods to DataFrame

pandas/core/series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
178178

179179
_name: Optional[Hashable]
180180
_metadata: List[str] = ["name"]
181+
_internal_names_set = {"index"} | generic.NDFrame._internal_names_set
181182
_accessors = {"dt", "cat", "str", "sparse"}
182183
_deprecations = (
183184
base.IndexOpsMixin._deprecations
@@ -4486,7 +4487,6 @@ def to_period(self, freq=None, copy=True) -> "Series":
44864487
index: "Index" = properties.AxisProperty(
44874488
axis=0, doc="The index (axis labels) of the Series."
44884489
)
4489-
generic.NDFrame._internal_names_set.add("index")
44904490

44914491
# ----------------------------------------------------------------------
44924492
# Accessor Methods

pandas/tests/test_multilevel.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,10 @@ def test_swaplevel(self):
957957
exp = self.frame.swaplevel("first", "second").T
958958
tm.assert_frame_equal(swapped, exp)
959959

960+
msg = "Can only swap levels on a hierarchical axis."
961+
with pytest.raises(TypeError, match=msg):
962+
DataFrame(range(3)).swaplevel()
963+
960964
def test_reorder_levels(self):
961965
result = self.ymd.reorder_levels(["month", "day", "year"])
962966
expected = self.ymd.swaplevel(0, 1).swaplevel(1, 2)

0 commit comments

Comments
 (0)