diff --git a/pandas/_libs/interval.pyx b/pandas/_libs/interval.pyx index 074e9b19eaf72..e07d80dd04b31 100644 --- a/pandas/_libs/interval.pyx +++ b/pandas/_libs/interval.pyx @@ -492,8 +492,9 @@ cdef class Interval(IntervalMixin): def __repr__(self) -> str: left, right = self._repr_base() + disp = str if isinstance(left, np.generic) else repr name = type(self).__name__ - repr_str = f"{name}({repr(left)}, {repr(right)}, closed={repr(self.closed)})" + repr_str = f"{name}({disp(left)}, {disp(right)}, closed={repr(self.closed)})" return repr_str def __str__(self) -> str: diff --git a/pandas/core/arrays/masked.py b/pandas/core/arrays/masked.py index 955adfab7f8dc..1de3ae3b2428e 100644 --- a/pandas/core/arrays/masked.py +++ b/pandas/core/arrays/masked.py @@ -3,6 +3,7 @@ from typing import ( TYPE_CHECKING, Any, + Callable, Literal, overload, ) @@ -160,6 +161,10 @@ def _empty(cls, shape: Shape, dtype: ExtensionDtype): ) return result + def _formatter(self, boxed: bool = False) -> Callable[[Any], str | None]: + # NEP 51: https://github.com/numpy/numpy/pull/22449 + return str + @property def dtype(self) -> BaseMaskedDtype: raise AbstractMethodError(self) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 2f044905d33f9..3c434dfa81ae3 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5250,7 +5250,8 @@ def _raise_scalar_data_error(cls, data): # in order to keep mypy happy raise TypeError( f"{cls.__name__}(...) must be called with a collection of some " - f"kind, {repr(data)} was passed" + f"kind, {repr(data) if not isinstance(data, np.generic) else str(data)} " + "was passed" ) def _validate_fill_value(self, value): @@ -6974,7 +6975,7 @@ def drop( mask = indexer == -1 if mask.any(): if errors != "ignore": - raise KeyError(f"{list(labels[mask])} not found in axis") + raise KeyError(f"{labels[mask].tolist()} not found in axis") indexer = indexer[~mask] return self.delete(indexer) diff --git a/pandas/tests/arrays/floating/test_construction.py b/pandas/tests/arrays/floating/test_construction.py index 2dcd54f443029..4007ee6b415c9 100644 --- a/pandas/tests/arrays/floating/test_construction.py +++ b/pandas/tests/arrays/floating/test_construction.py @@ -125,6 +125,7 @@ def test_to_array_error(values): "Cannot pass scalar", r"float\(\) argument must be a string or a (real )?number, not 'dict'", "could not convert string to float: 'foo'", + r"could not convert string to float: np\.str_\('foo'\)", ] ) with pytest.raises((TypeError, ValueError), match=msg): diff --git a/pandas/tests/arrays/sparse/test_dtype.py b/pandas/tests/arrays/sparse/test_dtype.py index 6c95770e9290f..234f4092421e5 100644 --- a/pandas/tests/arrays/sparse/test_dtype.py +++ b/pandas/tests/arrays/sparse/test_dtype.py @@ -197,7 +197,7 @@ def test_update_dtype(original, dtype, expected): ( SparseDtype(str, "abc"), int, - re.escape("invalid literal for int() with base 10: 'abc'"), + r"invalid literal for int\(\) with base 10: ('abc'|np\.str_\('abc'\))", ), ], )