Skip to content

Instantly share code, notes, and snippets.

@decatur
Last active September 18, 2020 09:32
Show Gist options
  • Save decatur/fb83ba41d184b5847fca2c4395205489 to your computer and use it in GitHub Desktop.
Save decatur/fb83ba41d184b5847fca2c4395205489 to your computer and use it in GitHub Desktop.
typed pandas
import numpy as np
class Plant:
"""
KISS implementation of a time series data class with a ndarray backing.
Think of Pandas with typing.
Properties can be also set by scalar values.
"""
def __init__(self, dim: int):
self.buffer = np.ndarray([dim, 6], dtype=float)
def _fill(self, x: np.ndarray or float):
if type(x) in (float, int):
return np.repeat(x, self.buffer.shape[0])
return x
@property
def power_min(self) -> np.ndarray:
return self.buffer[:, 0]
@power_min.setter
def power_min(self, x: np.ndarray or float):
x = self._fill(x)
assert (x > 0).all()
self.buffer[:, 0] = x
@property
def power_max(self) -> np.ndarray:
return self.buffer[:, 0]
@power_max.setter
def power_max(self, x: np.ndarray or float):
x = self._fill(x)
assert (x > 0).all()
self.buffer[:, 0] = x
plant = Plant(10)
plant.power_max = 3
print(plant.power_max)
print(plant.__dict__.keys()) # -> ['buffer']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment