Skip to content

CLN: remove unused nan comparison functions, ensure_float #52385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 0 additions & 27 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,6 @@
_is_scipy_sparse = None

ensure_float64 = algos.ensure_float64


def ensure_float(arr):
"""
Ensure that an array object has a float dtype if possible.

Parameters
----------
arr : array-like
The array whose data type we want to enforce as float.

Returns
-------
float_arr : The original array cast to the float dtype if
possible. Otherwise, the original array is returned.
"""
if is_extension_array_dtype(arr.dtype):
if is_float_dtype(arr.dtype):
arr = arr.to_numpy(dtype=arr.dtype.numpy_dtype, na_value=np.nan)
else:
arr = arr.to_numpy(dtype="float64", na_value=np.nan)
elif issubclass(arr.dtype.type, (np.integer, np.bool_)):
arr = arr.astype(float)
return arr


ensure_int64 = algos.ensure_int64
ensure_int32 = algos.ensure_int32
ensure_int16 = algos.ensure_int16
Expand Down Expand Up @@ -1735,7 +1709,6 @@ def is_all_strings(value: ArrayLike) -> bool:
"classes",
"classes_and_not_datetimelike",
"DT64NS_DTYPE",
"ensure_float",
"ensure_float64",
"ensure_python_int",
"ensure_str",
Expand Down
31 changes: 0 additions & 31 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import functools
import itertools
import operator
from typing import (
Any,
Callable,
Expand Down Expand Up @@ -36,7 +35,6 @@

from pandas.core.dtypes.common import (
is_any_int_dtype,
is_bool_dtype,
is_complex,
is_float,
is_float_dtype,
Expand Down Expand Up @@ -1688,35 +1686,6 @@ def _ensure_numeric(x):
return x


# NA-friendly array comparisons


def make_nancomp(op):
def f(x, y):
xmask = isna(x)
ymask = isna(y)
mask = xmask | ymask

result = op(x, y)

if mask.any():
if is_bool_dtype(result):
result = result.astype("O")
np.putmask(result, mask, np.nan)

return result

return f


nangt = make_nancomp(operator.gt)
nange = make_nancomp(operator.ge)
nanlt = make_nancomp(operator.lt)
nanle = make_nancomp(operator.le)
naneq = make_nancomp(operator.eq)
nanne = make_nancomp(operator.ne)


def na_accum_func(values: ArrayLike, accum_func, *, skipna: bool) -> ArrayLike:
"""
Cumulative function with skipna support.
Expand Down
16 changes: 1 addition & 15 deletions pandas/tests/series/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
isna,
)
import pandas._testing as tm
from pandas.core import (
nanops,
ops,
)
from pandas.core import ops
from pandas.core.computation import expressions as expr
from pandas.core.computation.check import NUMEXPR_INSTALLED

Expand Down Expand Up @@ -499,17 +496,6 @@ def test_ser_cmp_result_names(self, names, comparison_op):
assert result.name == names[2]

def test_comparisons(self):
left = np.random.randn(10)
right = np.random.randn(10)
left[:3] = np.nan

result = nanops.nangt(left, right)
with np.errstate(invalid="ignore"):
expected = (left > right).astype("O")
expected[:3] = np.nan

tm.assert_almost_equal(result, expected)

s = Series(["a", "b", "c"])
s2 = Series([False, True, False])

Expand Down
48 changes: 0 additions & 48 deletions pandas/tests/test_nanops.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from functools import partial
import operator
import warnings

import numpy as np
Expand Down Expand Up @@ -744,53 +743,6 @@ def test_nancov(self):
self.check_nancorr_nancov_1d(nanops.nancov, targ0, targ1)


@pytest.mark.parametrize(
"op,nanop",
[
(operator.eq, nanops.naneq),
(operator.ne, nanops.nanne),
(operator.gt, nanops.nangt),
(operator.ge, nanops.nange),
(operator.lt, nanops.nanlt),
(operator.le, nanops.nanle),
],
)
def test_nan_comparison(request, op, nanop, disable_bottleneck):
arr_float = request.getfixturevalue("arr_float")
arr_float1 = request.getfixturevalue("arr_float")
targ0 = op(arr_float, arr_float1)
arr_nan = request.getfixturevalue("arr_nan")
arr_nan_nan = request.getfixturevalue("arr_nan_nan")
arr_float_nan = request.getfixturevalue("arr_float_nan")
arr_float1_nan = request.getfixturevalue("arr_float_nan")
arr_nan_float1 = request.getfixturevalue("arr_nan_float1")

while targ0.ndim:
res0 = nanop(arr_float, arr_float1)
tm.assert_almost_equal(targ0, res0)

if targ0.ndim > 1:
targ1 = np.vstack([targ0, arr_nan])
else:
targ1 = np.hstack([targ0, arr_nan])
res1 = nanop(arr_float_nan, arr_float1_nan)
tm.assert_numpy_array_equal(targ1, res1, check_dtype=False)

targ2 = arr_nan_nan
res2 = nanop(arr_float_nan, arr_nan_float1)
tm.assert_numpy_array_equal(targ2, res2, check_dtype=False)

# Lower dimension for next step in the loop
arr_float = np.take(arr_float, 0, axis=-1)
arr_float1 = np.take(arr_float1, 0, axis=-1)
arr_nan = np.take(arr_nan, 0, axis=-1)
arr_nan_nan = np.take(arr_nan_nan, 0, axis=-1)
arr_float_nan = np.take(arr_float_nan, 0, axis=-1)
arr_float1_nan = np.take(arr_float1_nan, 0, axis=-1)
arr_nan_float1 = np.take(arr_nan_float1, 0, axis=-1)
targ0 = np.take(targ0, 0, axis=-1)


@pytest.mark.parametrize(
"arr, correct",
[
Expand Down