Skip to content

Commit

Permalink
BUG: Series.__getitem__ with downstream scalars (pandas-dev#32684)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed Mar 21, 2020
1 parent 5b92d03 commit 0f0ec28
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ Indexing
- 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`)
- Bug in :meth:`DataFrame.iloc.__setitem__` on a :class:`DataFrame` with duplicate columns incorrectly setting values for all matching columns (:issue:`15686`, :issue:`22036`)
- 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`)
- Bug in :meth:`Series.__getitem__` indexing with non-standard scalars, e.g. ``np.dtype`` (:issue:`32684`)

Missing
^^^^^^^
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,10 @@ def _get_with(self, key):
elif isinstance(key, tuple):
return self._get_values_tuple(key)

elif not is_list_like(key):
# e.g. scalars that aren't recognized by lib.is_scalar, GH#32684
return self.loc[key]

if not isinstance(key, (list, np.ndarray, ExtensionArray, Series, Index)):
key = list(key)

Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,7 @@ def test_is_scalar_pandas_scalars(self):
assert is_scalar(Period("2014-01-01"))
assert is_scalar(Interval(left=0, right=1))
assert is_scalar(DateOffset(days=1))
assert is_scalar(pd.offsets.Minute(3))

def test_is_scalar_pandas_containers(self):
assert not is_scalar(Series(dtype=object))
Expand All @@ -1445,6 +1446,11 @@ def test_is_scalar_pandas_containers(self):
assert not is_scalar(DataFrame([[1]]))
assert not is_scalar(Index([]))
assert not is_scalar(Index([1]))
assert not is_scalar(Categorical([]))
assert not is_scalar(DatetimeIndex([])._data)
assert not is_scalar(TimedeltaIndex([])._data)
assert not is_scalar(DatetimeIndex([])._data.to_period("D"))
assert not is_scalar(pd.array([1, 2, 3]))

def test_is_scalar_number(self):
# Number() is not recognied by PyNumber_Check, so by extension
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,3 +923,15 @@ def test_getitem_2d_no_warning():
series = pd.Series([1, 2, 3], index=[1, 2, 3])
with tm.assert_produces_warning(None):
series[:, None]


def test_getitem_unrecognized_scalar():
# GH#32684 a scalar key that is not recognized by lib.is_scalar

# a series that might be produced via `frame.dtypes`
ser = pd.Series([1, 2], index=[np.dtype("O"), np.dtype("i8")])

key = ser.index[1]

result = ser[key]
assert result == 2

0 comments on commit 0f0ec28

Please sign in to comment.