Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEPS: Update NumpyExtensionArray repr for NEP51 #61085

Merged
merged 7 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Other enhancements
updated to work correctly with NumPy >= 2 (:issue:`57739`)
- :meth:`Series.str.decode` result now has ``StringDtype`` when ``future.infer_string`` is True (:issue:`60709`)
- :meth:`~Series.to_hdf` and :meth:`~DataFrame.to_hdf` now round-trip with ``StringDtype`` (:issue:`60663`)
- Improved ``repr`` of :class:`.NumpyExtensionArray` to account for NEP51 (:issue:`61085`)
- The :meth:`Series.str.decode` has gained the argument ``dtype`` to control the dtype of the result (:issue:`60940`)
- The :meth:`~Series.cumsum`, :meth:`~Series.cummin`, and :meth:`~Series.cummax` reductions are now implemented for ``StringDtype`` columns (:issue:`60633`)
- The :meth:`~Series.sum` reduction is now implemented for ``StringDtype`` columns (:issue:`59853`)
Expand Down
12 changes: 12 additions & 0 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import (
TYPE_CHECKING,
Any,
Literal,
)

Expand Down Expand Up @@ -29,6 +30,8 @@
from pandas.core.strings.object_array import ObjectStringArrayMixin

if TYPE_CHECKING:
from collections.abc import Callable

from pandas._typing import (
AxisInt,
Dtype,
Expand Down Expand Up @@ -565,3 +568,12 @@ def _wrap_ndarray_result(self, result: np.ndarray):

return TimedeltaArray._simple_new(result, dtype=result.dtype)
return type(self)(result)

def _formatter(self, boxed: bool = False) -> Callable[[Any], str | None]:
# NEP 51: https://github.com/numpy/numpy/pull/22449
if self.dtype.kind in "SU":
return "'{}'".format
elif self.dtype == "object":
return repr
else:
return str
31 changes: 29 additions & 2 deletions pandas/tests/arrays/numpy_/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
np.array([True, False], dtype=bool),
np.array([0, 1], dtype="datetime64[ns]"),
np.array([0, 1], dtype="timedelta64[ns]"),
]
],
)
def any_numpy_array(request):
"""
Parametrized fixture for NumPy arrays with different dtypes.

This excludes string and bytes.
"""
return request.param
return request.param.copy()


# ----------------------------------------------------------------------------
Expand Down Expand Up @@ -323,3 +323,30 @@ def test_factorize_unsigned():
tm.assert_numpy_array_equal(res_codes, exp_codes)

tm.assert_extension_array_equal(res_unique, NumpyExtensionArray(exp_unique))


# ----------------------------------------------------------------------------
# Output formatting


def test_array_repr(any_numpy_array):
# GH#61085
nparray = any_numpy_array
arr = NumpyExtensionArray(nparray)
if nparray.dtype == "object":
values = "['a', 'b']"
elif nparray.dtype == "float64":
values = "[0.0, 1.0]"
elif str(nparray.dtype).startswith("int"):
values = "[0, 1]"
elif nparray.dtype == "complex128":
values = "[0j, (1+2j)]"
elif nparray.dtype == "bool":
values = "[True, False]"
elif nparray.dtype == "datetime64[ns]":
values = "[1970-01-01T00:00:00.000000000, 1970-01-01T00:00:00.000000001]"
elif nparray.dtype == "timedelta64[ns]":
values = "[0 nanoseconds, 1 nanoseconds]"
expected = f"<NumpyExtensionArray>\n{values}\nLength: 2, dtype: {nparray.dtype}"
result = repr(arr)
assert result == expected, f"{result} vs {expected}"
Loading