Skip to content

Commit 2ae56c2

Browse files
committed
BUG: Fix GroupBy aggregate coersion of outputs inconsistency for pyarrow dtypes (#61636)
1 parent bc500f7 commit 2ae56c2

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

pandas/core/arrays/arrow/array.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ def floordiv_compat(
189189
ArrayLike,
190190
AxisInt,
191191
Dtype,
192+
DtypeObj,
192193
FillnaOptions,
193194
InterpolateOptions,
194195
Iterator,
@@ -308,6 +309,18 @@ def __init__(self, values: pa.Array | pa.ChunkedArray) -> None:
308309
)
309310
self._dtype = ArrowDtype(self._pa_array.type)
310311

312+
@classmethod
313+
def _from_scalars(cls, scalars, dtype: DtypeObj) -> Self:
314+
try:
315+
pa_array = cls._from_sequence(scalars, dtype=dtype)
316+
except pa.ArrowNotImplementedError:
317+
# _from_scalars should only raise ValueError or TypeError.
318+
raise ValueError
319+
320+
if lib.infer_dtype(scalars, skipna=True) != lib.infer_dtype(pa_array, skipna=True):
321+
raise ValueError
322+
return pa_array
323+
311324
@classmethod
312325
def _from_sequence(
313326
cls, scalars, *, dtype: Dtype | None = None, copy: bool = False

pandas/core/arrays/string_arrow.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
from pandas._typing import (
5454
ArrayLike,
5555
Dtype,
56+
DtypeObj,
5657
NpDtype,
5758
Scalar,
5859
npt,
@@ -190,6 +191,12 @@ def __len__(self) -> int:
190191
"""
191192
return len(self._pa_array)
192193

194+
@classmethod
195+
def _from_scalars(cls, scalars, dtype: DtypeObj) -> Self:
196+
if lib.infer_dtype(scalars, skipna=True) not in ["string", "empty"]:
197+
raise ValueError
198+
return cls._from_sequence(scalars, dtype=dtype)
199+
193200
@classmethod
194201
def _from_sequence(
195202
cls, scalars, *, dtype: Dtype | None = None, copy: bool = False

0 commit comments

Comments
 (0)