Skip to content
Closed
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
22 changes: 13 additions & 9 deletions python/pyarrow/tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3069,15 +3069,19 @@ def test_category(self):
v2 = [4, 5, 6, 7, 8]
v3 = [b'foo', None, b'bar', b'qux', np.nan]

cat_strings = pd.Categorical(v1 * repeats)
cat_strings_with_na = cat_strings.set_categories(['foo', 'bar'])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We are probably still getting into missing categories being silently converted to NaN here and Pandas is moving away from that AFAIU.

As the idea in the test is to have NaN in the constructed categorical array, we might simply remove this line as the cat_strings actually already includes them:

In [19]: pd.Categorical(v1 * repeats)
    ...: 
Out[19]: 
['foo', NaN, 'bar', 'qux', NaN, ..., 'foo', NaN, 'bar', 'qux', NaN]
Length: 25
Categories (3, str): ['bar', 'foo', 'qux']

What we can do is to add:

v0 = ['foo', 'bar', 'qux']

and use this for cat_strings? This way we do not have to look for a workaround where we use deprecated behavior to construct NaN values due to missing categories.


cat_strings_ordered = pd.Categorical(
v1 * repeats, categories=['bar', 'qux', 'foo'], ordered=True
)

arrays = {
'cat_strings': pd.Categorical(v1 * repeats),
'cat_strings_with_na': pd.Categorical(v1 * repeats,
categories=['foo', 'bar']),
'cat_strings': cat_strings,
'cat_strings_with_na': cat_strings_with_na,
'cat_ints': pd.Categorical(v2 * repeats),
'cat_binary': pd.Categorical(v3 * repeats),
'cat_strings_ordered': pd.Categorical(
v1 * repeats, categories=['bar', 'qux', 'foo'],
ordered=True),
'cat_strings_ordered': cat_strings_ordered,
'ints': v2 * repeats,
'ints2': v2 * repeats,
'strings': v1 * repeats,
Expand All @@ -3096,10 +3100,10 @@ def _check(v):
result = arr.to_pandas()
tm.assert_series_equal(pd.Series(result), pd.Series(v))

base = pd.Categorical(['a', 'b', 'c'])
arrays = [
pd.Categorical(['a', 'b', 'c'], categories=['a', 'b']),
pd.Categorical(['a', 'b', 'c'], categories=['a', 'b'],
ordered=True)
base.set_categories(['a', 'b']),
base.set_categories(['a', 'b']).as_ordered(),
Comment on lines -3100 to +3106

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it would be simpler to do:

pd.Categorical(['a', 'b', None], categories=['a', 'b'])

This should be the same use case as reported here #19704.

]
for arr in arrays:
_check(arr)
Expand Down
Loading