Skip to content

Commit 05470fc

Browse files
committed
BUG: Fix GroupBy aggregate coersion of outputs inconsistency for pyarrow dtypes (#61636)
1 parent 5909621 commit 05470fc

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
@@ -185,6 +185,7 @@ def floordiv_compat(
185185
ArrayLike,
186186
AxisInt,
187187
Dtype,
188+
DtypeObj,
188189
FillnaOptions,
189190
InterpolateOptions,
190191
Iterator,
@@ -313,6 +314,18 @@ def __init__(self, values: pa.Array | pa.ChunkedArray) -> None:
313314
)
314315
self._dtype = ArrowDtype(self._pa_array.type)
315316

317+
@classmethod
318+
def _from_scalars(cls, scalars, dtype: DtypeObj) -> Self:
319+
try:
320+
pa_array = cls._from_sequence(scalars, dtype=dtype)
321+
except pa.ArrowNotImplementedError:
322+
# _from_scalars should only raise ValueError or TypeError.
323+
raise ValueError
324+
325+
if lib.infer_dtype(scalars, skipna=True) != lib.infer_dtype(pa_array, skipna=True):
326+
raise ValueError
327+
return pa_array
328+
316329
@classmethod
317330
def _from_sequence(
318331
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
Self,
5859
npt,
@@ -180,6 +181,12 @@ def __len__(self) -> int:
180181
"""
181182
return len(self._pa_array)
182183

184+
@classmethod
185+
def _from_scalars(cls, scalars, dtype: DtypeObj) -> Self:
186+
if lib.infer_dtype(scalars, skipna=True) not in ["string", "empty"]:
187+
raise ValueError
188+
return cls._from_sequence(scalars, dtype=dtype)
189+
183190
@classmethod
184191
def _from_sequence(
185192
cls, scalars, *, dtype: Dtype | None = None, copy: bool = False

0 commit comments

Comments
 (0)