|
5 | 5 |
|
6 | 6 | from pandas.core.dtypes.dtypes import CategoricalDtype
|
7 | 7 |
|
8 |
| -from pandas import Categorical, Index, Series, isna |
| 8 | +import pandas as pd |
| 9 | +from pandas import Categorical, DataFrame, Index, Series, isna |
9 | 10 | import pandas._testing as tm
|
10 | 11 |
|
11 | 12 |
|
@@ -82,3 +83,53 @@ def test_fillna_iterable_category(self, named):
|
82 | 83 | expected = Categorical([Point(0, 0), Point(0, 1), Point(0, 0)])
|
83 | 84 |
|
84 | 85 | tm.assert_categorical_equal(result, expected)
|
| 86 | + |
| 87 | + @pytest.mark.parametrize( |
| 88 | + "values, expected", |
| 89 | + [ |
| 90 | + ([1, 2, 3], np.array([False, False, False])), |
| 91 | + ([1, 2, np.nan], np.array([False, False, True])), |
| 92 | + ([1, 2, np.inf], np.array([False, False, True])), |
| 93 | + ([1, 2, pd.NA], np.array([False, False, True])), |
| 94 | + ], |
| 95 | + ) |
| 96 | + def test_use_inf_as_na(self, values, expected): |
| 97 | + # https://github.com/pandas-dev/pandas/issues/33594 |
| 98 | + with pd.option_context("mode.use_inf_as_na", True): |
| 99 | + cat = Categorical(values) |
| 100 | + result = cat.isna() |
| 101 | + tm.assert_numpy_array_equal(result, expected) |
| 102 | + |
| 103 | + result = Series(cat).isna() |
| 104 | + expected = Series(expected) |
| 105 | + tm.assert_series_equal(result, expected) |
| 106 | + |
| 107 | + result = DataFrame(cat).isna() |
| 108 | + expected = DataFrame(expected) |
| 109 | + tm.assert_frame_equal(result, expected) |
| 110 | + |
| 111 | + @pytest.mark.parametrize( |
| 112 | + "values, expected", |
| 113 | + [ |
| 114 | + ([1, 2, 3], np.array([False, False, False])), |
| 115 | + ([1, 2, np.nan], np.array([False, False, True])), |
| 116 | + ([1, 2, np.inf], np.array([False, False, True])), |
| 117 | + ([1, 2, pd.NA], np.array([False, False, True])), |
| 118 | + ], |
| 119 | + ) |
| 120 | + def test_use_inf_as_na_outside_context(self, values, expected): |
| 121 | + # https://github.com/pandas-dev/pandas/issues/33594 |
| 122 | + # Using isna directly for Categorical will fail in general here |
| 123 | + cat = Categorical(values) |
| 124 | + |
| 125 | + with pd.option_context("mode.use_inf_as_na", True): |
| 126 | + result = pd.isna(cat) |
| 127 | + tm.assert_numpy_array_equal(result, expected) |
| 128 | + |
| 129 | + result = pd.isna(Series(cat)) |
| 130 | + expected = Series(expected) |
| 131 | + tm.assert_series_equal(result, expected) |
| 132 | + |
| 133 | + result = pd.isna(DataFrame(cat)) |
| 134 | + expected = DataFrame(expected) |
| 135 | + tm.assert_frame_equal(result, expected) |
0 commit comments