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: groupby.agg with udf incorrectly inferring pyarrow timestamp dtype #49163

Closed
jbrockmendel opened this issue Oct 18, 2022 · 0 comments · Fixed by #53089
Closed

BUG: groupby.agg with udf incorrectly inferring pyarrow timestamp dtype #49163

jbrockmendel opened this issue Oct 18, 2022 · 0 comments · Fixed by #53089
Labels
Apply Apply, Aggregate, Transform, Map Arrow pyarrow functionality Bug Dtype Conversions Unexpected or buggy dtype conversions Groupby

Comments

@jbrockmendel
Copy link
Member

jbrockmendel commented Oct 18, 2022

# based on test_agg_tzaware_non_datetime_result
import pyarrow as pa
import pandas as pd

dtype = pd.ArrowDtype(pa.timestamp(unit="ns", tz="UTC"))
dti = pd.date_range("2012-01-01", periods=4, tz="UTC").astype(dtype)
df = pd.DataFrame({"a": [0, 0, 1, 1], "b": dti})

gb = df.groupby("a")

result = gb["b"].agg(lambda x: x.iloc[0].year)
expected = pd.Series([2012, 2012], name="b")
expected.index.name = "a"
tm.assert_series_equal(result, expected)  # <- raises

The agg call does dtype inference on the result by calling maybe_cast_pointwise_result, which has special-casing (which we've wanted to get rid of for a while) for pd.DatetimeTZDtype.

AFAICT the way to fix this is to make _from_sequence stricter xref #33254.

@jbrockmendel jbrockmendel added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 18, 2022
@rhshadrach rhshadrach added Groupby Apply Apply, Aggregate, Transform, Map Dtype Conversions Unexpected or buggy dtype conversions and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 20, 2022
@jbrockmendel jbrockmendel added the Arrow pyarrow functionality label May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Arrow pyarrow functionality Bug Dtype Conversions Unexpected or buggy dtype conversions Groupby
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants