-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
REF: move MaskedArray subclass attributes to dtypes #58423
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
Changes from 3 commits
f994a36
ba04b7a
ed17121
0dec0ba
420c27e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
Any, | ||
Callable, | ||
Literal, | ||
cast, | ||
overload, | ||
) | ||
import warnings | ||
|
@@ -16,22 +17,6 @@ | |
missing as libmissing, | ||
) | ||
from pandas._libs.tslibs import is_supported_dtype | ||
from pandas._typing import ( | ||
ArrayLike, | ||
AstypeArg, | ||
AxisInt, | ||
DtypeObj, | ||
FillnaOptions, | ||
InterpolateOptions, | ||
NpDtype, | ||
PositionalIndexer, | ||
Scalar, | ||
ScalarIndexer, | ||
Self, | ||
SequenceIndexer, | ||
Shape, | ||
npt, | ||
) | ||
from pandas.compat import ( | ||
IS64, | ||
is_platform_windows, | ||
|
@@ -97,6 +82,20 @@ | |
from pandas._typing import ( | ||
NumpySorter, | ||
NumpyValueArrayLike, | ||
ArrayLike, | ||
AstypeArg, | ||
AxisInt, | ||
DtypeObj, | ||
FillnaOptions, | ||
InterpolateOptions, | ||
NpDtype, | ||
PositionalIndexer, | ||
Scalar, | ||
ScalarIndexer, | ||
Self, | ||
SequenceIndexer, | ||
Shape, | ||
npt, | ||
) | ||
from pandas._libs.missing import NAType | ||
from pandas.core.arrays import FloatingArray | ||
|
@@ -111,16 +110,10 @@ class BaseMaskedArray(OpsMixin, ExtensionArray): | |
numpy based | ||
""" | ||
|
||
# The value used to fill '_data' to avoid upcasting | ||
_internal_fill_value: Scalar | ||
# our underlying data and mask are each ndarrays | ||
_data: np.ndarray | ||
_mask: npt.NDArray[np.bool_] | ||
|
||
# Fill values used for any/all | ||
_truthy_value = Scalar # bool(_truthy_value) = True | ||
_falsey_value = Scalar # bool(_falsey_value) = False | ||
|
||
@classmethod | ||
def _simple_new(cls, values: np.ndarray, mask: npt.NDArray[np.bool_]) -> Self: | ||
result = BaseMaskedArray.__new__(cls) | ||
|
@@ -155,8 +148,9 @@ def _from_sequence(cls, scalars, *, dtype=None, copy: bool = False) -> Self: | |
@classmethod | ||
@doc(ExtensionArray._empty) | ||
def _empty(cls, shape: Shape, dtype: ExtensionDtype) -> Self: | ||
values = np.empty(shape, dtype=dtype.type) | ||
values.fill(cls._internal_fill_value) | ||
dtype = cast(BaseMaskedDtype, dtype) | ||
values: np.ndarray = np.empty(shape, dtype=dtype.type) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the type declaration required here? I assume mypy would be able to infer this from the right hand side There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mypy complained without this |
||
values.fill(dtype._internal_fill_value) | ||
mask = np.ones(shape, dtype=bool) | ||
result = cls(values, mask) | ||
if not isinstance(result, cls) or dtype != result.dtype: | ||
|
@@ -917,7 +911,9 @@ def take( | |
) -> Self: | ||
# we always fill with 1 internally | ||
# to avoid upcasting | ||
data_fill_value = self._internal_fill_value if isna(fill_value) else fill_value | ||
data_fill_value = ( | ||
self.dtype._internal_fill_value if isna(fill_value) else fill_value | ||
) | ||
result = take( | ||
self._data, | ||
indexer, | ||
|
@@ -1397,12 +1393,7 @@ def any( | |
nv.validate_any((), kwargs) | ||
|
||
values = self._data.copy() | ||
# error: Argument 3 to "putmask" has incompatible type "object"; | ||
# expected "Union[_SupportsArray[dtype[Any]], | ||
# _NestedSequence[_SupportsArray[dtype[Any]]], | ||
# bool, int, float, complex, str, bytes, | ||
# _NestedSequence[Union[bool, int, float, complex, str, bytes]]]" | ||
np.putmask(values, self._mask, self._falsey_value) # type: ignore[arg-type] | ||
np.putmask(values, self._mask, self.dtype.falsey_value) | ||
result = values.any() | ||
if skipna: | ||
return result | ||
|
@@ -1490,12 +1481,7 @@ def all( | |
nv.validate_all((), kwargs) | ||
|
||
values = self._data.copy() | ||
# error: Argument 3 to "putmask" has incompatible type "object"; | ||
# expected "Union[_SupportsArray[dtype[Any]], | ||
# _NestedSequence[_SupportsArray[dtype[Any]]], | ||
# bool, int, float, complex, str, bytes, | ||
# _NestedSequence[Union[bool, int, float, complex, str, bytes]]]" | ||
np.putmask(values, self._mask, self._truthy_value) # type: ignore[arg-type] | ||
np.putmask(values, self._mask, self.dtype.truthy_value) | ||
result = values.all(axis=axis) | ||
|
||
if skipna: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,7 @@ | |
DtypeObj, | ||
IntervalClosedType, | ||
Ordered, | ||
Scalar, | ||
Self, | ||
npt, | ||
type_t, | ||
|
@@ -1542,6 +1543,25 @@ class BaseMaskedDtype(ExtensionDtype): | |
|
||
base = None | ||
type: type | ||
_internal_fill_value: Scalar | ||
|
||
@property | ||
def truthy_value(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these be "private" ie `_truthy_value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. they are accessed from outside the dtype's methods There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't these raise NotImplementedError as a fallback? The idea is this class can be used for more than just float/integer/bool in the future right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i dont think there's any plans to extend like that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On second thought decided to privatize after all |
||
# Fill values used for 'any' | ||
if self.kind == "f": | ||
return 1.0 | ||
if self.kind in "iu": | ||
return 1 | ||
return True | ||
|
||
@property | ||
def falsey_value(self): | ||
# Fill values used for 'all' | ||
if self.kind == "f": | ||
return 0.0 | ||
if self.kind in "iu": | ||
return 0 | ||
return False | ||
|
||
@property | ||
def na_value(self) -> libmissing.NAType: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cast suspicious - why wouldn't we just change the type of
dtype
toBaseMaskedType
if the_internal_fill_value
attribute was required?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC mypy complains if we do that, but in practice that is correct that we should only ever get BaseMaskedDtype here