File tree Expand file tree Collapse file tree 2 files changed +8
-4
lines changed Expand file tree Collapse file tree 2 files changed +8
-4
lines changed Original file line number Diff line number Diff line change @@ -478,7 +478,7 @@ def maybe_cast_pointwise_result(
478
478
return result
479
479
480
480
481
- def maybe_cast_to_pyarrow_dtype (result : ArrayLike ) -> ArrayLike :
481
+ def maybe_cast_to_pyarrow_dtype (result : ArrayLike , obj_dtype : Dtype ) -> ArrayLike :
482
482
"""
483
483
Try casting result of a pointwise operation to its pyarrow dtype if
484
484
appropriate.
@@ -504,8 +504,12 @@ def maybe_cast_to_pyarrow_dtype(result: ArrayLike) -> ArrayLike:
504
504
from pandas .core .construction import array as pd_array
505
505
506
506
result [isna (result )] = np .nan
507
- pyarrow_result = pa .array (result )
508
- pandas_pyarrow_dtype = ArrowDtype (pyarrow_result .type )
507
+ if result .size == 0 or all (isna (result )):
508
+ pandas_pyarrow_dtype = obj_dtype
509
+ else :
510
+ pyarrow_result = pa .array (result )
511
+ pandas_pyarrow_dtype = ArrowDtype (pyarrow_result .type )
512
+
509
513
result = pd_array (result , dtype = pandas_pyarrow_dtype )
510
514
except (
511
515
ArrowNotImplementedError ,
Original file line number Diff line number Diff line change @@ -918,7 +918,7 @@ def agg_series(
918
918
919
919
result = self ._aggregate_series_pure_python (obj , func )
920
920
if isinstance (obj ._values , ArrowExtensionArray ):
921
- out = maybe_cast_to_pyarrow_dtype (result )
921
+ out = maybe_cast_to_pyarrow_dtype (result , obj . dtype )
922
922
return out
923
923
924
924
if not isinstance (obj ._values , np .ndarray ) and not isinstance (
You can’t perform that action at this time.
0 commit comments