Skip to content

Commit aa7cb2a

Browse files
committed
Edit doc-string, test failure cases.
1 parent fbfd56d commit aa7cb2a

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
@@ -5614,6 +5614,12 @@ def reset_index(
56145614
col_fill : object, default ''
56155615
If the columns have multiple levels, determines how the other
56165616
levels are named. If None then the index name is repeated.
5617+
names : str, tuple or list, default None
5618+
Using the given string, rename the DataFrame column which contains the index data.
5619+
If the DataFrame has a MultiIndex, this has to be a list or tuple with length
5620+
equal to the number of levels.
5621+
5622+
.. versionadded:: 1.4.0
56175623
56185624
Returns
56195625
-------
@@ -5756,6 +5762,14 @@ class max type
57565762
if len(level) < self.index.nlevels:
57575763
new_index = self.index.droplevel(level)
57585764

5765+
if names is not None:
5766+
if isinstance(self.index, MultiIndex):
5767+
if not isinstance(names, (tuple, list)):
5768+
raise ValueError("Names must be a tuple or list")
5769+
else:
5770+
if not isinstance(names, str):
5771+
raise ValueError("Names must be a string")
5772+
57595773
if not drop:
57605774
to_insert: Iterable[tuple[Any, Any | None]]
57615775
if isinstance(self.index, MultiIndex):
@@ -5764,6 +5778,14 @@ class max type
57645778
(n if n is not None else f"level_{i}")
57655779
for i, n in enumerate(self.index.names)
57665780
]
5781+
else:
5782+
5783+
if len(names) != self.index.nlevels:
5784+
raise ValueError(
5785+
f"The number of provided names "
5786+
f"({len(names)}) does not match the number of"
5787+
f" MultiIndex levels ({self.index.nlevels})"
5788+
)
57675789
to_insert = zip(self.index.levels, self.index.codes)
57685790
else:
57695791
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)