Skip to content

Commit b72d4f8

Browse files
committed
BUG: Aggregation on arrow array return same type.
Signed-off-by: Liang Yan <[email protected]>
1 parent 641427e commit b72d4f8

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

pandas/core/groupby/ops.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
if TYPE_CHECKING:
7474
from pandas.core.generic import NDFrame
7575

76+
from pandas.arrays import ArrowExtensionArray
77+
7678

7779
def check_result_array(obj, dtype):
7880
# Our operation is supposed to be an aggregation/reduction. If
@@ -837,7 +839,11 @@ def agg_series(
837839
# test_groupby_empty_with_category gets here with self.ngroups == 0
838840
# and len(obj) > 0
839841

840-
if len(obj) > 0 and not isinstance(obj._values, np.ndarray):
842+
if (
843+
len(obj) > 0
844+
and not isinstance(obj._values, np.ndarray)
845+
and not isinstance(obj._values, ArrowExtensionArray)
846+
):
841847
# we can preserve a little bit more aggressively with EA dtype
842848
# because maybe_cast_pointwise_result will do a try/except
843849
# with _from_sequence. NB we are assuming here that _from_sequence

pandas/tests/groupby/aggregate/test_aggregate.py

+17
Original file line numberDiff line numberDiff line change
@@ -1603,3 +1603,20 @@ def test_agg_with_as_index_false_with_list():
16031603
columns=MultiIndex.from_tuples([("a1", ""), ("a2", ""), ("b", "sum")]),
16041604
)
16051605
tm.assert_frame_equal(result, expected)
1606+
1607+
1608+
def test_agg_arrow_type():
1609+
df = DataFrame.from_dict(
1610+
{
1611+
"category": ["A"] * 10 + ["B"] * 10,
1612+
"bool_numpy": [True] * 5 + [False] * 5 + [True] * 5 + [False] * 5,
1613+
}
1614+
)
1615+
df["bool_arrow"] = df["bool_numpy"].astype("bool[pyarrow]")
1616+
result = df.groupby("category").agg(lambda x: x.sum() / x.count())
1617+
expected = DataFrame(
1618+
[[0.5, 0.5], [0.5, 0.5]],
1619+
columns=["bool_numpy", "bool_arrow"],
1620+
index=Index(["A", "B"], name="category"),
1621+
)
1622+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)