Skip to content

API: ArrowExtensionArray.value_counts returns pyarrow.int64 type #51542

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

Merged
merged 6 commits into from
Feb 24, 2023

Conversation

mroeschke
Copy link
Member

@mroeschke mroeschke commented Feb 21, 2023

@mroeschke mroeschke added the Arrow pyarrow functionality label Feb 21, 2023
@mroeschke mroeschke added this to the 2.0 milestone Feb 21, 2023
@@ -873,6 +873,9 @@ def value_counts(
result.name = name
result.index.name = index_name
counts = result._values
if not isinstance(counts, np.ndarray):
# e.g. ArrowExtensionArray
counts = np.asarray(counts)
Copy link
Member

Choose a reason for hiding this comment

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

why is this needed? id expect pd.value_counts to behave like obj.value_counts

Copy link
Member Author

Choose a reason for hiding this comment

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

For normalize below, counts.sum() is called and ._values which is an ArrowExtensionArray doesn't have sum.

I think it was assumed that counts is a numpy array here?

Copy link
Member Author

Choose a reason for hiding this comment

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

If EA's implement value_counts they should also handle normalize?

Copy link
Member

Choose a reason for hiding this comment

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

I think it was assumed that counts is a numpy array here?

For a long time I wanted value_counts to have int64 dtype and get rid of Int64 cases, but eventually gave up on that. So i think at least Int64 is possible. IIRC value_counts is only kinda-sorta part of the EA interface so the exact requirements aren't pinned down.

If EA's implement value_counts they should also handle normalize?

If the normalization logic can live exclusively here, I think I'd prefer that.

which is an ArrowExtensionArray doesn't have sum.

Out of scope, but I'd just put sum and the other most-basic reductions (say, all the ones in the np.ndarray namespace) on EA and let it raise TypeError for non-supported dtypes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Would you prefer me to put sum on ArrowExtensionArray so this works without the if statement or should we leave in this check?

Copy link
Member

Choose a reason for hiding this comment

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

i guess cleaner to leave this and can do another api change later if called for


result = ser.value_counts(normalize=True).sort_index()

if not isinstance(data, pd.Categorical):
Copy link
Member

Choose a reason for hiding this comment

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

we can get here with Categorical?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, yeah we can't. This is a copy/paste from the base class. Removed

@mroeschke
Copy link
Member Author

Greenish

Copy link
Member

@jbrockmendel jbrockmendel left a comment

Choose a reason for hiding this comment

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

LGTM

@jbrockmendel jbrockmendel merged commit 21b019f into pandas-dev:main Feb 24, 2023
@jbrockmendel
Copy link
Member

thanks @mroeschke

meeseeksmachine pushed a commit to meeseeksmachine/pandas that referenced this pull request Feb 24, 2023
@mroeschke mroeschke deleted the api/arrow/value_counts branch February 24, 2023 22:22
mroeschke added a commit that referenced this pull request Feb 25, 2023
…unts returns pyarrow.int64 type) (#51620)

Backport PR #51542: API: ArrowExtensionArray.value_counts returns pyarrow.int64 type

Co-authored-by: Matthew Roeschke <[email protected]>
mroeschke added a commit that referenced this pull request Jun 27, 2024
* note breaking change in json_normalize retaining index

For context: #51542 & #57422

* Update doc/source/whatsnew/v3.0.0.rst

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Matthew Roeschke <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arrow pyarrow functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API: ArrowExtensionArray.value_counts should return int64[pyarrow] instead of Int64
2 participants