Bases: NestedDict
, _Arithmetics
A NestedDict that supports arithmetics.
Other methods are included that make DataDict similar to DataFrames.
Source code in ndicts\data_dict.py
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 | class DataDict(NestedDict, _Arithmetics):
"""A NestedDict that supports arithmetics.
Other methods are included that make DataDict similar to DataFrames."""
def _arithmetic_operation(self, other, operation: str, symbol: str):
"""Implements any arithmetic operation, just pass the underlying method as string
The symbol, passed as a string, will appear in the exception message if any
The operation is performed only between NestedProperties or with numbers"""
result = self.copy()
if isinstance(other, self.__class__):
for other_key, other_value in other.items():
if other_key in self:
for key, value in self.extract[other_key].items():
result[key] = getattr(value, operation).__call__(other_value)
else:
raise TypeError(
f"unsupported operand type(s) for {symbol}: incompatible keys"
)
return result
elif isinstance(other, Number):
for key, value in self.items():
result[key] = getattr(value, operation).__call__(other)
return result
return TypeError(
f"unsupported operand type(s) for {symbol}: {type(self)} and {type(other)}"
)
def apply(self, func: Callable, inplace: bool = False):
"""Apply func to all values."""
if inplace:
for key, leaf in self.items():
self[key] = func(leaf)
else:
new_self = self.copy()
for key, leaf in new_self.items():
new_self[key] = func(leaf)
return new_self
def reduce(self, func: Callable, *initial: Any):
"""Pass func and initial to functools.reduce and apply it to all values."""
return reduce(func, self.values(), *initial)
def total(self):
"""Returns sum of all values."""
return sum(self.values())
def mean(self) -> Number:
"""Returns mean of all values."""
return self.total() / len(self)
def std(self) -> Number:
"""Returns standard deviation of all values."""
step = self.reduce(lambda a, b: a + (b - self.mean()) ** 2, 0)
step /= len(self) - 1
return step**0.5
|
apply(func, inplace=False)
Apply func to all values.
Source code in ndicts\data_dict.py
77
78
79
80
81
82
83
84
85
86 | def apply(self, func: Callable, inplace: bool = False):
"""Apply func to all values."""
if inplace:
for key, leaf in self.items():
self[key] = func(leaf)
else:
new_self = self.copy()
for key, leaf in new_self.items():
new_self[key] = func(leaf)
return new_self
|
mean()
Returns mean of all values.
Source code in ndicts\data_dict.py
| def mean(self) -> Number:
"""Returns mean of all values."""
return self.total() / len(self)
|
reduce(func, *initial)
Pass func and initial to functools.reduce and apply it to all values.
Source code in ndicts\data_dict.py
| def reduce(self, func: Callable, *initial: Any):
"""Pass func and initial to functools.reduce and apply it to all values."""
return reduce(func, self.values(), *initial)
|
std()
Returns standard deviation of all values.
Source code in ndicts\data_dict.py
| def std(self) -> Number:
"""Returns standard deviation of all values."""
step = self.reduce(lambda a, b: a + (b - self.mean()) ** 2, 0)
step /= len(self) - 1
return step**0.5
|
total()
Returns sum of all values.
Source code in ndicts\data_dict.py
| def total(self):
"""Returns sum of all values."""
return sum(self.values())
|