16
16
from pandas .compat .numpy import function as nv
17
17
18
18
import pandas .core .common as com
19
+ from pandas .core import ops
19
20
from pandas .core .indexes .base import Index , _index_shared_docs
20
21
from pandas .util ._decorators import Appender , cache_readonly
21
22
import pandas .core .dtypes .concat as _concat
@@ -570,16 +571,12 @@ def __floordiv__(self, other):
570
571
def _add_numeric_methods_binary (cls ):
571
572
""" add in numeric methods, specialized to RangeIndex """
572
573
573
- def _make_evaluate_binop (op , opstr , reversed = False , step = False ):
574
+ def _make_evaluate_binop (op , step = False ):
574
575
"""
575
576
Parameters
576
577
----------
577
578
op : callable that accepts 2 parms
578
579
perform the binary op
579
- opstr : string
580
- string name of ops
581
- reversed : boolean, default False
582
- if this is a reversed op, e.g. radd
583
580
step : callable, optional, default to False
584
581
op to apply to the step parm if not None
585
582
if False, use the existing step
@@ -594,17 +591,13 @@ def _evaluate_numeric_binop(self, other):
594
591
elif isinstance (other , (timedelta , np .timedelta64 )):
595
592
# GH#19333 is_integer evaluated True on timedelta64,
596
593
# so we need to catch these explicitly
597
- if reversed :
598
- return op (other , self ._int64index )
599
594
return op (self ._int64index , other )
600
595
601
- other = self ._validate_for_numeric_binop (other , op , opstr )
596
+ other = self ._validate_for_numeric_binop (other , op )
602
597
attrs = self ._get_attributes_dict ()
603
598
attrs = self ._maybe_update_attributes (attrs )
604
599
605
600
left , right = self , other
606
- if reversed :
607
- left , right = right , left
608
601
609
602
try :
610
603
# apply if we have an override
@@ -638,43 +631,26 @@ def _evaluate_numeric_binop(self, other):
638
631
639
632
return result
640
633
641
- except (ValueError , TypeError , AttributeError ,
642
- ZeroDivisionError ):
634
+ except (ValueError , TypeError , ZeroDivisionError ):
643
635
# Defer to Int64Index implementation
644
- if reversed :
645
- return op (other , self ._int64index )
646
636
return op (self ._int64index , other )
637
+ # TODO: Do attrs get handled reliably?
647
638
648
639
return _evaluate_numeric_binop
649
640
650
- cls .__add__ = cls .__radd__ = _make_evaluate_binop (
651
- operator .add , '__add__' )
652
- cls .__sub__ = _make_evaluate_binop (operator .sub , '__sub__' )
653
- cls .__rsub__ = _make_evaluate_binop (
654
- operator .sub , '__sub__' , reversed = True )
655
- cls .__mul__ = cls .__rmul__ = _make_evaluate_binop (
656
- operator .mul ,
657
- '__mul__' ,
658
- step = operator .mul )
659
- cls .__truediv__ = _make_evaluate_binop (
660
- operator .truediv ,
661
- '__truediv__' ,
662
- step = operator .truediv )
663
- cls .__rtruediv__ = _make_evaluate_binop (
664
- operator .truediv ,
665
- '__truediv__' ,
666
- reversed = True ,
667
- step = operator .truediv )
641
+ cls .__add__ = _make_evaluate_binop (operator .add )
642
+ cls .__radd__ = _make_evaluate_binop (ops .radd )
643
+ cls .__sub__ = _make_evaluate_binop (operator .sub )
644
+ cls .__rsub__ = _make_evaluate_binop (ops .rsub )
645
+ cls .__mul__ = _make_evaluate_binop (operator .mul , step = operator .mul )
646
+ cls .__rmul__ = _make_evaluate_binop (ops .rmul , step = ops .rmul )
647
+ cls .__truediv__ = _make_evaluate_binop (operator .truediv ,
648
+ step = operator .truediv )
649
+ cls .__rtruediv__ = _make_evaluate_binop (ops .rtruediv ,
650
+ step = ops .rtruediv )
668
651
if not compat .PY3 :
669
- cls .__div__ = _make_evaluate_binop (
670
- operator .div ,
671
- '__div__' ,
672
- step = operator .div )
673
- cls .__rdiv__ = _make_evaluate_binop (
674
- operator .div ,
675
- '__div__' ,
676
- reversed = True ,
677
- step = operator .div )
652
+ cls .__div__ = _make_evaluate_binop (operator .div , step = operator .div )
653
+ cls .__rdiv__ = _make_evaluate_binop (ops .rdiv , step = ops .rdiv )
678
654
679
655
680
656
RangeIndex ._add_numeric_methods ()
0 commit comments