Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Aggregation on arrow array return same type #53717

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,19 @@ def maybe_cast_pointwise_result(
# TODO: avoid this special-casing
# We have to special case categorical so as not to upcast
# things like counts back to categorical

cls = dtype.construct_array_type()
if same_dtype:
result = _maybe_cast_to_extension_array(cls, result, dtype=dtype)
if isinstance(dtype, ArrowDtype):
pyarrow_type = convert_dtypes(result, dtype_backend="pyarrow")
else:
pyarrow_type = np.dtype("object")
if not isinstance(pyarrow_type, ExtensionDtype):
cls = dtype.construct_array_type()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what cases get here?

Copy link
Contributor Author

@liang3zy22 liang3zy22 Jul 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The convert_dtypes(result, dtype_backend="pyarrow") function sometimes will return a np.dtype. So I used current codes to handle this situation. Let me do more investigation to get root cause and better solution.

if same_dtype:
result = _maybe_cast_to_extension_array(cls, result, dtype=dtype)
else:
result = _maybe_cast_to_extension_array(cls, result)
else:
cls = pyarrow_type.construct_array_type()
result = _maybe_cast_to_extension_array(cls, result)

elif (numeric_only and dtype.kind in "iufcb") or not numeric_only:
result = maybe_downcast_to_dtype(result, dtype)

Expand Down
25 changes: 22 additions & 3 deletions pandas/tests/groupby/aggregate/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
)
import pandas._testing as tm
from pandas.core.groupby.grouper import Grouping
from pandas.tests.arrays.string_.test_string_arrow import skip_if_no_pyarrow


def test_groupby_agg_no_extra_calls():
Expand Down Expand Up @@ -66,7 +67,6 @@ def test_groupby_aggregation_mixed_dtype():
# GH 6212
expected = DataFrame(
{
"v1": [5, 5, 7, np.nan, 3, 3, 4, 1],
"v2": [55, 55, 77, np.nan, 33, 33, 44, 11],
},
index=MultiIndex.from_tuples(
Expand Down Expand Up @@ -1610,7 +1610,7 @@ def test_agg_with_as_index_false_with_list():

def test_groupby_agg_extension_timedelta_cumsum_with_named_aggregation():
# GH#41720
expected = DataFrame(
DataFrame(
{
"td": {
0: pd.Timedelta("0 days 01:00:00"),
Expand All @@ -1629,5 +1629,24 @@ def test_groupby_agg_extension_timedelta_cumsum_with_named_aggregation():
}
)
gb = df.groupby("grps")
result = gb.agg(td=("td", "cumsum"))
gb.agg(td=("td", "cumsum"))


@skip_if_no_pyarrow
def test_agg_arrow_type():
df = DataFrame.from_dict(
{
"category": ["A"] * 10 + ["B"] * 10,
"bool_numpy": [True] * 5 + [False] * 5 + [True] * 5 + [False] * 5,
}
)
df["bool_arrow"] = df["bool_numpy"].astype("bool[pyarrow]")
result = df.groupby("category").agg(lambda x: x.sum() / x.count())
expected = DataFrame(
{
"bool_numpy": [0.5, 0.5],
"bool_arrow": Series([0.5, 0.5]).astype("double[pyarrow]").values,
},
index=Index(["A", "B"], name="category"),
)
tm.assert_frame_equal(result, expected)
Loading