Skip to content

Commit dffa527

Browse files
committed
Edit doc-string, test failure cases.
1 parent 4bccd28 commit dffa527

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

pandas/core/frame.py

+22
Original file line numberDiff line numberDiff line change
@@ -5639,6 +5639,12 @@ def reset_index(
56395639
col_fill : object, default ''
56405640
If the columns have multiple levels, determines how the other
56415641
levels are named. If None then the index name is repeated.
5642+
names : str, tuple or list, default None
5643+
Using the given string, rename the DataFrame column which contains the index data.
5644+
If the DataFrame has a MultiIndex, this has to be a list or tuple with length
5645+
equal to the number of levels.
5646+
5647+
.. versionadded:: 1.4.0
56425648
56435649
Returns
56445650
-------
@@ -5781,6 +5787,14 @@ class max type
57815787
if len(level) < self.index.nlevels:
57825788
new_index = self.index.droplevel(level)
57835789

5790+
if names is not None:
5791+
if isinstance(self.index, MultiIndex):
5792+
if not isinstance(names, (tuple, list)):
5793+
raise ValueError("Names must be a tuple or list")
5794+
else:
5795+
if not isinstance(names, str):
5796+
raise ValueError("Names must be a string")
5797+
57845798
if not drop:
57855799
to_insert: Iterable[tuple[Any, Any | None]]
57865800
if isinstance(self.index, MultiIndex):
@@ -5789,6 +5803,14 @@ class max type
57895803
(n if n is not None else f"level_{i}")
57905804
for i, n in enumerate(self.index.names)
57915805
]
5806+
else:
5807+
5808+
if len(names) != self.index.nlevels:
5809+
raise ValueError(
5810+
f"The number of provided names "
5811+
f"({len(names)}) does not match the number of"
5812+
f" MultiIndex levels ({self.index.nlevels})"
5813+
)
57925814
to_insert = zip(self.index.levels, self.index.codes)
57935815
else:
57945816
default = "index" if "index" not in self else "level_0"

pandas/tests/frame/methods/test_reset_index.py

+9
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ def test_reset_index_rename(self, float_frame):
189189
exp = Series(float_frame.index.values, name="new_name")
190190
tm.assert_series_equal(rdf["new_name"], exp)
191191

192+
with pytest.raises(ValueError, match="Names must be a string"):
193+
float_frame.reset_index(names=1)
194+
192195
def test_reset_index_rename_multiindex(self, float_frame):
193196
# GH 6878
194197
stacked = float_frame.stack()[::2]
@@ -205,6 +208,12 @@ def test_reset_index_rename_multiindex(self, float_frame):
205208
deleveled["second"], deleveled2["new_second"], check_names=False
206209
)
207210

211+
with pytest.raises(ValueError, match=r".* number of provided names .*"):
212+
stacked.reset_index(names=["new_first"])
213+
214+
with pytest.raises(ValueError, match="Names must be a tuple or list"):
215+
stacked.reset_index(names={"first": "new_first", "second": "new_second"})
216+
208217
def test_reset_index_level(self):
209218
df = DataFrame([[1, 2, 3, 4], [5, 6, 7, 8]], columns=["A", "B", "C", "D"])
210219

0 commit comments

Comments
 (0)