Skip to content

ENH: add default ExtensionArray.round (closes #49387)#65507

Open
jbrockmendel wants to merge 4 commits into
pandas-dev:mainfrom
jbrockmendel:enh-round
Open

ENH: add default ExtensionArray.round (closes #49387)#65507
jbrockmendel wants to merge 4 commits into
pandas-dev:mainfrom
jbrockmendel:enh-round

Conversation

@jbrockmendel
Copy link
Copy Markdown
Member

Summary

  • Adds a non-performant elementwise default round to the ExtensionArray base class so third-party numeric EAs (e.g. pint) participate in Series.round without each implementer wiring up their own method.
  • Boolean dtypes return a copy; non-numeric dtypes raise TypeError.
  • ArrowExtensionArray keeps its pc.round fast-path and defers to the base for the boolean/non-numeric contract (since pc.round raises ArrowNotImplementedError on those).
  • Adds a generic BaseMethodsTests.test_round; skips for DatetimeArray since its round(freq, ...) has a different signature.

Picks up the work from the stale GH-54582 and addresses the outstanding review comments from that PR.

closes #49387

Test plan

  • new pandas/tests/extension/base/methods.py::BaseMethodsTests::test_round exercises numeric, boolean, and non-numeric dtypes across all EA test files (70 passed, 1 skipped for DatetimeArray)
  • full pandas/tests/extension/ suite passes
  • pandas/tests/series/methods/test_round.py, pandas/tests/frame/methods/test_round.py, pandas/tests/indexes/datetimes/methods/test_round.py all pass
  • pre-commit clean

@jbrockmendel jbrockmendel added Enhancement ExtensionArray Extending pandas with custom dtypes or arrays. labels May 28, 2026
jbrockmendel and others added 4 commits June 1, 2026 14:41
Provides a non-performant elementwise default `round` on the
`ExtensionArray` base class so third-party numeric EAs (e.g. pint) can
participate in `Series.round` without each implementer wiring up their
own method.  Boolean dtypes return a copy; non-numeric dtypes raise
`TypeError`.  `ArrowExtensionArray` keeps its `pc.round` fast-path and
defers to the base for the boolean/non-numeric contract.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
pyarrow < 14 upcasts integer inputs to double in pc.round; cast back to
the input type so the result matches the input dtype.  Surfaced by the
new BaseMethodsTests.test_round on the Minimum Versions CI job.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
numpy complex scalars do not define __round__, so the elementwise
default ExtensionArray.round (and the BaseMethodsTests reference) was
hitting Python's DeprecationWarning for round(complex), which becomes
an error under PYTHONDEVMODE=1 in CI.  Fall back to np.round when the
scalar lacks __round__.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The previous hasattr(__round__) guard was Python-version-dependent —
on Python 3.11 the complex __round__ slot exists but emits a
DeprecationWarning when called, while on Python 3.13 the slot is
absent.  Dispatch on dtype.kind == "c" instead, which is independent
of CPython behavior and routes around builtin round() for complex
inputs regardless of Python version.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement ExtensionArray Extending pandas with custom dtypes or arrays.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Series.round() expects ExtensionArray.round() to be implemented

1 participant