ndarray extended to supply .name and other arbitrary properties in ._opts dict.
| 94 | |
| 95 | |
| 96 | class _Array(np.ndarray): |
| 97 | """ |
| 98 | ndarray extended to supply .name and other arbitrary properties |
| 99 | in ._opts dict. |
| 100 | """ |
| 101 | def __new__(cls, array, *, name=None, **kwargs): |
| 102 | obj = np.asarray(array).view(cls) |
| 103 | obj.name = name or array.name |
| 104 | obj._opts = kwargs |
| 105 | return obj |
| 106 | |
| 107 | def __array_finalize__(self, obj): |
| 108 | if obj is not None: |
| 109 | self.name = getattr(obj, 'name', '') |
| 110 | self._opts = getattr(obj, '_opts', {}) |
| 111 | |
| 112 | # Make sure properties name and _opts are carried over |
| 113 | # when (un-)pickling. |
| 114 | def __reduce__(self): |
| 115 | value = super().__reduce__() |
| 116 | return value[:2] + (value[2] + (self.__dict__,),) |
| 117 | |
| 118 | def __setstate__(self, state): |
| 119 | self.__dict__.update(state[-1]) |
| 120 | super().__setstate__(state[:-1]) |
| 121 | |
| 122 | def __bool__(self): |
| 123 | try: |
| 124 | return bool(self[-1]) |
| 125 | except IndexError: |
| 126 | return super().__bool__() |
| 127 | |
| 128 | def __float__(self): |
| 129 | try: |
| 130 | return float(self[-1]) |
| 131 | except IndexError: |
| 132 | return super().__float__() |
| 133 | |
| 134 | def to_series(self): |
| 135 | warnings.warn("`.to_series()` is deprecated. For pd.Series conversion, use accessor `.s`") |
| 136 | return self.s |
| 137 | |
| 138 | @property |
| 139 | def s(self) -> pd.Series: |
| 140 | values = np.atleast_2d(self) |
| 141 | index = self._opts['index'][:values.shape[1]] |
| 142 | return pd.Series(values[0], index=index, name=self.name) |
| 143 | |
| 144 | @property |
| 145 | def df(self) -> pd.DataFrame: |
| 146 | values = np.atleast_2d(np.asarray(self)) |
| 147 | index = self._opts['index'][:values.shape[1]] |
| 148 | df = pd.DataFrame(values.T, index=index, columns=[self.name] * len(values)) |
| 149 | return df |
| 150 | |
| 151 | |
| 152 | class _Indicator(_Array): |
no outgoing calls