-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: ArrowExtensionArray.to_numpy avoid object dtype when na_value provided #54843
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
mroeschke
merged 7 commits into
pandas-dev:main
from
lukemanley:arrow-to-numpy-without-dtype
Sep 1, 2023
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e26e164
ENH: ArrowExtensionArray.to_numpy to avoid object dtype when na_value…
lukemanley c95d904
Merge remote-tracking branch 'upstream/main' into arrow-to-numpy-with…
lukemanley 084c4c1
refactor
lukemanley baf3227
Merge remote-tracking branch 'upstream/main' into arrow-to-numpy-with…
lukemanley 609b755
cleanup
lukemanley 0eb9ba0
mypy
lukemanley 3dca86b
fix
lukemanley 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 |
---|---|---|
|
@@ -29,12 +29,12 @@ | |
from pandas.util._decorators import doc | ||
from pandas.util._validators import validate_fillna_kwargs | ||
|
||
from pandas.core.dtypes.cast import can_hold_element | ||
from pandas.core.dtypes.common import ( | ||
is_array_like, | ||
is_bool_dtype, | ||
is_integer, | ||
is_list_like, | ||
is_object_dtype, | ||
is_scalar, | ||
) | ||
from pandas.core.dtypes.dtypes import DatetimeTZDtype | ||
|
@@ -1240,46 +1240,50 @@ def to_numpy( | |
) -> np.ndarray: | ||
if dtype is not None: | ||
dtype = np.dtype(dtype) | ||
elif self._hasna: | ||
dtype = np.dtype(object) | ||
|
||
if na_value is lib.no_default: | ||
na_value = self.dtype.na_value | ||
|
||
pa_type = self._pa_array.type | ||
if not self._hasna or isna(na_value) or pa.types.is_null(pa_type): | ||
data = self | ||
else: | ||
data = self.fillna(na_value) | ||
copy = False | ||
|
||
if pa.types.is_timestamp(pa_type) or pa.types.is_duration(pa_type): | ||
result = self._maybe_convert_datelike_array() | ||
result = data._maybe_convert_datelike_array() | ||
if dtype is None or dtype.kind == "O": | ||
result = result.to_numpy(dtype=object, na_value=na_value) | ||
else: | ||
result = result.to_numpy(dtype=dtype) | ||
return result | ||
elif pa.types.is_time(pa_type) or pa.types.is_date(pa_type): | ||
# convert to list of python datetime.time objects before | ||
# wrapping in ndarray | ||
result = np.array(list(self), dtype=dtype) | ||
elif is_object_dtype(dtype) and self._hasna: | ||
result = np.empty(len(self), dtype=object) | ||
mask = ~self.isna() | ||
result[mask] = np.asarray(self[mask]._pa_array) | ||
elif pa.types.is_null(self._pa_array.type): | ||
fill_value = None if isna(na_value) else na_value | ||
return np.full(len(self), fill_value=fill_value, dtype=dtype) | ||
elif self._hasna: | ||
data = self.fillna(na_value) | ||
result = np.array(list(data), dtype=dtype) | ||
if data._hasna: | ||
result[data.isna()] = na_value | ||
elif pa.types.is_null(pa_type): | ||
if dtype is not None and dtype.kind != "O" and isna(na_value): | ||
na_value = None | ||
result = np.full(len(data), fill_value=na_value, dtype=dtype) | ||
elif not data._hasna or (pa.types.is_floating(pa_type) and na_value is np.nan): | ||
result = data._pa_array.to_numpy() | ||
if dtype is not None: | ||
result = result.astype(dtype, copy=False) | ||
return result | ||
else: | ||
result = self._pa_array.to_numpy() | ||
if dtype is not None: | ||
result = result.astype(dtype, copy=False) | ||
if copy: | ||
result = result.copy() | ||
return result | ||
if self._hasna: | ||
result[self.isna()] = na_value | ||
else: | ||
if dtype is None: | ||
empty = data._pa_array[:0].to_numpy() | ||
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 think 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. yep, thanks. updated |
||
if can_hold_element(empty, na_value): | ||
dtype = empty.dtype | ||
else: | ||
dtype = np.object_ | ||
result = np.empty(len(data), dtype=dtype) | ||
mask = data.isna() | ||
result[mask] = na_value | ||
result[~mask] = data[~mask]._pa_array.to_numpy() | ||
return result | ||
|
||
def unique(self) -> Self: | ||
|
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
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.
Why do we need to skip object here?
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.
removed it, not needed