File tree Expand file tree Collapse file tree 2 files changed +16
-9
lines changed Expand file tree Collapse file tree 2 files changed +16
-9
lines changed Original file line number Diff line number Diff line change @@ -478,9 +478,7 @@ def maybe_cast_pointwise_result(
478
478
return result
479
479
480
480
481
- def maybe_cast_to_pyarrow_dtype (
482
- result : ArrayLike , converted_result : ArrayLike
483
- ) -> ArrayLike :
481
+ def maybe_cast_to_pyarrow_dtype (result : ArrayLike ) -> ArrayLike :
484
482
"""
485
483
Try casting result of a pointwise operation to its pyarrow dtype if
486
484
appropriate.
@@ -499,6 +497,7 @@ def maybe_cast_to_pyarrow_dtype(
499
497
import pyarrow as pa
500
498
from pyarrow import (
501
499
ArrowInvalid ,
500
+ ArrowMemoryError ,
502
501
ArrowNotImplementedError ,
503
502
)
504
503
@@ -508,8 +507,14 @@ def maybe_cast_to_pyarrow_dtype(
508
507
pyarrow_result = pa .array (result )
509
508
pandas_pyarrow_dtype = ArrowDtype (pyarrow_result .type )
510
509
result = pd_array (result , dtype = pandas_pyarrow_dtype )
511
- except (ArrowNotImplementedError , ArrowInvalid ):
512
- return converted_result
510
+ except (
511
+ ArrowNotImplementedError ,
512
+ ArrowInvalid ,
513
+ ArrowMemoryError ,
514
+ TypeError ,
515
+ ValueError ,
516
+ ):
517
+ result = lib .maybe_convert_objects (result , try_float = False )
513
518
514
519
return result
515
520
Original file line number Diff line number Diff line change @@ -916,6 +916,11 @@ def agg_series(
916
916
np.ndarray or ExtensionArray
917
917
"""
918
918
919
+ result = self ._aggregate_series_pure_python (obj , func )
920
+ if isinstance (obj ._values , ArrowExtensionArray ):
921
+ out = maybe_cast_to_pyarrow_dtype (result )
922
+ return out
923
+
919
924
if not isinstance (obj ._values , np .ndarray ) and not isinstance (
920
925
obj ._values , ArrowExtensionArray
921
926
):
@@ -925,15 +930,12 @@ def agg_series(
925
930
# is sufficiently strict that it casts appropriately.
926
931
preserve_dtype = True
927
932
928
- result = self ._aggregate_series_pure_python (obj , func )
929
933
npvalues = lib .maybe_convert_objects (result , try_float = False )
930
-
931
934
if preserve_dtype :
932
935
out = maybe_cast_pointwise_result (npvalues , obj .dtype , numeric_only = True )
933
- elif isinstance (obj ._values , ArrowExtensionArray ):
934
- out = maybe_cast_to_pyarrow_dtype (result , npvalues )
935
936
else :
936
937
out = npvalues
938
+
937
939
return out
938
940
939
941
@final
You can’t perform that action at this time.
0 commit comments