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
REF: move MaskedArray subclass attributes to dtypes #58423
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f994a36
REF: move _internal_fill_value to MaskedDtype
jbrockmendel ba04b7a
REF: move MaskedArray subclass attrs to dtype
jbrockmendel ed17121
mypy fixup
jbrockmendel 0dec0ba
Merge branch 'main' into ref-maskeddtype
jbrockmendel 420c27e
privatize
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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