Skip to content

Commit 9d121a0

Browse files
GH1299 Allow for Index and Series in the second argument of DataFrame.loc (#1310)
* GH1299 Allow for Index and Series in the second argument of DataFrame.loc * GH1299 Formatting
1 parent 1168ec7 commit 9d121a0

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

pandas-stubs/core/frame.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class _LocIndexerFrame(_LocIndexer, Generic[_T]):
224224
| slice
225225
| _IndexSliceTuple
226226
| Callable,
227-
MaskType | list[HashableT] | slice | Callable,
227+
MaskType | list[HashableT] | IndexType | Callable,
228228
]
229229
),
230230
) -> _T: ...

tests/test_frame.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3737,13 +3737,25 @@ def test_xs_key() -> None:
37373737

37383738

37393739
def test_loc_slice() -> None:
3740-
# GH 277
3740+
"""Test DataFrame.loc with a slice, Index, Series."""
3741+
# GH277
37413742
df1 = pd.DataFrame(
37423743
{"x": [1, 2, 3, 4]},
37433744
index=pd.MultiIndex.from_product([[1, 2], ["a", "b"]], names=["num", "let"]),
37443745
)
37453746
check(assert_type(df1.loc[1, :], Union[pd.Series, pd.DataFrame]), pd.DataFrame)
37463747

3748+
# GH1299
3749+
ind = pd.Index(["a", "b"])
3750+
mask = pd.Series([True, False])
3751+
mask_col = pd.Series([True, False], index=pd.Index(["a", "b"]))
3752+
df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
3753+
3754+
# loc with index for columns
3755+
check(assert_type(df.loc[mask, ind], pd.DataFrame), pd.DataFrame)
3756+
# loc with index for columns
3757+
check(assert_type(df.loc[mask, mask_col], pd.DataFrame), pd.DataFrame)
3758+
37473759

37483760
def test_where() -> None:
37493761
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})

0 commit comments

Comments
 (0)