Skip to content

Commit f47516d

Browse files
authored
BUG: Series.__getitem__ with downstream scalars (#32684)
1 parent 9f494da commit f47516d

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ Indexing
303303
- Bug in :meth:`Series.loc` and :meth:`DataFrame.loc` when indexing with an integer key on a object-dtype :class:`Index` that is not all-integers (:issue:`31905`)
304304
- Bug in :meth:`DataFrame.iloc.__setitem__` on a :class:`DataFrame` with duplicate columns incorrectly setting values for all matching columns (:issue:`15686`, :issue:`22036`)
305305
- Bug in :meth:`DataFrame.loc:` and :meth:`Series.loc` with a :class:`DatetimeIndex`, :class:`TimedeltaIndex`, or :class:`PeriodIndex` incorrectly allowing lookups of non-matching datetime-like dtypes (:issue:`32650`)
306+
- Bug in :meth:`Series.__getitem__` indexing with non-standard scalars, e.g. ``np.dtype`` (:issue:`32684`)
306307

307308
Missing
308309
^^^^^^^

pandas/core/series.py

+4
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,10 @@ def _get_with(self, key):
926926
elif isinstance(key, tuple):
927927
return self._get_values_tuple(key)
928928

929+
elif not is_list_like(key):
930+
# e.g. scalars that aren't recognized by lib.is_scalar, GH#32684
931+
return self.loc[key]
932+
929933
if not isinstance(key, (list, np.ndarray, ExtensionArray, Series, Index)):
930934
key = list(key)
931935

pandas/tests/dtypes/test_inference.py

+6
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,7 @@ def test_is_scalar_pandas_scalars(self):
14371437
assert is_scalar(Period("2014-01-01"))
14381438
assert is_scalar(Interval(left=0, right=1))
14391439
assert is_scalar(DateOffset(days=1))
1440+
assert is_scalar(pd.offsets.Minute(3))
14401441

14411442
def test_is_scalar_pandas_containers(self):
14421443
assert not is_scalar(Series(dtype=object))
@@ -1445,6 +1446,11 @@ def test_is_scalar_pandas_containers(self):
14451446
assert not is_scalar(DataFrame([[1]]))
14461447
assert not is_scalar(Index([]))
14471448
assert not is_scalar(Index([1]))
1449+
assert not is_scalar(Categorical([]))
1450+
assert not is_scalar(DatetimeIndex([])._data)
1451+
assert not is_scalar(TimedeltaIndex([])._data)
1452+
assert not is_scalar(DatetimeIndex([])._data.to_period("D"))
1453+
assert not is_scalar(pd.array([1, 2, 3]))
14481454

14491455
def test_is_scalar_number(self):
14501456
# Number() is not recognied by PyNumber_Check, so by extension

pandas/tests/series/indexing/test_indexing.py

+12
Original file line numberDiff line numberDiff line change
@@ -923,3 +923,15 @@ def test_getitem_2d_no_warning():
923923
series = pd.Series([1, 2, 3], index=[1, 2, 3])
924924
with tm.assert_produces_warning(None):
925925
series[:, None]
926+
927+
928+
def test_getitem_unrecognized_scalar():
929+
# GH#32684 a scalar key that is not recognized by lib.is_scalar
930+
931+
# a series that might be produced via `frame.dtypes`
932+
ser = pd.Series([1, 2], index=[np.dtype("O"), np.dtype("i8")])
933+
934+
key = ser.index[1]
935+
936+
result = ser[key]
937+
assert result == 2

0 commit comments

Comments
 (0)