Skip to content

Commit ce7522b

Browse files
authored
PERF: ArrowExtensionArray.to_numpy (#52525)
* PERF: ArrowExtensionArray.to_numpy * whatsnew * mypy
1 parent efccff8 commit ce7522b

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ Performance improvements
199199
- Performance improvement in :class:`Series` reductions (:issue:`52341`)
200200
- Performance improvement in :meth:`Series.to_numpy` when dtype is a numpy float dtype and ``na_value`` is ``np.nan`` (:issue:`52430`)
201201
- Performance improvement in :meth:`Series.corr` and :meth:`Series.cov` for extension dtypes (:issue:`52502`)
202+
- Performance improvement in :meth:`~arrays.ArrowExtensionArray.to_numpy` (:issue:`52525`)
202203
-
203204

204205
.. ---------------------------------------------------------------------------

pandas/core/arrays/arrow/array.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -1046,18 +1046,21 @@ def to_numpy(
10461046
mask = ~self.isna()
10471047
result[mask] = np.asarray(self[mask]._pa_array)
10481048
elif pa.types.is_null(self._pa_array.type):
1049-
result = np.asarray(self._pa_array, dtype=dtype)
1050-
if not isna(na_value):
1051-
result[:] = na_value
1052-
return result
1049+
fill_value = None if isna(na_value) else na_value
1050+
return np.full(len(self), fill_value=fill_value, dtype=dtype)
10531051
elif self._hasna:
1054-
data = self.copy()
1055-
data[self.isna()] = na_value
1056-
return np.asarray(data._pa_array, dtype=dtype)
1052+
data = self.fillna(na_value)
1053+
result = data._pa_array.to_numpy()
1054+
if dtype is not None:
1055+
result = result.astype(dtype, copy=False)
1056+
return result
10571057
else:
1058-
result = np.asarray(self._pa_array, dtype=dtype)
1058+
result = self._pa_array.to_numpy()
1059+
if dtype is not None:
1060+
result = result.astype(dtype, copy=False)
10591061
if copy:
10601062
result = result.copy()
1063+
return result
10611064
if self._hasna:
10621065
result[self.isna()] = na_value
10631066
return result

0 commit comments

Comments
 (0)