Skip to content

Commit 43f3dde

Browse files
authored
REF: avoid special-casing in agg_series (#56540)
1 parent 4ec987b commit 43f3dde

File tree

4 files changed

+8
-19
lines changed

4 files changed

+8
-19
lines changed

pandas/core/arrays/arrow/array.py

-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
is_integer,
4141
is_list_like,
4242
is_scalar,
43-
pandas_dtype,
4443
)
4544
from pandas.core.dtypes.dtypes import DatetimeTZDtype
4645
from pandas.core.dtypes.missing import isna
@@ -275,10 +274,6 @@ def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy: bool = Fal
275274
"""
276275
Construct a new ExtensionArray from a sequence of scalars.
277276
"""
278-
if dtype is not None and isinstance(dtype, str):
279-
# FIXME: in tests.extension.test_arrow we pass pyarrow _type_ objects
280-
# which raise when passed to pandas_dtype
281-
dtype = pandas_dtype(dtype)
282277
pa_type = to_pyarrow_type(dtype)
283278
pa_array = cls._box_pa_array(scalars, pa_type=pa_type, copy=copy)
284279
arr = cls(pa_array)

pandas/core/arrays/string_.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def tolist(self):
264264

265265
@classmethod
266266
def _from_scalars(cls, scalars, dtype: DtypeObj) -> Self:
267-
if lib.infer_dtype(scalars, skipna=True) != "string":
267+
if lib.infer_dtype(scalars, skipna=True) not in ["string", "empty"]:
268268
# TODO: require any NAs be valid-for-string
269269
raise ValueError
270270
return cls._from_sequence(scalars, dtype=dtype)

pandas/core/groupby/groupby.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class providing the base-class of operations.
8686
is_object_dtype,
8787
is_scalar,
8888
needs_i8_conversion,
89+
pandas_dtype,
8990
)
9091
from pandas.core.dtypes.missing import (
9192
isna,
@@ -2330,7 +2331,8 @@ def hfunc(bvalues: ArrayLike) -> ArrayLike:
23302331
elif isinstance(bvalues, ArrowExtensionArray) and not isinstance(
23312332
bvalues.dtype, StringDtype
23322333
):
2333-
return type(bvalues)._from_sequence(counted[0], dtype="int64[pyarrow]")
2334+
dtype = pandas_dtype("int64[pyarrow]")
2335+
return type(bvalues)._from_sequence(counted[0], dtype=dtype)
23342336
if is_series:
23352337
assert counted.ndim == 2
23362338
assert counted.shape[0] == 1

pandas/core/groupby/ops.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
from pandas.util._decorators import cache_readonly
3636
from pandas.util._exceptions import find_stack_level
3737

38-
from pandas.core.dtypes.base import ExtensionDtype
3938
from pandas.core.dtypes.cast import (
4039
maybe_cast_pointwise_result,
4140
maybe_downcast_to_dtype,
@@ -885,18 +884,11 @@ def agg_series(
885884

886885
result = self._aggregate_series_pure_python(obj, func)
887886

888-
if len(obj) == 0 and len(result) == 0 and isinstance(obj.dtype, ExtensionDtype):
889-
cls = obj.dtype.construct_array_type()
890-
out = cls._from_sequence(result)
891-
887+
npvalues = lib.maybe_convert_objects(result, try_float=False)
888+
if preserve_dtype:
889+
out = maybe_cast_pointwise_result(npvalues, obj.dtype, numeric_only=True)
892890
else:
893-
npvalues = lib.maybe_convert_objects(result, try_float=False)
894-
if preserve_dtype:
895-
out = maybe_cast_pointwise_result(
896-
npvalues, obj.dtype, numeric_only=True
897-
)
898-
else:
899-
out = npvalues
891+
out = npvalues
900892
return out
901893

902894
@final

0 commit comments

Comments
 (0)