forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathna_scalar.py
112 lines (82 loc) · 3.24 KB
/
na_scalar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import numbers
def _create_binary_propagating_op(name):
def method(self, other):
if isinstance(other, numbers.Number) or other is NA or isinstance(other, str):
return NA
return NotImplemented
method.__name__ = name
return method
def _create_unary_propagating_op(name):
def method(self):
return NA
method.__name__ = name
return method
class NAType:
_instance = None
def __new__(cls, *args, **kwargs):
if NAType._instance is None:
NAType._instance = object.__new__(cls, *args, **kwargs)
return NAType._instance
def __repr__(self) -> str:
return "NA"
def __str__(self) -> str:
return "NA"
def __bool__(self):
raise TypeError("boolean value of NA is ambiguous")
def __hash__(self):
return id(self)
# Binary arithmetic and comparison ops -> propagate
__add__ = _create_binary_propagating_op("__add__")
__radd__ = _create_binary_propagating_op("__radd__")
__sub__ = _create_binary_propagating_op("__sub__")
__rsub__ = _create_binary_propagating_op("__rsub__")
__mul__ = _create_binary_propagating_op("__mul__")
__rmul__ = _create_binary_propagating_op("__rmul__")
__matmul__ = _create_binary_propagating_op("__matmul__")
__rmatmul__ = _create_binary_propagating_op("__rmatmul__")
__truediv__ = _create_binary_propagating_op("__truediv__")
__rtruediv__ = _create_binary_propagating_op("__rtruediv__")
__floordiv__ = _create_binary_propagating_op("__floordiv__")
__rfloordiv__ = _create_binary_propagating_op("__rfloordiv__")
__mod__ = _create_binary_propagating_op("__mod__")
__rmod__ = _create_binary_propagating_op("__rmod__")
__divmod__ = _create_binary_propagating_op("__divmod__")
__rdivmod__ = _create_binary_propagating_op("__rdivmod__")
__pow__ = _create_binary_propagating_op("__pow__")
__rpow__ = _create_binary_propagating_op("__rpow__")
# __lshift__ = _create_binary_propagating_op("__add__")
# __rshift__ = _create_binary_propagating_op("__add__")
__eq__ = _create_binary_propagating_op("__eq__")
__ne__ = _create_binary_propagating_op("__ne__")
__le__ = _create_binary_propagating_op("__le__")
__lt__ = _create_binary_propagating_op("__lt__")
__gt__ = _create_binary_propagating_op("__gt__")
__ge__ = _create_binary_propagating_op("__ge__")
# Unary ops
__neg__ = _create_unary_propagating_op("__neg__")
__pos__ = _create_unary_propagating_op("__pos__")
__abs__ = _create_unary_propagating_op("__abs__")
__invert__ = _create_unary_propagating_op("__invert__")
# Logical ops using Kleene logic
def __and__(self, other):
if other is False:
return False
elif other is True or other is NA:
return NA
else:
return NotImplemented
__rand__ = __and__
def __or__(self, other):
if other is True:
return True
elif other is False or other is NA:
return NA
else:
return NotImplemented
__ror__ = __or__
def __xor__(self, other):
if other is False or other is True or other is NA:
return NA
return NotImplemented
__rxor__ = __xor__
NA = NAType()