From d820112aad5bda8e5d35eb635dbe66abc687b03c Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 4 Jul 2019 17:00:33 -0700 Subject: [PATCH 1/3] REF: make ops directory --- pandas/core/{ops.py => ops/__init__.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename pandas/core/{ops.py => ops/__init__.py} (100%) diff --git a/pandas/core/ops.py b/pandas/core/ops/__init__.py similarity index 100% rename from pandas/core/ops.py rename to pandas/core/ops/__init__.py From 043b1c3a12a8ef04ea338825cce31cb76a44e913 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 4 Jul 2019 17:04:48 -0700 Subject: [PATCH 2/3] implement roperator --- pandas/core/ops/__init__.py | 15 +++++++++ pandas/core/ops/roperator.py | 59 ++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 pandas/core/ops/roperator.py diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index 5c58a1433ba3c..eabbd94aa7f8d 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -51,6 +51,21 @@ import pandas.core.common as com import pandas.core.missing as missing +from .roperator import ( # noqa:F401 + radd, + rand_, + rdiv, + rdivmod, + rfloordiv, + rmod, + rmul, + ror_, + rpow, + rsub, + rtruediv, + rxor, +) + # ----------------------------------------------------------------------------- # Ops Wrapping Utilities diff --git a/pandas/core/ops/roperator.py b/pandas/core/ops/roperator.py new file mode 100644 index 0000000000000..c362ce23d0799 --- /dev/null +++ b/pandas/core/ops/roperator.py @@ -0,0 +1,59 @@ +""" +Reversed Operations not available in the stdlib operator module. +Defining these instead of using lambdas allows us to reference them by name. +""" + +def radd(left, right): + return right + left + + +def rsub(left, right): + return right - left + + +def rmul(left, right): + return right * left + + +def rdiv(left, right): + return right / left + + +def rtruediv(left, right): + return right / left + + +def rfloordiv(left, right): + return right // left + + +def rmod(left, right): + # check if right is a string as % is the string + # formatting operation; this is a TypeError + # otherwise perform the op + if isinstance(right, str): + raise TypeError( + "{typ} cannot perform the operation mod".format(typ=type(left).__name__) + ) + + return right % left + + +def rdivmod(left, right): + return divmod(right, left) + + +def rpow(left, right): + return right ** left + + +def rand_(left, right): + return operator.and_(right, left) + + +def ror_(left, right): + return operator.or_(right, left) + + +def rxor(left, right): + return operator.xor(right, left) From 7f858d5e0ac848dc0deca7e36d3c3535a521a3e4 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 4 Jul 2019 17:05:55 -0700 Subject: [PATCH 3/3] missing import, remove duplicates --- pandas/core/ops/__init__.py | 61 ------------------------------------ pandas/core/ops/roperator.py | 2 ++ 2 files changed, 2 insertions(+), 61 deletions(-) diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index eabbd94aa7f8d..6fd53c4b24471 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -166,67 +166,6 @@ def maybe_upcast_for_op(obj): return obj -# ----------------------------------------------------------------------------- -# Reversed Operations not available in the stdlib operator module. -# Defining these instead of using lambdas allows us to reference them by name. - - -def radd(left, right): - return right + left - - -def rsub(left, right): - return right - left - - -def rmul(left, right): - return right * left - - -def rdiv(left, right): - return right / left - - -def rtruediv(left, right): - return right / left - - -def rfloordiv(left, right): - return right // left - - -def rmod(left, right): - # check if right is a string as % is the string - # formatting operation; this is a TypeError - # otherwise perform the op - if isinstance(right, str): - raise TypeError( - "{typ} cannot perform the operation mod".format(typ=type(left).__name__) - ) - - return right % left - - -def rdivmod(left, right): - return divmod(right, left) - - -def rpow(left, right): - return right ** left - - -def rand_(left, right): - return operator.and_(right, left) - - -def ror_(left, right): - return operator.or_(right, left) - - -def rxor(left, right): - return operator.xor(right, left) - - # ----------------------------------------------------------------------------- diff --git a/pandas/core/ops/roperator.py b/pandas/core/ops/roperator.py index c362ce23d0799..4cb02238aea16 100644 --- a/pandas/core/ops/roperator.py +++ b/pandas/core/ops/roperator.py @@ -2,6 +2,8 @@ Reversed Operations not available in the stdlib operator module. Defining these instead of using lambdas allows us to reference them by name. """ +import operator + def radd(left, right): return right + left