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: value_counts() check Index with PyArrow categorical columns #60569

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,10 @@ def __init__(
if isinstance(values.dtype, ArrowDtype) and issubclass(
values.dtype.type, CategoricalDtypeType
):
arr = values._pa_array.combine_chunks()
if values.__class__.__name__ == 'Index':
Copy link
Member

Choose a reason for hiding this comment

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

Use isinstance instead of checking the name.

arr = values._data._pa_array.combine_chunks()
else:
arr = values._pa_array.combine_chunks()
categories = arr.dictionary.to_pandas(types_mapper=ArrowDtype)
codes = arr.indices.to_numpy()
dtype = CategoricalDtype(categories, values.dtype.pyarrow_dtype.ordered)
Expand Down
20 changes: 19 additions & 1 deletion pandas/tests/arrays/categorical/test_dtypes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import numpy as np
import pyarrow as pa
import pytest

from pandas.core.dtypes.dtypes import CategoricalDtype
from pandas.core.dtypes.dtypes import (
ArrowDtype,
CategoricalDtype,
)

from pandas import (
Categorical,
Expand Down Expand Up @@ -136,3 +140,17 @@ def test_interval_index_category(self):
[0, 1], [1, 2], dtype="interval[uint64, right]"
)
tm.assert_index_equal(result, expected)

def test_values_is_index():
# GH 60563
values = Index(["a1", "a2"], dtype=ArrowDtype(pa.string()))
arr = values._data._pa_array.combine_chunks()

assert arr.equals(values._data._pa_array.combine_chunks())
Copy link
Member

Choose a reason for hiding this comment

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

What is this testing?


def test_values_is_not_index():
# GH 60563
values = Series(["a1", "a2"], dtype=ArrowDtype(pa.string()))
arr = values._pa_array.combine_chunks()

assert arr.equals(values._pa_array.combine_chunks())
Loading