Skip to content

Commit ddd6d7c

Browse files
author
pilkibun
committed
Follow DataFrame.set_index very closely
1 parent 9a48c58 commit ddd6d7c

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

pandas/core/series.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -1472,33 +1472,34 @@ def set_index(self, labels, append=False, inplace=False, verify_integrity=False)
14721472
labels = [labels]
14731473

14741474
err_msg = (
1475-
'The parameter "labels" may be an Index or a list-like '
1476-
"and it must be of the same length is the calling Series."
1475+
'The parameter "labels" may be a column key, one-dimensional '
1476+
"array, or a list containing only "
1477+
"one-dimensional arrays."
14771478
)
14781479

1479-
if isinstance(
1480-
labels, (ABCIndexClass, ABCSeries, np.ndarray, list, abc.Iterator)
1481-
):
1482-
# arrays are fine as long as they are one-dimensional
1483-
# iterators get converted to list below
1484-
if getattr(labels, "ndim", 1) != 1:
1485-
raise ValueError(err_msg)
1480+
for col in labels:
1481+
if isinstance(
1482+
col, (ABCIndexClass, ABCSeries, np.ndarray, list, abc.Iterator)
1483+
):
1484+
# arrays are fine as long as they are one-dimensional
1485+
# iterators get converted to list below
1486+
if getattr(col, "ndim", 1) != 1:
1487+
raise ValueError(err_msg)
14861488

14871489
if inplace:
14881490
ser = self
14891491
else:
14901492
ser = self.copy()
14911493

14921494
arrays = []
1495+
names = []
14931496
if append:
14941497
names = [x for x in self.index.names]
14951498
if isinstance(self.index, ABCMultiIndex):
14961499
for i in range(self.index.nlevels):
14971500
arrays.append(self.index._get_level_values(i))
14981501
else:
14991502
arrays.append(self.index)
1500-
else:
1501-
names = []
15021503

15031504
for col in labels:
15041505
if isinstance(col, ABCMultiIndex):
@@ -1509,18 +1510,18 @@ def set_index(self, labels, append=False, inplace=False, verify_integrity=False)
15091510
# if Index then not MultiIndex (treated above)
15101511
arrays.append(col)
15111512
names.append(col.name)
1512-
elif isinstance(col, (np.ndarray,)):
1513+
elif isinstance(col, (list, np.ndarray)):
15131514
arrays.append(col)
15141515
names.append(None)
15151516
elif isinstance(col, abc.Iterator):
15161517
arrays.append(list(col))
15171518
names.append(None)
1519+
# from here, col can only be a column label
15181520
else:
1519-
msg = "Passing type '{typ}' in labels is not allowed"
1520-
raise ValueError(msg.format(typ=type(col)))
1521+
raise ValueError("MultiIndex Levels must be array-like")
15211522

15221523
if len(arrays[-1]) != len(self):
1523-
# check newest element against length of calling series, since
1524+
# check newest element against length of calling ser, since
15241525
# ensure_index_from_sequences would not raise for append=False.
15251526
raise ValueError(
15261527
"Length mismatch: Expected {len_self} rows, "

pandas/tests/series/test_api.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ def test_set_index(self, arrayklass):
803803
tm.assert_series_equal(res, exp)
804804

805805
def test_set_index_raises(self):
806-
ser = pd.Series([[0], [1], [2]]) # not allowed
806+
ser = pd.Series([0, 1, 2])
807807

808-
with pytest.raises(ValueError, match="not allowed"):
809-
ser.set_index([["a"]])
808+
with pytest.raises(ValueError, match="must be array"):
809+
ser.set_index(["A", "B", "C"])

0 commit comments

Comments
 (0)