-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: add ExtensionArray.to_numpy to have control over conversion to numpy array #30322
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
Merged
jorisvandenbossche
merged 18 commits into
pandas-dev:master
from
jorisvandenbossche:NA-to_numpy
Jan 7, 2020
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a3dab78
ENH: add to_numpy to have control over conversion to numpy array
jorisvandenbossche 8e669fc
Merge remote-tracking branch 'upstream/master' into NA-to_numpy
jorisvandenbossche fd1c04a
move isna check earlier + simplify
jorisvandenbossche 92f14d2
passthrough keywords in Series.to_numpy
jorisvandenbossche bf9592c
Merge remote-tracking branch 'upstream/master' into NA-to_numpy
TomAugspurger 278447d
doc note
TomAugspurger 1da45bd
update
TomAugspurger 3889986
Merge remote-tracking branch 'upstream/master' into NA-to_numpy
TomAugspurger 6832f42
updates
TomAugspurger e8460ac
update
TomAugspurger f1e34c6
dataframe
TomAugspurger 1624712
compat
TomAugspurger 34307ca
typing
TomAugspurger 2b43793
Merge remote-tracking branch 'upstream/master' into NA-to_numpy
TomAugspurger afc7350
fixups
TomAugspurger 008b54f
remove unused test
TomAugspurger 91a4639
Update doc/source/reference/extensions.rst
jorisvandenbossche fff20a0
Merge branch 'NA-to_numpy' of https://github.com/jorisvandenbossche/p…
TomAugspurger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -296,29 +296,47 @@ def __getitem__(self, item): | |
return self._data[item] | ||
return type(self)(self._data[item], self._mask[item]) | ||
|
||
def _coerce_to_ndarray(self, dtype=None, na_value: "Scalar" = libmissing.NA): | ||
def to_numpy(self, dtype=None, copy=False, na_value: "Scalar" = libmissing.NA): | ||
""" | ||
Coerce to an ndarray of object dtype or bool dtype (if force_bool=True). | ||
Convert to a numpy array. | ||
|
||
By default converts to a numpy object array. Specify the `dtype` and | ||
`na_value` keywords to customize the conversion. | ||
|
||
Parameters | ||
---------- | ||
dtype : dtype, default object | ||
The numpy dtype to convert to | ||
The numpy dtype to convert to. | ||
copy : bool, default False | ||
Whether to ensure that the returned value is a not a view on | ||
the array. Note that ``copy=False`` does not *ensure* that | ||
``to_numpy()`` is no-copy. Rather, ``copy=True`` ensure that | ||
a copy is made, even if not strictly necessary. | ||
na_value : scalar, optional | ||
Scalar missing value indicator to use in numpy array. Defaults | ||
to the native missing value indicator of this array (pd.NA). | ||
|
||
Returns | ||
------- | ||
np.ndarray | ||
""" | ||
if dtype is None: | ||
dtype = object | ||
if is_bool_dtype(dtype): | ||
if is_bool_dtype(dtype) and na_value is libmissing.NA: | ||
if not self.isna().any(): | ||
return self._data | ||
data = self._data | ||
if copy: | ||
data = data.copy() | ||
else: | ||
raise ValueError( | ||
"cannot convert to bool numpy array in presence of missing values" | ||
) | ||
data = self._data.astype(dtype) | ||
data[self._mask] = na_value | ||
if self.isna().any(): | ||
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. Can you move this up to before L325? It seems like an |
||
# don't pass copy to astype -> always need a copy since we are mutating | ||
data = self._data.astype(dtype) | ||
data[self._mask] = na_value | ||
else: | ||
data = self._data.astype(dtype, copy=copy) | ||
return data | ||
|
||
__array_priority__ = 1000 # higher than ndarray so ops dispatch to us | ||
|
@@ -329,7 +347,7 @@ def __array__(self, dtype=None): | |
We return an object array here to preserve our scalar values | ||
""" | ||
# by default (no dtype specified), return an object array | ||
return self._coerce_to_ndarray(dtype=dtype) | ||
return self.to_numpy(dtype=dtype) | ||
|
||
def __arrow_array__(self, type=None): | ||
""" | ||
|
@@ -505,7 +523,7 @@ def astype(self, dtype, copy=True): | |
if is_float_dtype(dtype): | ||
na_value = np.nan | ||
# coerce | ||
data = self._coerce_to_ndarray(na_value=na_value) | ||
data = self.to_numpy(na_value=na_value) | ||
return astype_nansafe(data, dtype, copy=None) | ||
|
||
def value_counts(self, dropna=True): | ||
|
This file contains hidden or 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
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.
Can we give some guidance on when no-copy is possible? Is it only when there are no missing values and we're going to the numpy dtype (bool in this case)?
And thinking forward, can a pyarrow array with no NAs be converted to an ndarray without any copies?
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.
Yes, I think it is only possible with no NAs and bool dtype. I first thought int8 would also be possible, but numpy doesn't seem to do such conversion without copy.
For boolean not (since it is bits, not bytes), but in general (eg for IntegerArray without nulls) yes