ENH: add default ExtensionArray.round (closes #49387)#65507
Open
jbrockmendel wants to merge 4 commits into
Open
ENH: add default ExtensionArray.round (closes #49387)#65507jbrockmendel wants to merge 4 commits into
jbrockmendel wants to merge 4 commits into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
roundto theExtensionArraybase class so third-party numeric EAs (e.g. pint) participate inSeries.roundwithout each implementer wiring up their own method.TypeError.ArrowExtensionArraykeeps itspc.roundfast-path and defers to the base for the boolean/non-numeric contract (sincepc.roundraisesArrowNotImplementedErroron those).BaseMethodsTests.test_round; skips forDatetimeArraysince itsround(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
pandas/tests/extension/base/methods.py::BaseMethodsTests::test_roundexercises numeric, boolean, and non-numeric dtypes across all EA test files (70 passed, 1 skipped forDatetimeArray)pandas/tests/extension/suite passespandas/tests/series/methods/test_round.py,pandas/tests/frame/methods/test_round.py,pandas/tests/indexes/datetimes/methods/test_round.pyall pass