Skip to content

[ArrowStringArray] fix test_value_counts_na #41002

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 1 commit into from
Apr 19, 2021
Merged
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
15 changes: 10 additions & 5 deletions pandas/core/arrays/string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,13 +675,18 @@ def value_counts(self, dropna: bool = True) -> Series:

vc = self._data.value_counts()

# Index cannot hold ExtensionArrays yet
index = Index(type(self)(vc.field(0)).astype(object))
values = vc.field(0)
counts = vc.field(1)
if dropna and self._data.null_count > 0:
mask = values.is_valid()
values = values.filter(mask)
counts = counts.filter(mask)

# No missing values so we can adhere to the interface and return a numpy array.
counts = np.array(vc.field(1))
counts = np.array(counts)

if dropna and self._data.null_count > 0:
raise NotImplementedError("yo")
# Index cannot hold ExtensionArrays yet
index = Index(type(self)(values)).astype(object)

return Series(counts, index=index).astype("Int64")

Expand Down
14 changes: 2 additions & 12 deletions pandas/tests/arrays/string_/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,7 @@ def test_arrow_roundtrip(dtype, dtype_object):
assert result.loc[2, "a"] is pd.NA


def test_value_counts_na(dtype, request):
if dtype == "arrow_string":
reason = "TypeError: boolean value of NA is ambiguous"
mark = pytest.mark.xfail(reason=reason)
request.node.add_marker(mark)

def test_value_counts_na(dtype):
arr = pd.array(["a", "b", "a", pd.NA], dtype=dtype)
result = arr.value_counts(dropna=False)
expected = pd.Series([2, 1, 1], index=["a", "b", pd.NA], dtype="Int64")
Expand All @@ -492,12 +487,7 @@ def test_value_counts_na(dtype, request):
tm.assert_series_equal(result, expected)


def test_value_counts_with_normalize(dtype, request):
if dtype == "arrow_string":
reason = "TypeError: boolean value of NA is ambiguous"
mark = pytest.mark.xfail(reason=reason)
request.node.add_marker(mark)

def test_value_counts_with_normalize(dtype):
s = pd.Series(["a", "b", "a", pd.NA], dtype=dtype)
result = s.value_counts(normalize=True)
expected = pd.Series([2, 1], index=["a", "b"], dtype="Float64") / 3
Expand Down