File tree Expand file tree Collapse file tree 3 files changed +38
-0
lines changed Expand file tree Collapse file tree 3 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 2
2
3
3
from typing import (
4
4
TYPE_CHECKING ,
5
+ Any ,
5
6
Literal ,
6
7
)
7
8
29
30
from pandas .core .strings .object_array import ObjectStringArrayMixin
30
31
31
32
if TYPE_CHECKING :
33
+ from collections .abc import Callable
34
+
32
35
from pandas ._typing import (
33
36
AxisInt ,
34
37
Dtype ,
@@ -565,3 +568,10 @@ def _wrap_ndarray_result(self, result: np.ndarray):
565
568
566
569
return TimedeltaArray ._simple_new (result , dtype = result .dtype )
567
570
return type (self )(result )
571
+
572
+ def _formatter (self , boxed : bool = False ) -> Callable [[Any ], str | None ]:
573
+ # NEP 51: https://github.com/numpy/numpy/pull/22449
574
+ if self .dtype == "object" :
575
+ return repr
576
+ else :
577
+ return str
Original file line number Diff line number Diff line change @@ -323,3 +323,30 @@ def test_factorize_unsigned():
323
323
tm .assert_numpy_array_equal (res_codes , exp_codes )
324
324
325
325
tm .assert_extension_array_equal (res_unique , NumpyExtensionArray (exp_unique ))
326
+
327
+
328
+ # ----------------------------------------------------------------------------
329
+ # Output formatting
330
+
331
+
332
+ def test_array_repr (any_numpy_array ):
333
+ # GH#???
334
+ nparray = any_numpy_array
335
+ arr = NumpyExtensionArray (nparray )
336
+ if nparray .dtype == "object" :
337
+ values = "['a', 'b']"
338
+ elif nparray .dtype == "float64" :
339
+ values = "[0.0, 1.0]"
340
+ elif nparray .dtype == "int64" :
341
+ values = "[0, 1]"
342
+ elif nparray .dtype == "complex128" :
343
+ values = "[0j, (1+2j)]"
344
+ elif nparray .dtype == "bool" :
345
+ values = "[True, False]"
346
+ elif nparray .dtype == "datetime64[ns]" :
347
+ values = "[1970-01-01T00:00:00.000000000, 1970-01-01T00:00:00.000000001]"
348
+ elif nparray .dtype == "timedelta64[ns]" :
349
+ values = "[0 nanoseconds, 1 nanoseconds]"
350
+ expected = f"<NumpyExtensionArray>\n { values } \n Length: 2, dtype: { nparray .dtype } "
351
+ result = repr (arr )
352
+ assert result == expected
Original file line number Diff line number Diff line change @@ -181,4 +181,5 @@ def test_repr_large():
181
181
"'2001-01-01']\n "
182
182
"Length: 1000, dtype: period[D]"
183
183
)
184
+ print (result )
184
185
assert result == expected
You can’t perform that action at this time.
0 commit comments