|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from pandas.core.dtypes.common import is_string_dtype |
| 4 | +from pandas.core.dtypes.dtypes import CategoricalDtype |
| 5 | + |
| 6 | +import pandas as pd |
| 7 | + |
| 8 | + |
| 9 | +def test_is_string_dtype_categorical_consistency(): |
| 10 | + """Test that is_string_dtype returns consistent results for |
| 11 | + Categorical series and dtype.""" |
| 12 | + # Test with CategoricalDtype directly |
| 13 | + categorical_dtype = CategoricalDtype() |
| 14 | + assert not is_string_dtype(categorical_dtype) |
| 15 | + |
| 16 | + # Test with Series containing Categorical |
| 17 | + categorical_series = pd.Series(pd.Categorical(["a", "b", "c"])) |
| 18 | + assert not is_string_dtype(categorical_series) |
| 19 | + |
| 20 | + # Test with ordered CategoricalDtype |
| 21 | + ordered_categorical_dtype = CategoricalDtype(ordered=True) |
| 22 | + assert not is_string_dtype(ordered_categorical_dtype) |
| 23 | + |
| 24 | + # Test with Series containing ordered Categorical |
| 25 | + ordered_categorical_series = pd.Series( |
| 26 | + pd.Categorical(["a", "b", "c"], ordered=True) |
| 27 | + ) |
| 28 | + assert not is_string_dtype(ordered_categorical_series) |
| 29 | + |
| 30 | + # Test with CategoricalDtype with specific categories |
| 31 | + specific_categorical_dtype = CategoricalDtype(categories=["x", "y", "z"]) |
| 32 | + assert not is_string_dtype(specific_categorical_dtype) |
| 33 | + |
| 34 | + # Test with Series containing Categorical with specific categories |
| 35 | + specific_categorical_series = pd.Series( |
| 36 | + pd.Categorical(["x", "y", "z"], categories=["x", "y", "z"]) |
| 37 | + ) |
| 38 | + assert not is_string_dtype(specific_categorical_series) |
| 39 | + |
| 40 | + # Test with empty Categorical |
| 41 | + empty_categorical = pd.Series(pd.Categorical([])) |
| 42 | + assert not is_string_dtype(empty_categorical) |
| 43 | + |
| 44 | + # Test with Categorical containing NaN values |
| 45 | + nan_categorical = pd.Series(pd.Categorical([np.nan, "a", "b"])) |
| 46 | + assert not is_string_dtype(nan_categorical) |
| 47 | + |
| 48 | + # Test with numeric Categorical |
| 49 | + numeric_categorical = pd.Series(pd.Categorical([1, 2, 3])) |
| 50 | + assert not is_string_dtype(numeric_categorical) |
0 commit comments