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

CLN: remove Index.__inv__ , add Index.__invert__ #22336

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ Indexing
- Fixed ``DataFrame[np.nan]`` when columns are non-unique (:issue:`21428`)
- Bug when indexing :class:`DatetimeIndex` with nanosecond resolution dates and timezones (:issue:`11679`)
- Bug where indexing with a Numpy array containing negative values would mutate the indexer (:issue:`21867`)
- Method :meth:`Index.__inv__` was suppressed, as it provided the same functionality as :meth:`Index.__neg__` rather than what its name suggested; :meth:`Index.__invert__` was added instead, and the ``~`` operator now works (analogously as for :class:`Series`) (:issue:`21688`)
-

Missing
^^^^^^^
Expand Down Expand Up @@ -688,4 +690,3 @@ Other
- :meth: `~pandas.io.formats.style.Styler.background_gradient` now also supports tablewise application (in addition to rowwise and columnwise) with ``axis=None`` (:issue:`15204`)
-
-
-
4 changes: 2 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4689,7 +4689,7 @@ def _add_numeric_methods_disabled(cls):
cls.__neg__ = make_invalid_op('__neg__')
cls.__pos__ = make_invalid_op('__pos__')
cls.__abs__ = make_invalid_op('__abs__')
cls.__inv__ = make_invalid_op('__inv__')
cls.__invert__ = make_invalid_op('__invert__')

def _maybe_update_attributes(self, attrs):
""" Update Index attributes (e.g. freq) depending on op """
Expand Down Expand Up @@ -4786,7 +4786,7 @@ def _evaluate_numeric_unary(self):
cls.__neg__ = _make_evaluate_unary(operator.neg, '__neg__')
cls.__pos__ = _make_evaluate_unary(operator.pos, '__pos__')
cls.__abs__ = _make_evaluate_unary(np.abs, '__abs__')
cls.__inv__ = _make_evaluate_unary(lambda x: -x, '__inv__')
cls.__invert__ = _make_evaluate_unary(np.invert, '__invert__')

@classmethod
def _add_numeric_methods(cls):
Expand Down
37 changes: 36 additions & 1 deletion pandas/tests/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pandas._libs.tslibs import IncompatibleFrequency
from pandas import (
timedelta_range,
Timedelta, Timestamp, NaT, Series, TimedeltaIndex, DatetimeIndex,
Timedelta, Timestamp, NaT, Series, Index, TimedeltaIndex, DatetimeIndex,
DataFrame)


Expand Down Expand Up @@ -73,6 +73,41 @@ def box_df_fail(request):
return request.param


# ------------------------------------------------------------------
# Numeric types Arithmetic Operations

class TestNumericOps(object):

@pytest.mark.parametrize('opname', ['neg', 'pos', 'abs', 'inv', 'invert'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add this in conftest as a fixture (unary_op_fixtures)?

@pytest.mark.parametrize('dtype', [int, float, object])
def test_numeric_unary_ops(self, box, dtype, opname):
# GH 22335
data = [-2, 0, 4, 5]
obj = box(data, dtype=dtype)

def call_method():
return getattr(obj, '__{}__'.format(opname))()

if opname == 'inv':
# GH 22335 - 'inv' was wrong and was removed
pytest.raises(AttributeError, call_method)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make these if/elseif, then don't need the return (put the final clause in the else)

return
if opname == 'invert' and dtype is float:
# inversion of floats is undefined
pytest.raises(TypeError, call_method)
return
if dtype in (object, bool) and isinstance(obj, Index):
# GH 16873 - numeric operations on 'object' dtype might be removed
# from NDFrames as well
pytest.raises(TypeError, call_method)
return

result = call_method()
library_op = getattr(operator, opname)
expected = box([library_op(i) for i in data], dtype=dtype)
tm.assert_equal(result, expected)


# ------------------------------------------------------------------
# Timedelta64[ns] dtype Comparisons

Expand Down