-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
REF/CLN: ops boilerplate #23853 #24846
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
Changes from all commits
d60f317
2c09418
0c1a296
cb938ce
8966432
99f8afd
c17322e
45e1df0
72e8b2d
b07279f
5cf404c
96e5148
f744ccd
1234372
ef40bcd
57463cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
from __future__ import division | ||
|
||
import datetime | ||
from functools import wraps | ||
import operator | ||
import textwrap | ||
import warnings | ||
|
@@ -28,8 +29,8 @@ | |
is_integer_dtype, is_list_like, is_object_dtype, is_period_dtype, | ||
is_scalar, is_timedelta64_dtype, needs_i8_conversion) | ||
from pandas.core.dtypes.generic import ( | ||
ABCDataFrame, ABCIndex, ABCIndexClass, ABCPanel, ABCSeries, ABCSparseArray, | ||
ABCSparseSeries) | ||
ABCDataFrame, ABCExtensionArray, ABCIndex, ABCIndexClass, ABCPanel, | ||
ABCSeries, ABCSparseArray, ABCSparseSeries) | ||
from pandas.core.dtypes.missing import isna, notna | ||
|
||
import pandas as pd | ||
|
@@ -136,6 +137,62 @@ def maybe_upcast_for_op(obj): | |
return obj | ||
|
||
|
||
class CompWrapper(object): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haven't looked at implementation in detail but can you add a docstring for this class to describe utility and parameters? Also what is the reasoning for going this route instead of say a meta class? |
||
__key__ = ['list_to_array', 'validate_len', | ||
'zerodim', 'inst_from_senior_cls'] | ||
|
||
def __init__(self, | ||
list_to_array=None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you make these arg names even more explict, e.g. |
||
validate_len=None, | ||
zerodim=None, | ||
inst_from_senior_cls=None): | ||
self.list_to_array = list_to_array | ||
self.validate_len = validate_len | ||
self.zerodim = zerodim | ||
self.inst_from_senior_cls = inst_from_senior_cls | ||
|
||
def _list_to_array(self, comp): | ||
@wraps(comp) | ||
def wrapper(comp_self, comp_other): | ||
if is_list_like(comp_other): | ||
comp_other = np.asarray(comp_other) | ||
return comp(comp_self, comp_other) | ||
return wrapper | ||
|
||
def _validate_len(self, comp): | ||
@wraps(comp) | ||
def wrapper(comp_self, comp_other): | ||
if is_list_like(comp_other) and len(comp_other) != len(comp_self): | ||
raise ValueError("Lengths must match to compare") | ||
return comp(comp_self, comp_other) | ||
return wrapper | ||
|
||
def _zerodim(self, comp): | ||
@wraps(comp) | ||
def wrapper(comp_self, comp_other): | ||
from pandas._libs import lib | ||
comp_other = lib.item_from_zerodim(comp_other) | ||
return comp(comp_self, comp_other) | ||
return wrapper | ||
|
||
def _inst_from_senior_cls(self, comp): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a doc-string to each of these |
||
@wraps(comp) | ||
def wrapper(comp_self, comp_other): | ||
if isinstance(comp_self, ABCExtensionArray): | ||
if isinstance(comp_other, (ABCDataFrame, ABCSeries, | ||
ABCIndexClass)): | ||
# Rely on pandas to unbox and dispatch to us. | ||
return NotImplemented | ||
return comp(comp_self, comp_other) | ||
return wrapper | ||
|
||
def __call__(self, comp): | ||
for key in CompWrapper.__key__: | ||
if getattr(self, key) is True: | ||
comp = getattr(self, '_' + key)(comp) | ||
return comp | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
# Reversed Operations not available in the stdlib operator module. | ||
# Defining these instead of using lambdas allows us to reference them by name. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra value here