Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/pandas/core/ops/common.py : 55%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2Boilerplate functions used in defining binary operations.
3"""
4from functools import wraps
6from pandas._libs.lib import item_from_zerodim
8from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
11def unpack_zerodim_and_defer(name: str):
12 """
13 Boilerplate for pandas conventions in arithmetic and comparison methods.
15 Parameters
16 ----------
17 name : str
19 Returns
20 -------
21 decorator
22 """
24 def wrapper(method):
25 return _unpack_zerodim_and_defer(method, name)
27 return wrapper
30def _unpack_zerodim_and_defer(method, name: str):
31 """
32 Boilerplate for pandas conventions in arithmetic and comparison methods.
34 Ensure method returns NotImplemented when operating against "senior"
35 classes. Ensure zero-dimensional ndarrays are always unpacked.
37 Parameters
38 ----------
39 method : binary method
40 name : str
42 Returns
43 -------
44 method
45 """
47 is_cmp = name.strip("__") in {"eq", "ne", "lt", "le", "gt", "ge"}
49 @wraps(method)
50 def new_method(self, other):
52 if is_cmp and isinstance(self, ABCIndexClass) and isinstance(other, ABCSeries):
53 # For comparison ops, Index does *not* defer to Series
54 pass
55 else:
56 for cls in [ABCDataFrame, ABCSeries, ABCIndexClass]:
57 if isinstance(self, cls):
58 break
59 if isinstance(other, cls):
60 return NotImplemented
62 other = item_from_zerodim(other)
64 return method(self, other)
66 return new_method