Skip to content

Commit 092e14b

Browse files
GH1299 Allow for Index and Series in the second argument of DataFrame.loc
1 parent 1168ec7 commit 092e14b

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-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: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ def test_types_loc_at() -> None:
281281
df.loc[0, "col1"]
282282

283283

284+
284285
def test_types_boolean_indexing() -> None:
285286
df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]})
286287
check(assert_type(df[df > 1], pd.DataFrame), pd.DataFrame)
@@ -3737,13 +3738,26 @@ def test_xs_key() -> None:
37373738

37383739

37393740
def test_loc_slice() -> None:
3740-
# GH 277
3741+
"""Test DataFrame.loc with a slice, Index, Series."""
3742+
# GH277
37413743
df1 = pd.DataFrame(
37423744
{"x": [1, 2, 3, 4]},
37433745
index=pd.MultiIndex.from_product([[1, 2], ["a", "b"]], names=["num", "let"]),
37443746
)
37453747
check(assert_type(df1.loc[1, :], Union[pd.Series, pd.DataFrame]), pd.DataFrame)
37463748

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

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

0 commit comments

Comments
 (0)