Skip to content

Commit aae6474

Browse files
committed
fix
1 parent ce66719 commit aae6474

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

pandas/core/dtypes/common.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,12 +636,17 @@ def is_string_dtype(arr_or_dtype) -> bool:
636636
False
637637
"""
638638
# Handle Categorical consistently whether passed as array or dtype
639-
if hasattr(arr_or_dtype, "dtype") and isinstance(_get_dtype(arr_or_dtype), CategoricalDtype):
639+
if hasattr(arr_or_dtype, "dtype") and isinstance(
640+
_get_dtype(arr_or_dtype), CategoricalDtype
641+
):
640642
return is_all_strings(arr_or_dtype)
641643
elif isinstance(arr_or_dtype, CategoricalDtype):
642644
# For CategoricalDtype, check if categories are strings
645+
# Handle case where categories is None
646+
if arr_or_dtype.categories is None:
647+
return False
643648
return arr_or_dtype.categories.inferred_type == "string"
644-
649+
645650
if hasattr(arr_or_dtype, "dtype") and _get_dtype(arr_or_dtype).kind == "O":
646651
return is_all_strings(arr_or_dtype)
647652

@@ -1907,6 +1912,9 @@ def is_all_strings(value: ArrayLike) -> bool:
19071912
np.asarray(value), skipna=False
19081913
)
19091914
elif isinstance(dtype, CategoricalDtype):
1915+
# Handle case where categories is None
1916+
if dtype.categories is None:
1917+
return False
19101918
return dtype.categories.inferred_type == "string"
19111919
return dtype == "string"
19121920

pandas/core/indexes/datetimelike.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ def equals(self, other: Any) -> bool:
180180
should_try = other.inferred_type in inferable
181181
elif isinstance(other.dtype, CategoricalDtype):
182182
other = cast("CategoricalIndex", other)
183-
should_try = other.categories.inferred_type in inferable
183+
# Handle case where categories is None
184+
if other.categories is not None:
185+
should_try = other.categories.inferred_type in inferable
186+
else:
187+
should_try = False
184188

185189
if should_try:
186190
try:

pandas/tests/dtypes/test_common.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,15 +339,15 @@ def test_is_string_dtype_nullable(nullable_string_dtype):
339339

340340
def test_is_string_dtype_categorical():
341341
# GH#XXXXX - is_string_dtype should be consistent for Categorical series and dtype
342-
cat_series = pd.Categorical(['A', 'B', 'C'])
342+
cat_series = pd.Categorical(["A", "B", "C"])
343343
assert not com.is_string_dtype(cat_series)
344344
assert not com.is_string_dtype(cat_series.dtype)
345-
345+
346346
# Test with string categories
347-
cat_string_series = pd.Categorical(['A', 'B', 'C'], categories=['A', 'B', 'C'])
347+
cat_string_series = pd.Categorical(["A", "B", "C"], categories=["A", "B", "C"])
348348
assert com.is_string_dtype(cat_string_series)
349349
assert com.is_string_dtype(cat_string_series.dtype)
350-
350+
351351
# Test with non-string categories
352352
cat_int_series = pd.Categorical([1, 2, 3], categories=[1, 2, 3])
353353
assert not com.is_string_dtype(cat_int_series)

pandas/tests/frame/test_query_eval.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def test_query_duplicate_column_name(self, engine, parser):
168168
}
169169
).rename(columns={"B": "A"})
170170

171-
res = df.query('C == 1', engine=engine, parser=parser)
171+
res = df.query("C == 1", engine=engine, parser=parser)
172172

173173
expect = DataFrame(
174174
[[1, 1, 1]],
@@ -1411,7 +1411,7 @@ def test_expr_with_column_name_with_backtick_and_hash(self):
14111411
def test_expr_with_column_name_with_backtick(self):
14121412
# GH 59285
14131413
df = DataFrame({"a`b": (1, 2, 3), "ab": (4, 5, 6)})
1414-
result = df.query("`a``b` < 2") # noqa
1414+
result = df.query("`a``b` < 2")
14151415
# Note: Formatting checks may wrongly consider the above ``inline code``.
14161416
expected = df[df["a`b"] < 2]
14171417
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)