Skip to content

Commit 339ef39

Browse files
GH1366 Relax index setter to align with columns (#1367)
1 parent cfed148 commit 339ef39

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

pandas-stubs/core/frame.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1760,7 +1760,9 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
17601760
@property
17611761
def index(self) -> Index: ...
17621762
@index.setter
1763-
def index(self, idx: Index) -> None: ...
1763+
def index(
1764+
self, idx: AnyArrayLike | SequenceNotStr[Hashable] | tuple[Hashable, ...]
1765+
) -> None: ...
17641766
@property
17651767
def loc(self) -> _LocIndexerFrame[Self]: ...
17661768
@property

pandas-stubs/core/series.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,9 @@ class Series(IndexOpsMixin[S1], NDFrame):
507507
@property
508508
def index(self) -> Index: ...
509509
@index.setter
510-
def index(self, idx: Index) -> None: ...
510+
def index(
511+
self, idx: AnyArrayLike | SequenceNotStr[Hashable] | tuple[Hashable, ...]
512+
) -> None: ...
511513
@overload
512514
def reset_index(
513515
self,

tests/series/test_series.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3982,3 +3982,12 @@ def test_series_explode() -> None:
39823982

39833983
check(assert_type(s.explode(), pd.Series), pd.Series)
39843984
check(assert_type(s.explode(ignore_index=True), pd.Series), pd.Series)
3985+
3986+
3987+
def test_series_index_setter() -> None:
3988+
"""Test Series.index setter property GH1366."""
3989+
sr = pd.Series(["a", "b"])
3990+
3991+
check(assert_type(sr.index, pd.Index), pd.Index)
3992+
sr.index = [2, 3]
3993+
check(assert_type(sr.index, pd.Index), pd.Index)

tests/test_frame.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4813,3 +4813,12 @@ def test_from_records() -> None:
48134813
),
48144814
pd.DataFrame,
48154815
)
4816+
4817+
4818+
def test_frame_index_setter() -> None:
4819+
"""Test DataFrame.index setter property GH1366."""
4820+
df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]})
4821+
4822+
check(assert_type(df.index, pd.Index), pd.Index)
4823+
df.index = [2, 3]
4824+
check(assert_type(df.index, pd.Index), pd.Index)

0 commit comments

Comments
 (0)