Skip to content

Commit 5fb4626

Browse files
committed
optimize checks
1 parent a922bbd commit 5fb4626

File tree

5 files changed

+17
-11
lines changed

5 files changed

+17
-11
lines changed

pandas/_config/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@
3333
def using_string_dtype() -> bool:
3434
_mode_options = _global_config["future"]
3535
return _mode_options["infer_string"]
36+
37+
38+
def using_pdep16_nan_behavior() -> bool:
39+
_mode_options = _global_config["mode"]
40+
return _mode_options["pdep16_nan_behavior"]

pandas/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2127,5 +2127,5 @@ def monkeysession():
21272127
@pytest.fixture(params=[True, False])
21282128
def pdep16_nan_behavior(request):
21292129
opt = request.param
2130-
with pd.option_context("mode.pdep16_nan_behavior", opt):
2130+
with pd.option_context("mode.PDEP16_nan_behavior", opt):
21312131
yield opt

pandas/core/arrays/masked.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import numpy as np
1313

14-
from pandas._config import get_option
14+
from pandas._config import using_pdep16_nan_behavior
1515

1616
from pandas._libs import (
1717
lib,
@@ -310,7 +310,7 @@ def __setitem__(self, key, value) -> None:
310310
def __contains__(self, key) -> bool:
311311
if isna(key) and key is not self.dtype.na_value:
312312
# GH#52840
313-
if lib.is_float(key) and get_option("mode.PDEP16_nan_behavior"):
313+
if lib.is_float(key) and using_pdep16_nan_behavior():
314314
key = self.dtype.na_value
315315
elif self._data.dtype.kind == "f" and lib.is_float(key):
316316
return bool((np.isnan(self._data) & ~self._mask).any())
@@ -659,7 +659,7 @@ def reconstruct(x: np.ndarray):
659659
# reached in e.g. np.sqrt on BooleanArray
660660
# we don't support float16
661661
x = x.astype(np.float32)
662-
if get_option("mode.PDEP16_nan_behavior"):
662+
if using_pdep16_nan_behavior():
663663
m[np.isnan(x)] = True
664664
return FloatingArray(x, m)
665665
else:
@@ -866,7 +866,7 @@ def _maybe_mask_result(
866866
if result.dtype.kind == "f":
867867
from pandas.core.arrays import FloatingArray
868868

869-
if get_option("mode.PDEP16_nan_behavior"):
869+
if using_pdep16_nan_behavior():
870870
mask[np.isnan(result)] = True
871871

872872
return FloatingArray(result, mask, copy=False)

pandas/core/arrays/numeric.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import numpy as np
1010

11-
from pandas._config import get_option
11+
from pandas._config import using_pdep16_nan_behavior
1212

1313
from pandas._libs import (
1414
lib,
@@ -103,7 +103,7 @@ def __from_arrow__(
103103
array = array.combine_chunks()
104104

105105
data, mask = pyarrow_array_to_numpy_and_mask(array, dtype=self.numpy_dtype)
106-
if data.dtype.kind == "f" and get_option("mode.PDEP16_nan_behavior"):
106+
if data.dtype.kind == "f" and using_pdep16_nan_behavior():
107107
mask[np.isnan(data)] = False
108108
return array_class(data.copy(), ~mask, copy=False)
109109

@@ -274,7 +274,7 @@ def __init__(
274274
# If we don't raise here, then accessing self.dtype would raise
275275
raise TypeError("FloatingArray does not support np.float16 dtype.")
276276

277-
# NB: if get_option("mode.PDEP16_nan_behavior") is True
277+
# NB: if using_pdep16_nan_behavior() is True
278278
# then caller is responsible for ensuring
279279
# assert mask[np.isnan(values)].all()
280280

pandas/core/indexes/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from pandas._config import (
2323
get_option,
24+
using_pdep16_nan_behavior,
2425
using_string_dtype,
2526
)
2627

@@ -162,6 +163,7 @@
162163
ExtensionArray,
163164
TimedeltaArray,
164165
)
166+
from pandas.core.arrays.floating import FloatingDtype
165167
from pandas.core.arrays.string_ import (
166168
StringArray,
167169
StringDtype,
@@ -6587,9 +6589,8 @@ def _maybe_cast_indexer(self, key):
65876589
if (
65886590
is_float(key)
65896591
and np.isnan(key)
6590-
and isinstance(self.dtype, ExtensionDtype)
6591-
and self.dtype.kind == "f"
6592-
and get_option("mode.pdep16_nan_behavior")
6592+
and isinstance(self.dtype, FloatingDtype)
6593+
and using_pdep16_nan_behavior()
65936594
):
65946595
# TODO: better place to do this?
65956596
key = self.dtype.na_value

0 commit comments

Comments
 (0)