Skip to content

Commit 0ebe650

Browse files
committed
changes ac. to comments
1 parent 0447667 commit 0ebe650

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
@@ -666,7 +666,8 @@ Other API changes
666666
- When testing pandas, the new minimum required version of pytest is 5.0.1 (:issue:`29664`)
667667
- :meth:`Series.str.__iter__` was deprecated and will be removed in future releases (:issue:`28277`).
668668
- Added ``<NA>`` to the list of default NA values for :meth:`read_csv` (:issue:`30821`)
669-
669+
- a ``TypeError`` is now raised if attempting to call :meth:`DataFrame.swaplevels` and the axis is not
670+
a :class:`MultiIndex`. Previously a ``AttributeError`` was raised (:issue:`31126`)
670671

671672
.. _whatsnew_100.api.documentation:
672673

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
@@ -5273,6 +5274,10 @@ def swaplevel(self, i=-2, j=-1, axis=0) -> "DataFrame":
52735274
result = self.copy()
52745275

52755276
axis = self._get_axis_number(axis)
5277+
5278+
if not isinstance(result._get_axis(axis), ABCMultiIndex): # pragma: no cover
5279+
raise TypeError("Can only swap levels on a hierarchical axis.")
5280+
52765281
if axis == 0:
52775282
assert isinstance(result.index, ABCMultiIndex)
52785283
result.index = result.index.swaplevel(i, j)
@@ -8481,11 +8486,9 @@ def isin(self, values) -> "DataFrame":
84818486
index: "Index" = properties.AxisProperty(
84828487
axis=1, doc="The index (row labels) of the DataFrame."
84838488
)
8484-
NDFrame._internal_names_set.add("index")
84858489
columns: "Index" = properties.AxisProperty(
84868490
axis=0, doc="The column labels of the DataFrame."
84878491
)
8488-
NDFrame._internal_names_set.add("columns")
84898492

84908493
# ----------------------------------------------------------------------
84918494
# 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
@@ -4499,7 +4500,6 @@ def to_period(self, freq=None, copy=True) -> "Series":
44994500
index: "Index" = properties.AxisProperty(
45004501
axis=0, doc="The index (axis labels) of the Series."
45014502
)
4502-
generic.NDFrame._internal_names_set.add("index")
45034503

45044504
# ----------------------------------------------------------------------
45054505
# 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)