Skip to content

Commit

Permalink
BUG: is_string_dtype(pd.Index([], dtype='O')) returns False (#54997)
Browse files Browse the repository at this point in the history
* correct def is_all_strings

* add a test, correct whatsnew/v2.2.0.rst

* move the test and add parametrization, correct v2.2.0.rst

* remove the line from v2.2.0.rst

* add the note to v2.1.1.rst

* correct the whatsnew note and move it to 2.2.0
  • Loading branch information
natmokval authored Oct 4, 2023
1 parent da01e38 commit 06cdcc0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ Bug fixes
~~~~~~~~~
- Bug in :class:`AbstractHolidayCalendar` where timezone data was not propagated when computing holiday observances (:issue:`54580`)
- Bug in :class:`pandas.core.window.Rolling` where duplicate datetimelike indexes are treated as consecutive rather than equal with ``closed='left'`` and ``closed='neither'`` (:issue:`20712`)
- Bug in :func:`pandas.api.types.is_string_dtype` while checking object array with no elements is of the string dtype (:issue:`54661`)
- Bug in :meth:`DataFrame.apply` where passing ``raw=True`` ignored ``args`` passed to the applied function (:issue:`55009`)
- Bug in :meth:`pandas.DataFrame.melt` where it would not preserve the datetime (:issue:`55254`)
- Bug in :meth:`pandas.read_excel` with a ODS file without cached formatted cell for float values (:issue:`55219`)
Expand Down
9 changes: 6 additions & 3 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1664,9 +1664,12 @@ def is_all_strings(value: ArrayLike) -> bool:
dtype = value.dtype

if isinstance(dtype, np.dtype):
return dtype == np.dtype("object") and lib.is_string_array(
np.asarray(value), skipna=False
)
if len(value) == 0:
return dtype == np.dtype("object")
else:
return dtype == np.dtype("object") and lib.is_string_array(
np.asarray(value), skipna=False
)
elif isinstance(dtype, CategoricalDtype):
return dtype.categories.inferred_type == "string"
return dtype == "string"
Expand Down
25 changes: 17 additions & 8 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,23 @@ def test_is_categorical_dtype():
assert com.is_categorical_dtype(pd.CategoricalIndex([1, 2, 3]))


def test_is_string_dtype():
assert not com.is_string_dtype(int)
assert not com.is_string_dtype(pd.Series([1, 2]))

assert com.is_string_dtype(str)
assert com.is_string_dtype(object)
assert com.is_string_dtype(np.array(["a", "b"]))
assert com.is_string_dtype(pd.StringDtype())
@pytest.mark.parametrize(
"dtype, expected",
[
(int, False),
(pd.Series([1, 2]), False),
(str, True),
(object, True),
(np.array(["a", "b"]), True),
(pd.StringDtype(), True),
(pd.Index([], dtype="O"), True),
],
)
def test_is_string_dtype(dtype, expected):
# GH#54661

result = com.is_string_dtype(dtype)
assert result is expected


@pytest.mark.parametrize(
Expand Down

0 comments on commit 06cdcc0

Please sign in to comment.