Skip to content

Revert "PERF: ArrowExtensionArray.to_numpy(dtype=object)" #51273

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
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ Performance improvements
- Performance improvement in :meth:`~arrays.ArrowExtensionArray.factorize` (:issue:`49177`)
- Performance improvement in :meth:`~arrays.ArrowExtensionArray.__setitem__` (:issue:`50248`, :issue:`50632`)
- Performance improvement in :class:`~arrays.ArrowExtensionArray` comparison methods when array contains NA (:issue:`50524`)
- Performance improvement in :meth:`~arrays.ArrowExtensionArray.to_numpy` (:issue:`49973`, :issue:`51227`)
- Performance improvement in :meth:`~arrays.ArrowExtensionArray.to_numpy` (:issue:`49973`)
- Performance improvement when parsing strings to :class:`BooleanDtype` (:issue:`50613`)
- Performance improvement in :meth:`DataFrame.join` when joining on a subset of a :class:`MultiIndex` (:issue:`48611`)
- Performance improvement for :meth:`MultiIndex.intersection` (:issue:`48604`)
Expand Down
10 changes: 5 additions & 5 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,12 +853,12 @@ def to_numpy(
na_value = self.dtype.na_value

pa_type = self._data.type
if pa.types.is_timestamp(pa_type) or pa.types.is_duration(pa_type):
if (
is_object_dtype(dtype)
or pa.types.is_timestamp(pa_type)
or pa.types.is_duration(pa_type)
):
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]._data)
else:
result = np.asarray(self._data, dtype=dtype)
if copy or self._hasna:
Expand Down
10 changes: 0 additions & 10 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1518,16 +1518,6 @@ def test_to_numpy_with_defaults(data):
tm.assert_numpy_array_equal(result, expected)


def test_to_numpy_int_with_na():
# GH51227: ensure to_numpy does not convert int to float
data = [1, None]
arr = pd.array(data, dtype="int64[pyarrow]")
result = arr.to_numpy()
expected = np.array([1, pd.NA], dtype=object)
assert isinstance(result[0], int)
tm.assert_numpy_array_equal(result, expected)


def test_setitem_null_slice(data):
# GH50248
orig = data.copy()
Expand Down