Skip to content

Commit ab9ee88

Browse files
author
Shashwati
committed
PYTHON: Fix pandas Categorical deprecation warnings in tests (GH-49255)
Replace pd.Categorical() calls that specify categories containing values not in the categories list with the recommended pattern: create the Categorical first, then use .set_categories() to restrict. Fixes deprecation warnings: - test_category: cat_strings_with_na - test_category_implicit_from_pandas: two Categorical instances Fixes #49255
1 parent 68d1368 commit ab9ee88

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

‎python/pyarrow/tests/test_pandas.py‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,15 +3069,19 @@ def test_category(self):
30693069
v2 = [4, 5, 6, 7, 8]
30703070
v3 = [b'foo', None, b'bar', b'qux', np.nan]
30713071

3072+
cat_strings = pd.Categorical(v1 * repeats)
3073+
cat_strings_with_na = cat_strings.set_categories(['foo', 'bar'])
3074+
3075+
cat_strings_ordered = pd.Categorical(
3076+
v1 * repeats, categories=['bar', 'qux', 'foo'], ordered=True
3077+
)
3078+
30723079
arrays = {
3073-
'cat_strings': pd.Categorical(v1 * repeats),
3074-
'cat_strings_with_na': pd.Categorical(v1 * repeats,
3075-
categories=['foo', 'bar']),
3080+
'cat_strings': cat_strings,
3081+
'cat_strings_with_na': cat_strings_with_na,
30763082
'cat_ints': pd.Categorical(v2 * repeats),
30773083
'cat_binary': pd.Categorical(v3 * repeats),
3078-
'cat_strings_ordered': pd.Categorical(
3079-
v1 * repeats, categories=['bar', 'qux', 'foo'],
3080-
ordered=True),
3084+
'cat_strings_ordered': cat_strings_ordered,
30813085
'ints': v2 * repeats,
30823086
'ints2': v2 * repeats,
30833087
'strings': v1 * repeats,
@@ -3096,10 +3100,10 @@ def _check(v):
30963100
result = arr.to_pandas()
30973101
tm.assert_series_equal(pd.Series(result), pd.Series(v))
30983102

3103+
base = pd.Categorical(['a', 'b', 'c'])
30993104
arrays = [
3100-
pd.Categorical(['a', 'b', 'c'], categories=['a', 'b']),
3101-
pd.Categorical(['a', 'b', 'c'], categories=['a', 'b'],
3102-
ordered=True)
3105+
base.set_categories(['a', 'b']),
3106+
base.set_categories(['a', 'b']).as_ordered(),
31033107
]
31043108
for arr in arrays:
31053109
_check(arr)

0 commit comments

Comments
 (0)