Skip to content

Commit de40565

Browse files
authored
TYP: make na_value consistently a property (#47676)
* TYP: make na_value consistently a property * property * replace a few more class accesses
1 parent f23e441 commit de40565

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

pandas/core/arrays/string_.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,11 @@ class StringDtype(StorageExtensionDtype):
9090

9191
name = "string"
9292

93-
#: StringDtype.na_value uses pandas.NA
94-
na_value = libmissing.NA
93+
#: StringDtype().na_value uses pandas.NA
94+
@property
95+
def na_value(self) -> libmissing.NAType:
96+
return libmissing.NA
97+
9598
_metadata = ("storage",)
9699

97100
def __init__(self, storage=None) -> None:
@@ -335,13 +338,11 @@ def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy=False):
335338
na_values = scalars._mask
336339
result = scalars._data
337340
result = lib.ensure_string_array(result, copy=copy, convert_na_value=False)
338-
result[na_values] = StringDtype.na_value
341+
result[na_values] = libmissing.NA
339342

340343
else:
341-
# convert non-na-likes to str, and nan-likes to StringDtype.na_value
342-
result = lib.ensure_string_array(
343-
scalars, na_value=StringDtype.na_value, copy=copy
344-
)
344+
# convert non-na-likes to str, and nan-likes to StringDtype().na_value
345+
result = lib.ensure_string_array(scalars, na_value=libmissing.NA, copy=copy)
345346

346347
# Manually creating new array avoids the validation step in the __init__, so is
347348
# faster. Refactor need for validation?
@@ -396,7 +397,7 @@ def __setitem__(self, key, value):
396397
# validate new items
397398
if scalar_value:
398399
if isna(value):
399-
value = StringDtype.na_value
400+
value = libmissing.NA
400401
elif not isinstance(value, str):
401402
raise ValueError(
402403
f"Cannot set non-string value '{value}' into a StringArray."
@@ -497,7 +498,7 @@ def _cmp_method(self, other, op):
497498

498499
if op.__name__ in ops.ARITHMETIC_BINOPS:
499500
result = np.empty_like(self._ndarray, dtype="object")
500-
result[mask] = StringDtype.na_value
501+
result[mask] = libmissing.NA
501502
result[valid] = op(self._ndarray[valid], other)
502503
return StringArray(result)
503504
else:
@@ -512,7 +513,7 @@ def _cmp_method(self, other, op):
512513
# String methods interface
513514
# error: Incompatible types in assignment (expression has type "NAType",
514515
# base class "PandasArray" defined the type as "float")
515-
_str_na_value = StringDtype.na_value # type: ignore[assignment]
516+
_str_na_value = libmissing.NA # type: ignore[assignment]
516517

517518
def _str_map(
518519
self, f, na_value=None, dtype: Dtype | None = None, convert: bool = True

pandas/core/arrays/string_arrow.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,9 @@ def astype(self, dtype, copy: bool = True):
242242
# ------------------------------------------------------------------------
243243
# String methods interface
244244

245-
# error: Cannot determine type of 'na_value'
246-
_str_na_value = StringDtype.na_value # type: ignore[has-type]
245+
# error: Incompatible types in assignment (expression has type "NAType",
246+
# base class "ObjectStringArrayMixin" defined the type as "float")
247+
_str_na_value = libmissing.NA # type: ignore[assignment]
247248

248249
def _str_map(
249250
self, f, na_value=None, dtype: Dtype | None = None, convert: bool = True

pandas/core/dtypes/dtypes.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -676,11 +676,14 @@ class DatetimeTZDtype(PandasExtensionDtype):
676676
kind: str_type = "M"
677677
num = 101
678678
base = np.dtype("M8[ns]") # TODO: depend on reso?
679-
na_value = NaT
680679
_metadata = ("unit", "tz")
681680
_match = re.compile(r"(datetime64|M8)\[(?P<unit>.+), (?P<tz>.+)\]")
682681
_cache_dtypes: dict[str_type, PandasExtensionDtype] = {}
683682

683+
@property
684+
def na_value(self) -> NaTType:
685+
return NaT
686+
684687
@cache_readonly
685688
def str(self):
686689
return f"|M8[{self._unit}]"
@@ -1450,7 +1453,9 @@ class BaseMaskedDtype(ExtensionDtype):
14501453
base = None
14511454
type: type
14521455

1453-
na_value = libmissing.NA
1456+
@property
1457+
def na_value(self) -> libmissing.NAType:
1458+
return libmissing.NA
14541459

14551460
@cache_readonly
14561461
def numpy_dtype(self) -> np.dtype:

pandas/core/indexes/numeric.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,10 @@ class Float64Index(NumericIndex):
409409
__doc__ = _num_index_shared_docs["class_descr"] % _index_descr_args
410410

411411
_typ = "float64index"
412-
_engine_type = libindex.Float64Engine
413412
_default_dtype = np.dtype(np.float64)
414413
_dtype_validation_metadata = (is_float_dtype, "float")
415414
_is_backward_compat_public_numeric_index: bool = False
415+
416+
@property
417+
def _engine_type(self) -> type[libindex.Float64Engine]:
418+
return libindex.Float64Engine

0 commit comments

Comments
 (0)