|
6 | 6 | without warning. |
7 | 7 | """ |
8 | 8 | import operator |
| 9 | +from functools import wraps |
9 | 10 |
|
10 | 11 | import numpy as np |
11 | 12 |
|
|
15 | 16 | from pandas.util._decorators import Appender, Substitution |
16 | 17 |
|
17 | 18 | from pandas.core.dtypes.common import is_list_like |
18 | | -from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries |
| 19 | +from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries |
19 | 20 | from pandas.core.dtypes.missing import isna |
20 | 21 |
|
21 | 22 | from pandas.core import ops |
@@ -1118,3 +1119,83 @@ def _create_arithmetic_method(cls, op): |
1118 | 1119 | @classmethod |
1119 | 1120 | def _create_comparison_method(cls, op): |
1120 | 1121 | return cls._create_method(op, coerce_to_dtype=False) |
| 1122 | + |
| 1123 | +''' |
| 1124 | +def validate_comp_other(comp, list_to_array=False, validate_len=False, |
| 1125 | + zerodim=False, inst_from_senior_cls=False): |
| 1126 | + def wrapper(self, other): |
| 1127 | + if list_to_array is True: |
| 1128 | + if is_list_like(other): |
| 1129 | + other = np.asarray(other) |
| 1130 | +
|
| 1131 | + if validate_len is True: |
| 1132 | + if is_list_like(other) and len(other) != len(self): |
| 1133 | + raise ValueError("Lenghts must match") |
| 1134 | +
|
| 1135 | + if zerodim is True: |
| 1136 | + import pandas._libs as lib |
| 1137 | + other = lib.item_from_zerodim(other) |
| 1138 | +
|
| 1139 | + if inst_from_senior_cls is True: |
| 1140 | + if isinstance(other, (ABCDataFrame, ABCSeries, ABCIndexClass)): |
| 1141 | + # Rely on pandas to unbox and dispatch to us. |
| 1142 | + return NotImplemented |
| 1143 | +
|
| 1144 | + comp(self, other) |
| 1145 | + return wrapper |
| 1146 | +''' |
| 1147 | + |
| 1148 | + |
| 1149 | +class CompWrapper(object): |
| 1150 | + __key__ = ['list_to_array', 'validate_len', |
| 1151 | + 'zerodim', 'inst_from_senior_cls'] |
| 1152 | + |
| 1153 | + def __init__(self, |
| 1154 | + list_to_array=None, |
| 1155 | + validate_len=None, |
| 1156 | + zerodim=None, |
| 1157 | + inst_from_senior_cls=None): |
| 1158 | + self.list_to_array = list_to_array |
| 1159 | + self.validate_len = validate_len |
| 1160 | + self.zerodim = zerodim |
| 1161 | + self.inst_from_senior_cls = inst_from_senior_cls |
| 1162 | + |
| 1163 | + def _list_to_array(self, comp): |
| 1164 | + @wraps(comp) |
| 1165 | + def wrapper(comp_self, comp_other): |
| 1166 | + if is_list_like(comp_other): |
| 1167 | + comp_other = np.asarray(comp_other) |
| 1168 | + return comp(comp_self, comp_other) |
| 1169 | + return wrapper |
| 1170 | + |
| 1171 | + def _validate_len(self, comp): |
| 1172 | + @wraps(comp) |
| 1173 | + def wrapper(comp_self, comp_other): |
| 1174 | + if is_list_like(comp_other) and len(comp_other) != len(comp_self): |
| 1175 | + raise ValueError("Lengths must match to compare") |
| 1176 | + return comp(comp_self, comp_other) |
| 1177 | + return wrapper |
| 1178 | + |
| 1179 | + def _zerodim(self, comp): |
| 1180 | + @wraps(comp) |
| 1181 | + def wrapper(comp_self, comp_other): |
| 1182 | + from pandas._libs import lib |
| 1183 | + comp_other = lib.item_from_zerodim(comp_other) |
| 1184 | + return comp(comp_self, comp_other) |
| 1185 | + return wrapper |
| 1186 | + |
| 1187 | + def _inst_from_senior_cls(self, comp): |
| 1188 | + @wraps(comp) |
| 1189 | + def wrapper(comp_self, comp_other): |
| 1190 | + if isinstance(comp_other, (ABCDataFrame, |
| 1191 | + ABCSeries, ABCIndexClass)): |
| 1192 | + # Rely on pandas to unbox and dispatch to us. |
| 1193 | + return NotImplemented |
| 1194 | + return comp(comp_self, comp_other) |
| 1195 | + return wrapper |
| 1196 | + |
| 1197 | + def __call__(self, comp): |
| 1198 | + for key in CompWrapper.__key__: |
| 1199 | + if getattr(self, key) is True: |
| 1200 | + comp = getattr(self, '_' + key)(comp) |
| 1201 | + return comp |
0 commit comments