Skip to content

pandas/object-dtype-check

Pack Severity Fix Versions
pandas error unsafe, with xpd check --fix --unsafe-fixes All versions

pandas 3 stores text in the str dtype, so a check for object is False for a text column. Use pd.api.types.is_string_dtype.

is_text = df["name"].dtype == object
is_text = pd.api.types.is_string_dtype(df["name"])

The rule reports df["k"].dtype == object, select_dtypes(include="object"), is_object_dtype(df["k"]), df["k"].dtype in ("object", "category") and df["k"].dtype.name == "object", which are False for a text column, because pandas 3 stores text in the str dtype. It leaves dtype.kind == "O" alone, which is still True for a text column.

xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.

The unsafe fix writes pd.api.types.is_string_dtype(df["k"]) for dtype == object, is_object_dtype and dtype.name == "object", and ["object", "str"] in select_dtypes, which selects the same columns on pandas 3 and raises TypeError on pandas 2. dtypes == object, a test with in, and any other spelling of object in select_dtypes, such as np.dtype("O"), have no fix.

is_string_dtype returns false for an object column of mixed values. The rule leaves a select_dtypes call alone when its list already names both object and "str".

Text columns have dtype str in pandas 3, so this comparison with object is False for them. Write pd.api.types.is_string_dtype($X).

See all rules in the pandas pack.