Skip to content

Commit 8a3bce6

Browse files
committed
Move CompWrapper from arrays/base.py to ops.py
1 parent f13d38a commit 8a3bce6

File tree

3 files changed

+58
-59
lines changed

3 files changed

+58
-59
lines changed

pandas/core/arrays/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from .array_ import array # noqa
22
from .base import (ExtensionArray, # noqa
33
ExtensionOpsMixin,
4-
ExtensionScalarOpsMixin,
5-
CompWrapper)
4+
ExtensionScalarOpsMixin)
65
from .categorical import Categorical # noqa
76
from .datetimes import DatetimeArray # noqa
87
from .interval import IntervalArray # noqa

pandas/core/arrays/base.py

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
This is an experimental API and subject to breaking changes
66
without warning.
77
"""
8-
from functools import wraps
98
import operator
109

1110
import numpy as np
@@ -16,7 +15,7 @@
1615
from pandas.util._decorators import Appender, Substitution
1716

1817
from pandas.core.dtypes.common import is_list_like
19-
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
18+
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
2019
from pandas.core.dtypes.missing import isna
2120

2221
from pandas.core import ops
@@ -1119,58 +1118,3 @@ def _create_arithmetic_method(cls, op):
11191118
@classmethod
11201119
def _create_comparison_method(cls, op):
11211120
return cls._create_method(op, coerce_to_dtype=False)
1122-
1123-
1124-
class CompWrapper(object):
1125-
__key__ = ['list_to_array', 'validate_len',
1126-
'zerodim', 'inst_from_senior_cls']
1127-
1128-
def __init__(self,
1129-
list_to_array=None,
1130-
validate_len=None,
1131-
zerodim=None,
1132-
inst_from_senior_cls=None):
1133-
self.list_to_array = list_to_array
1134-
self.validate_len = validate_len
1135-
self.zerodim = zerodim
1136-
self.inst_from_senior_cls = inst_from_senior_cls
1137-
1138-
def _list_to_array(self, comp):
1139-
@wraps(comp)
1140-
def wrapper(comp_self, comp_other):
1141-
if is_list_like(comp_other):
1142-
comp_other = np.asarray(comp_other)
1143-
return comp(comp_self, comp_other)
1144-
return wrapper
1145-
1146-
def _validate_len(self, comp):
1147-
@wraps(comp)
1148-
def wrapper(comp_self, comp_other):
1149-
if is_list_like(comp_other) and len(comp_other) != len(comp_self):
1150-
raise ValueError("Lengths must match to compare")
1151-
return comp(comp_self, comp_other)
1152-
return wrapper
1153-
1154-
def _zerodim(self, comp):
1155-
@wraps(comp)
1156-
def wrapper(comp_self, comp_other):
1157-
from pandas._libs import lib
1158-
comp_other = lib.item_from_zerodim(comp_other)
1159-
return comp(comp_self, comp_other)
1160-
return wrapper
1161-
1162-
def _inst_from_senior_cls(self, comp):
1163-
@wraps(comp)
1164-
def wrapper(comp_self, comp_other):
1165-
if isinstance(comp_other, (ABCDataFrame,
1166-
ABCSeries, ABCIndexClass)):
1167-
# Rely on pandas to unbox and dispatch to us.
1168-
return NotImplemented
1169-
return comp(comp_self, comp_other)
1170-
return wrapper
1171-
1172-
def __call__(self, comp):
1173-
for key in CompWrapper.__key__:
1174-
if getattr(self, key) is True:
1175-
comp = getattr(self, '_' + key)(comp)
1176-
return comp

pandas/core/ops.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import division
88

99
import datetime
10+
from functools import wraps
1011
import operator
1112
import textwrap
1213
import warnings
@@ -136,6 +137,61 @@ def maybe_upcast_for_op(obj):
136137
return obj
137138

138139

140+
class CompWrapper(object):
141+
__key__ = ['list_to_array', 'validate_len',
142+
'zerodim', 'inst_from_senior_cls']
143+
144+
def __init__(self,
145+
list_to_array=None,
146+
validate_len=None,
147+
zerodim=None,
148+
inst_from_senior_cls=None):
149+
self.list_to_array = list_to_array
150+
self.validate_len = validate_len
151+
self.zerodim = zerodim
152+
self.inst_from_senior_cls = inst_from_senior_cls
153+
154+
def _list_to_array(self, comp):
155+
@wraps(comp)
156+
def wrapper(comp_self, comp_other):
157+
if is_list_like(comp_other):
158+
comp_other = np.asarray(comp_other)
159+
return comp(comp_self, comp_other)
160+
return wrapper
161+
162+
def _validate_len(self, comp):
163+
@wraps(comp)
164+
def wrapper(comp_self, comp_other):
165+
if is_list_like(comp_other) and len(comp_other) != len(comp_self):
166+
raise ValueError("Lengths must match to compare")
167+
return comp(comp_self, comp_other)
168+
return wrapper
169+
170+
def _zerodim(self, comp):
171+
@wraps(comp)
172+
def wrapper(comp_self, comp_other):
173+
from pandas._libs import lib
174+
comp_other = lib.item_from_zerodim(comp_other)
175+
return comp(comp_self, comp_other)
176+
return wrapper
177+
178+
def _inst_from_senior_cls(self, comp):
179+
@wraps(comp)
180+
def wrapper(comp_self, comp_other):
181+
if isinstance(comp_other, (ABCDataFrame,
182+
ABCSeries, ABCIndexClass)):
183+
# Rely on pandas to unbox and dispatch to us.
184+
return NotImplemented
185+
return comp(comp_self, comp_other)
186+
return wrapper
187+
188+
def __call__(self, comp):
189+
for key in CompWrapper.__key__:
190+
if getattr(self, key) is True:
191+
comp = getattr(self, '_' + key)(comp)
192+
return comp
193+
194+
139195
# -----------------------------------------------------------------------------
140196
# Reversed Operations not available in the stdlib operator module.
141197
# Defining these instead of using lambdas allows us to reference them by name.

0 commit comments

Comments
 (0)