@@ -515,7 +515,7 @@ def is_string_or_object_np_dtype(dtype: np.dtype) -> bool:
515
515
"""
516
516
Faster alternative to is_string_dtype, assumes we have a np.dtype object.
517
517
"""
518
- return dtype == object or dtype .kind in "SU"
518
+ return dtype == object or dtype .kind in "SU" or issubclass ( dtype . type , str )
519
519
520
520
521
521
def is_string_dtype (arr_or_dtype ) -> bool :
@@ -1662,6 +1662,44 @@ def is_all_strings(value: ArrayLike) -> bool:
1662
1662
return dtype == "string"
1663
1663
1664
1664
1665
+ def is_legacy_string_dtype (arr_or_dtype , include_bytes = False ) -> bool :
1666
+ """Check if the dtype is a numpy legacy string dtype
1667
+
1668
+ Parameters
1669
+ ----------
1670
+ arr_or_dtype : array-like or dtype
1671
+ The array-like or dtype to check
1672
+
1673
+ include_bytes : boolean
1674
+ whether or not to include bytestring dtypes
1675
+
1676
+ Returns
1677
+ -------
1678
+ boolean
1679
+ True for legacy numpy dtypes that represent python strings,
1680
+ False otherwise. If include_bytes is True, also true for
1681
+ legacy bytes dtypes.
1682
+
1683
+ """
1684
+ if arr_or_dtype is None :
1685
+ return False
1686
+
1687
+ dtype = getattr (arr_or_dtype , "dtype" , arr_or_dtype )
1688
+
1689
+ if not isinstance (dtype , np .dtype ):
1690
+ return False
1691
+
1692
+ # the _legacy attribute was added in Numpy 1.25. If the attribute isn't
1693
+ # defined on the dtype class, Numpy isn't sufficiently new, so we have to be
1694
+ # dealing with a legacy dtype.
1695
+ is_legacy = getattr (type (dtype ), "_legacy" , True )
1696
+ if not is_legacy :
1697
+ return False
1698
+ if include_bytes :
1699
+ return issubclass (dtype .type , (str , bytes ))
1700
+ return issubclass (dtype .type , str )
1701
+
1702
+
1665
1703
__all__ = [
1666
1704
"classes" ,
1667
1705
"DT64NS_DTYPE" ,
@@ -1696,6 +1734,7 @@ def is_all_strings(value: ArrayLike) -> bool:
1696
1734
"is_interval" ,
1697
1735
"is_interval_dtype" ,
1698
1736
"is_iterator" ,
1737
+ "is_legacy_string_dtype" ,
1699
1738
"is_named_tuple" ,
1700
1739
"is_nested_list_like" ,
1701
1740
"is_number" ,
0 commit comments