diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index 4ecf9e1a06f4e..10249c338c16d 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -32,6 +32,7 @@ npt, ) from pandas.compat._optional import import_optional_dependency +from pandas.compat.numpy import np_version_gt2 from pandas.core.dtypes.common import ( is_complex, @@ -155,7 +156,7 @@ def _bn_ok_dtype(dtype: DtypeObj, name: str) -> bool: # Bottleneck chokes on datetime64, PeriodDtype (or and EA) if ( dtype != object - and dtype != np.dtypes.StringDType(na_object=libmissing.NA) + and (np_version_gt2 and dtype != np.dtypes.StringDType(na_object=libmissing.NA)) and not needs_i8_conversion(dtype) ): # GH 42878 diff --git a/pandas/tests/indexes/test_numpy_compat.py b/pandas/tests/indexes/test_numpy_compat.py index ace78d77350cb..a28c286f025f1 100644 --- a/pandas/tests/indexes/test_numpy_compat.py +++ b/pandas/tests/indexes/test_numpy_compat.py @@ -124,6 +124,10 @@ def test_numpy_ufuncs_other(index, func): with tm.external_error_raised(TypeError): func(index) + elif index.dtype == "string[python]" and func is np.isnan: + with tm.external_error_raised(ValueError): + func(index) + elif is_numeric_dtype(index) and not ( is_complex_dtype(index) and func is np.signbit ): diff --git a/pandas/tests/series/test_reductions.py b/pandas/tests/series/test_reductions.py index 0bc3092d30b43..02922ef685e47 100644 --- a/pandas/tests/series/test_reductions.py +++ b/pandas/tests/series/test_reductions.py @@ -191,10 +191,10 @@ def test_mean_dont_convert_j_to_complex(): with pytest.raises(TypeError, match=msg): df.agg("mean") - msg = "Could not convert string 'J' to numeric|does not support" + msg = "Could not convert string 'J' to numeric|does not support|Cannot pass" with pytest.raises(TypeError, match=msg): df["db"].mean() - msg = "Could not convert string 'J' to numeric|ufunc 'divide'" + msg = "Could not convert string 'J' to numeric|ufunc 'divide'|Cannot pass" with pytest.raises(TypeError, match=msg): np.mean(df["db"].astype("string").array)