Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pandas/tests/arrays/integer/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import numpy as np
import pytest

from pandas.compat.numpy import _np_version_under1p20

import pandas as pd
import pandas._testing as tm
from pandas.core.arrays import integer_array
Expand Down Expand Up @@ -197,7 +199,9 @@ def test_arith_coerce_scalar(data, all_arithmetic_operators):
result = op(s, other)
expected = op(s.astype(float), other)
# rfloordiv results in nan instead of inf
if all_arithmetic_operators == "__rfloordiv__":
if all_arithmetic_operators == "__rfloordiv__" and _np_version_under1p20:
# for numpy 1.20 https://github.com/numpy/numpy/pull/16161
# updated floordiv, now matches our behavior defined in core.ops
expected[(expected == np.inf) | (expected == -np.inf)] = np.nan

tm.assert_series_equal(result, expected)
Expand Down
30 changes: 25 additions & 5 deletions pandas/tests/arrays/sparse/test_arithmetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import numpy as np
import pytest

from pandas.compat.numpy import _np_version_under1p20

import pandas as pd
import pandas._testing as tm
from pandas.core import ops
from pandas.core.arrays.sparse import SparseArray, SparseDtype
from pandas.core.arrays.sparse import IntIndex, SparseArray, SparseDtype


@pytest.fixture(params=["integer", "block"])
Expand All @@ -29,7 +31,7 @@ class TestSparseArrayArithmetics:
def _assert(self, a, b):
tm.assert_numpy_array_equal(a, b)

def _check_numeric_ops(self, a, b, a_dense, b_dense, mix, op):
def _check_numeric_ops(self, a, b, a_dense, b_dense, mix, op, flag=False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is flag for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leftover from previous attempt at a fix, will remove

with np.errstate(invalid="ignore", divide="ignore"):
if mix:
result = op(a, b_dense).to_dense()
Expand All @@ -44,9 +46,27 @@ def _check_numeric_ops(self, a, b, a_dense, b_dense, mix, op):

if op in [operator.floordiv, ops.rfloordiv]:
# Series sets 1//0 to np.inf, which SparseArray does not do (yet)
mask = np.isinf(expected)
if mask.any():
expected[mask] = np.nan
condition = _np_version_under1p20
if isinstance(a_dense, np.ndarray) and isinstance(b_dense, np.ndarray):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add some comments here on what is changed, it is non-obvious from inspection what is changing (e.g. what is true in prior version and what is true now)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah as mentioned above, i have no frikkin idea what the underlying logic is here

condition = True
if a_dense.dtype == np.float64 and np.isnan(a.fill_value):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems really fragile

# NB: these conditions are just guess-and-check
# to find what passes, no idea why these particular
# conditions are necessary.
if b_dense.dtype == np.int64 and mix:
condition = False

if a.sp_index.equals(b.sp_index):
if not mix:
condition = False
elif isinstance(a.sp_index, IntIndex):
condition = False
if condition:
# numpy 1.20 updated floordiv, matching the behavior
# in core.ops. See https://github.com/numpy/numpy/pull/16161
mask = np.isinf(expected)
if mask.any():
expected[mask] = np.nan

self._assert(result, expected)

Expand Down