Skip to content

Commit e51216c

Browse files
authored
DEPS: Update reprs with numpy NEP51 scalar repr (#54268)
* DEPS: Update reprs with numpy NEP51 scalar repr * Just return str
1 parent 3fb33d6 commit e51216c

File tree

5 files changed

+12
-4
lines changed

5 files changed

+12
-4
lines changed

pandas/_libs/interval.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,9 @@ cdef class Interval(IntervalMixin):
492492
def __repr__(self) -> str:
493493

494494
left, right = self._repr_base()
495+
disp = str if isinstance(left, np.generic) else repr
495496
name = type(self).__name__
496-
repr_str = f"{name}({repr(left)}, {repr(right)}, closed={repr(self.closed)})"
497+
repr_str = f"{name}({disp(left)}, {disp(right)}, closed={repr(self.closed)})"
497498
return repr_str
498499

499500
def __str__(self) -> str:

pandas/core/arrays/masked.py

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import (
44
TYPE_CHECKING,
55
Any,
6+
Callable,
67
Literal,
78
overload,
89
)
@@ -160,6 +161,10 @@ def _empty(cls, shape: Shape, dtype: ExtensionDtype):
160161
)
161162
return result
162163

164+
def _formatter(self, boxed: bool = False) -> Callable[[Any], str | None]:
165+
# NEP 51: https://github.com/numpy/numpy/pull/22449
166+
return str
167+
163168
@property
164169
def dtype(self) -> BaseMaskedDtype:
165170
raise AbstractMethodError(self)

pandas/core/indexes/base.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -5250,7 +5250,8 @@ def _raise_scalar_data_error(cls, data):
52505250
# in order to keep mypy happy
52515251
raise TypeError(
52525252
f"{cls.__name__}(...) must be called with a collection of some "
5253-
f"kind, {repr(data)} was passed"
5253+
f"kind, {repr(data) if not isinstance(data, np.generic) else str(data)} "
5254+
"was passed"
52545255
)
52555256

52565257
def _validate_fill_value(self, value):
@@ -6974,7 +6975,7 @@ def drop(
69746975
mask = indexer == -1
69756976
if mask.any():
69766977
if errors != "ignore":
6977-
raise KeyError(f"{list(labels[mask])} not found in axis")
6978+
raise KeyError(f"{labels[mask].tolist()} not found in axis")
69786979
indexer = indexer[~mask]
69796980
return self.delete(indexer)
69806981

pandas/tests/arrays/floating/test_construction.py

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def test_to_array_error(values):
125125
"Cannot pass scalar",
126126
r"float\(\) argument must be a string or a (real )?number, not 'dict'",
127127
"could not convert string to float: 'foo'",
128+
r"could not convert string to float: np\.str_\('foo'\)",
128129
]
129130
)
130131
with pytest.raises((TypeError, ValueError), match=msg):

pandas/tests/arrays/sparse/test_dtype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def test_update_dtype(original, dtype, expected):
197197
(
198198
SparseDtype(str, "abc"),
199199
int,
200-
re.escape("invalid literal for int() with base 10: 'abc'"),
200+
r"invalid literal for int\(\) with base 10: ('abc'|np\.str_\('abc'\))",
201201
),
202202
],
203203
)

0 commit comments

Comments
 (0)