Skip to content

Commit 30d3386

Browse files
Dr-Irvtwoertwein
authored andcommitted
allow multiindex for a column in DataFrame.loc (pandas-dev#494)
* split Series[IntervalT] into separate __new__ * allow multiindex for a colun in DataFrame.loc * revert changes related to Series.__new__ and IntervalT * change tuple[ScalarT,...] to tuple[Scalar, ...]
1 parent ced4b00 commit 30d3386

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

pandas-stubs/core/frame.pyi

+3-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ class _LocIndexerFrame(_LocIndexer):
166166
@overload
167167
def __getitem__(
168168
self,
169-
idx: tuple[int | StrLike | tuple[ScalarT, ...], int | StrLike],
169+
idx: tuple[
170+
int | StrLike | tuple[Scalar, ...], int | StrLike | tuple[Scalar, ...]
171+
],
170172
) -> Scalar: ...
171173
@overload
172174
def __getitem__(

pandas-stubs/core/indexing.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ from pandas._typing import (
1515
)
1616

1717
_IndexSliceTuple: TypeAlias = tuple[
18-
Union[Index, MaskType, Scalar, list[ScalarT], slice], ...
18+
Union[Index, MaskType, Scalar, list[ScalarT], slice | tuple[Scalar, ...]], ...
1919
]
2020

2121
_IndexSliceUnion: TypeAlias = Union[slice, _IndexSliceTuple]

tests/test_frame.py

+13
Original file line numberDiff line numberDiff line change
@@ -2319,3 +2319,16 @@ def test_getattr_and_dataframe_groupby() -> None:
23192319
assert_type(df.groupby("col1").col3.agg([min, max]), pd.DataFrame),
23202320
pd.DataFrame,
23212321
)
2322+
2323+
2324+
def test_getsetitem_multiindex() -> None:
2325+
# GH 466
2326+
rows = pd.Index(["project A", "project B", "project C"])
2327+
years: tuple[str, ...] = ("Year 1", "Year 2", "Year 3")
2328+
quarters: tuple[str, ...] = ("Q1", "Q2", "Q3", "Q4")
2329+
index_tuples: list[tuple[str, ...]] = list(itertools.product(years, quarters))
2330+
cols = pd.MultiIndex.from_tuples(index_tuples)
2331+
budget = pd.DataFrame(index=rows, columns=cols)
2332+
multi_index: tuple[str, str] = ("Year 1", "Q1")
2333+
budget.loc["project A", multi_index] = 4700
2334+
check(assert_type(budget.loc["project A", multi_index], Scalar), int)

0 commit comments

Comments
 (0)