Skip to content

Commit 27f334c

Browse files
committed
Fix return type of loc
1 parent 0e978b6 commit 27f334c

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,7 @@ Indexing
12171217
- Bug in :meth:`DataFrame.__setitem__` on an empty :class:`DataFrame` with a tuple corrupting the frame (:issue:`54385`)
12181218
- Bug in :meth:`DataFrame.from_records` throwing a ``ValueError`` when passed an empty list in ``index`` (:issue:`58594`)
12191219
- Bug in :meth:`DataFrame.loc` and :meth:`DataFrame.iloc` returning incorrect dtype when selecting from a :class:`DataFrame` with mixed data types. (:issue:`60600`)
1220+
- Bug in :meth:`DataFrame.loc` returning incorrect dtype when the key is ``slice`` (:issue:`63071`)
12201221
- Bug in :meth:`DataFrame.loc` with inconsistent behavior of loc-set with 2 given indexes to Series (:issue:`59933`)
12211222
- Bug in :meth:`Index.equals` when comparing between :class:`Series` with string dtype :class:`Index` (:issue:`61099`)
12221223
- Bug in :meth:`Index.get_indexer` and similar methods when ``NaN`` is located at or after position 128 (:issue:`58924`)

pandas/core/indexing.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,11 @@ def _getitem_lowerdim(self, tup: tuple):
10871087
# Reverse tuple so that we are indexing along columns before rows
10881088
# and avoid unintended dtype inference. # GH60600
10891089
for i, key in zip(range(len(tup) - 1, -1, -1), reversed(tup), strict=True):
1090-
if is_label_like(key) or is_list_like(key):
1090+
if (
1091+
is_label_like(key)
1092+
or is_list_like(key)
1093+
or (isinstance(key, slice) and need_slice(key))
1094+
):
10911095
# We don't need to check for tuples here because those are
10921096
# caught by the _is_nested_tuple_indexer check above.
10931097
section = self._getitem_axis(key, axis=i)

pandas/tests/indexing/test_loc.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ def test_not_change_nan_loc(series, new_series, expected_ser):
6161
tm.assert_frame_equal(df.notna(), ~expected)
6262

6363

64-
def test_loc_dtype():
64+
@pytest.mark.parametrize("key", [[1, 2], slice(1, 3)])
65+
def test_loc_dtype(key):
6566
# GH 60600
6667
df = DataFrame([["a", 1.0, 2.0], ["b", 3.0, 4.0]])
67-
result = df.loc[0, [1, 2]]
68+
result = df.loc[0, key]
6869
expected = Series([1.0, 2.0], index=[1, 2], dtype=float, name=0)
6970
tm.assert_series_equal(result, expected)
7071

0 commit comments

Comments
 (0)