8000 ENH: Add Series.set_index · Pull Request #27504 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add Series.set_index #27504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Follow DataFrame.set_index very closely
  • Loading branch information
pilkibun committed Jul 25, 2019
commit ddd6d7c6b1d14f939397e0920f8cc7133a4eddea
31 changes: 16 additions & 15 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1472,33 +1472,34 @@ def set_index(self, labels, append=False, inplace=False, verify_integrity=False)
labels = [labels]

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

if isinstance(
labels, (ABCIndexClass, ABCSeries, np.ndarray, list, abc.Iterator)
):
# arrays are fine as long as they are one-dimensional
# iterators get converted to list below
if getattr(labels, "ndim", 1) != 1:
raise ValueError(err_msg)
for col in labels:
if isinstance(
col, (ABCIndexClass, ABCSeries, np.ndarray, list, abc.Iterator)
):
# arrays are fine as long as they are one-dimensional
# iterators get converted to list below
if getattr(col, "ndim", 1) != 1:
raise ValueError(err_msg)

if inplace:
ser = self
else:
ser = self.copy()

arrays = []
names = []
if append:
names = [x for x in self.index.names]
if isinstance(self.index, ABCMultiIndex):
for i in range(self.index.nlevels):
arrays.append(self.index._get_level_values(i))
else:
arrays.append(self.index)
else:
names = []

for col in labels:
if isinstance(col, ABCMultiIndex):
Expand All @@ -1509,18 +1510,18 @@ def set_index(self, labels, append=False, inplace=False, verify_integrity=False)
# if Index then not MultiIndex (treated above)
arrays.append(col)
names.append(col.name)
elif isinstance(col, (np.ndarray,)):
elif isinstance(col, (list, np.ndarray)):
arrays.append(col)
names.append(None)
elif isinstance(col, abc.Iterator):
arrays.append(list(col))
names.append(None)
# from here, col can only be a column label
else:
msg = "Passing type '{typ}' in labels is not allowed"
raise ValueError(msg.format(typ=type(col)))
raise ValueError("MultiIndex Levels must be array-like")

if len(arrays[-1]) != len(self):
# check newest element against length of calling series, since
# check newest element against length of calling ser, since
# ensure_index_from_sequences would not raise for append=False.
raise ValueError(
"Length mismatch: Expected {len_self} rows, "
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ def test_set_index(self, arrayklass):
tm.assert_series_equal(res, exp)

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

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