From d6fe9792a4ac08e3ddf6ceaa4c65c980b76d5d57 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 14 Aug 2024 09:32:29 -0700 Subject: [PATCH 1/2] REF (string): remove _str_na_value --- pandas/core/arrays/numpy_.py | 4 ---- pandas/core/arrays/string_.py | 10 ---------- pandas/core/arrays/string_arrow.py | 4 ---- pandas/core/strings/object_array.py | 10 ++++------ 4 files changed, 4 insertions(+), 24 deletions(-) diff --git a/pandas/core/arrays/numpy_.py b/pandas/core/arrays/numpy_.py index 07eb91e0cb13b..03712f75db0c7 100644 --- a/pandas/core/arrays/numpy_.py +++ b/pandas/core/arrays/numpy_.py @@ -557,7 +557,3 @@ def _wrap_ndarray_result(self, result: np.ndarray): return TimedeltaArray._simple_new(result, dtype=result.dtype) return type(self)(result) - - # ------------------------------------------------------------------------ - # String methods interface - _str_na_value = np.nan diff --git a/pandas/core/arrays/string_.py b/pandas/core/arrays/string_.py index bdced4c496d14..11924003ba4d5 100644 --- a/pandas/core/arrays/string_.py +++ b/pandas/core/arrays/string_.py @@ -827,12 +827,6 @@ def _cmp_method(self, other, op): _arith_method = _cmp_method - # ------------------------------------------------------------------------ - # String methods interface - # error: Incompatible types in assignment (expression has type "NAType", - # base class "NumpyExtensionArray" defined the type as "float") - _str_na_value = libmissing.NA # type: ignore[assignment] - class StringArrayNumpySemantics(StringArray): _storage = "python" @@ -894,7 +888,3 @@ def value_counts(self, dropna: bool = True) -> Series: result = value_counts(self._ndarray, sort=False, dropna=dropna) result.index = result.index.astype(self.dtype) return result - - # ------------------------------------------------------------------------ - # String methods interface - _str_na_value = np.nan diff --git a/pandas/core/arrays/string_arrow.py b/pandas/core/arrays/string_arrow.py index 8e33179de0512..e7baf802c3194 100644 --- a/pandas/core/arrays/string_arrow.py +++ b/pandas/core/arrays/string_arrow.py @@ -275,10 +275,6 @@ def astype(self, dtype, copy: bool = True): # ------------------------------------------------------------------------ # String methods interface - # error: Incompatible types in assignment (expression has type "NAType", - # base class "ObjectStringArrayMixin" defined the type as "float") - _str_na_value = libmissing.NA # type: ignore[assignment] - _str_map = BaseStringArray._str_map def _str_contains( diff --git a/pandas/core/strings/object_array.py b/pandas/core/strings/object_array.py index 290a28ab60ae1..148a3830840a6 100644 --- a/pandas/core/strings/object_array.py +++ b/pandas/core/strings/object_array.py @@ -37,8 +37,6 @@ class ObjectStringArrayMixin(BaseStringArrayMethods): String Methods operating on object-dtype ndarrays. """ - _str_na_value = np.nan - def __len__(self) -> int: # For typing, _str_map relies on the object being sized. raise NotImplementedError @@ -56,7 +54,7 @@ def _str_map( na_value : Scalar, optional The value to set for NA values. Might also be used for the fill value if the callable `f` raises an exception. - This defaults to ``self._str_na_value`` which is ``np.nan`` + This defaults to ``self.dtype.na_value`` which is ``np.nan`` for object-dtype and Categorical and ``pd.NA`` for StringArray. dtype : Dtype, optional The dtype of the result array. @@ -66,7 +64,7 @@ def _str_map( if dtype is None: dtype = np.dtype("object") if na_value is None: - na_value = self._str_na_value + na_value = self.dtype.na_value if not len(self): return np.array([], dtype=dtype) @@ -272,7 +270,7 @@ def f(x): return x.get(i) elif len(x) > i >= -len(x): return x[i] - return self._str_na_value + return self.dtype.na_value return self._str_map(f) @@ -466,7 +464,7 @@ def _str_removesuffix(self, suffix: str): def _str_extract(self, pat: str, flags: int = 0, expand: bool = True): regex = re.compile(pat, flags=flags) - na_value = self._str_na_value + na_value = self.dtype.na_value if not expand: From 188275b1d53c7814ba419c32bd99ab88e329f7ec Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 14 Aug 2024 10:59:54 -0700 Subject: [PATCH 2/2] mypy fixup --- pandas/core/strings/object_array.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/strings/object_array.py b/pandas/core/strings/object_array.py index 148a3830840a6..100afa956bd24 100644 --- a/pandas/core/strings/object_array.py +++ b/pandas/core/strings/object_array.py @@ -64,7 +64,7 @@ def _str_map( if dtype is None: dtype = np.dtype("object") if na_value is None: - na_value = self.dtype.na_value + na_value = self.dtype.na_value # type: ignore[attr-defined] if not len(self): return np.array([], dtype=dtype) @@ -270,7 +270,7 @@ def f(x): return x.get(i) elif len(x) > i >= -len(x): return x[i] - return self.dtype.na_value + return self.dtype.na_value # type: ignore[attr-defined] return self._str_map(f) @@ -464,7 +464,7 @@ def _str_removesuffix(self, suffix: str): def _str_extract(self, pat: str, flags: int = 0, expand: bool = True): regex = re.compile(pat, flags=flags) - na_value = self.dtype.na_value + na_value = self.dtype.na_value # type: ignore[attr-defined] if not expand: