Skip to content

Commit 2061e9e

Browse files
gliptakjreback
authored andcommitted
BUG: Fix series comparison operators when dealing with zero rank numpy arrays
closes #13006 Author: Gábor Lipták <[email protected]> Closes #13307 from gliptak/seriescomp1 and squashes the following commits: 4967db4 [Gábor Lipták] Fix series comparison operators when dealing with zero rank numpy arrays
1 parent 0c6226c commit 2061e9e

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

doc/source/whatsnew/v0.18.2.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ API changes
101101

102102

103103
- Non-convertible dates in an excel date column will be returned without conversion and the column will be ``object`` dtype, rather than raising an exception (:issue:`10001`)
104-
- An ``UnsupportedFunctionCall`` error is now raised if numpy ufuncs like ``np.mean`` are called on groupby or resample objects (:issue:`12811`)
104+
- An ``UnsupportedFunctionCall`` error is now raised if NumPy ufuncs like ``np.mean`` are called on groupby or resample objects (:issue:`12811`)
105105
- Calls to ``.sample()`` will respect the random seed set via ``numpy.random.seed(n)`` (:issue:`13161`)
106106

107107
.. _whatsnew_0182.api.tolist:
@@ -368,6 +368,7 @@ Bug Fixes
368368
- Bug in ``.unstack`` with ``Categorical`` dtype resets ``.ordered`` to ``True`` (:issue:`13249`)
369369

370370

371+
- Bug in ``Series`` comparison operators when dealing with zero dim NumPy arrays (:issue:`13006`)
371372
- Bug in ``groupby`` where ``apply`` returns different result depending on whether first result is ``None`` or not (:issue:`12824`)
372373

373374

pandas/core/ops.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,10 @@ def wrapper(self, other, axis=None):
754754
elif isinstance(other, pd.DataFrame): # pragma: no cover
755755
return NotImplemented
756756
elif isinstance(other, (np.ndarray, pd.Index)):
757-
if len(self) != len(other):
757+
# do not check length of zerodim array
758+
# as it will broadcast
759+
if (not lib.isscalar(lib.item_from_zerodim(other)) and
760+
len(self) != len(other)):
758761
raise ValueError('Lengths must match to compare')
759762
return self._constructor(na_op(self.values, np.asarray(other)),
760763
index=self.index).__finalize__(self)

pandas/tests/series/test_operators.py

+12
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,18 @@ def test_operators_timedelta64(self):
264264
rs[2] += np.timedelta64(timedelta(minutes=5, seconds=1))
265265
self.assertEqual(rs[2], value)
266266

267+
def test_operator_series_comparison_zerorank(self):
268+
# GH 13006
269+
result = np.float64(0) > pd.Series([1, 2, 3])
270+
expected = 0.0 > pd.Series([1, 2, 3])
271+
self.assert_series_equal(result, expected)
272+
result = pd.Series([1, 2, 3]) < np.float64(0)
273+
expected = pd.Series([1, 2, 3]) < 0.0
274+
self.assert_series_equal(result, expected)
275+
result = np.array([0, 1, 2])[0] > pd.Series([0, 1, 2])
276+
expected = 0.0 > pd.Series([1, 2, 3])
277+
self.assert_series_equal(result, expected)
278+
267279
def test_timedeltas_with_DateOffset(self):
268280

269281
# GH 4532

0 commit comments

Comments
 (0)