DEPR: __invert__ on object dtype (GH#51567) - #67372
Open
jbrockmendel wants to merge 2 commits into
Open
Conversation
~ on object dtype dispatches elementwise to the objects' own __invert__, so a boolean mask that has been cast to object silently becomes integers instead of being negated. Object is the only dtype left to deprecate: ~ currently succeeds only on bool-like, integer-like, and object dtypes. datetime64, timedelta64, period, interval, category, float, complex, string, decimal and every non-integer arrow type already raise. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
tests/arithmetic holds binary-operator suites; move the deprecation tests to the per-container unary homes instead. Adds coverage for the SparseArray[object] call site, which tests/extension/test_sparse.py never reached (it only parametrizes float dtypes). Also reference GH#16873 and GH#31035 in the whatsnew note; both are resolved by this deprecation. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
jbrockmendel
marked this pull request as ready for review
August 30, 2026 18:34
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.
closes #51567
closes #16873
closes #31035
~on object dtype dispatches elementwise to the objects' own__invert__, so a boolean mask that has been cast to object silently becomes integers instead of being negated:This is the cause of GH-16873 (
~ser.shift(1).dropna()on a bool Series returns[-1, -1, -2], sinceshiftreturns object) and GH-31035 ((~s).astype(bool)on an object bool Series returns allTrue). It is also whyser.where(cond)accepts an object-dtype booleancondbutser.mask(cond)raisesTypeError: Boolean array expected for the condition, not int64—maskcomputes~cond.GH-31035 asks for the opposite resolution — that
~on an object-dtype boolean Series return correct booleans. This deprecates the operation instead; the replacement is~obj.astype(bool).Object dtype is the only thing left to deprecate here.
~currently succeeds on exactly three families — bool-like (bool,boolean,bool[pyarrow],Sparse[bool]), integer-like (numpy, masked, arrow,Sparse[int64]), and object.datetime64,timedelta64,period,interval,category, float/complex, string, decimal and every non-integer arrow type already raise, so "deprecate everything besides int and bool" and "deprecate object" are the same change.Object dtype is not even self-consistent today, which is why the bug hides so well —
np.bool_elements give the correct logical NOT while Pythonboolelements give integers, and the two are indistinguishable to the user:Worth noting that CPython reached the same conclusion independently: Python 3.12+ emits
DeprecationWarning: Bitwise inversion '~' on bool is deprecated and will be removed in Python 3.16, so the boolean-mask case will raise on its own in 3.16 regardless.The warning is emitted from four entry points (
NDFrame.__invert__,Index.__invert__,NumpyExtensionArray.__invert__,SparseArray.__invert__).SparseArraymatters becauseSparse[object]with an objectfill_valuesucceeds and reproduces the bug; with the defaultnanfill_value it happens to raise.Integer dtypes are deliberately left alone — that behavior matches Python and numpy, is tested through
pd.eval, and was documented with worked examples in GH-65291.AI disclosure: drafted with Claude Code (
claude opus 5 (high)), which surveyed the current per-dtype behavior, instrumented the test suite to find every affected test in advance, and wrote the implementation and tests under my review.