Code: Alles auswählen
class TimeDataManager():
def __init__(self, max_size):
self.max_size = max_size
self.data = pd.DataFrame(columns=["time", "...", "...", "...",])
self.data.set_index("time", inplace=True)
- Ist die Zeit der neuen Zeile gleich der Zeit der aktuellen Zeile mit Index 0: entferne die Zeile mit Index 0 (diese wird durch die neue ersetzt)
- Entferne am Ende des Dataframes so viele Zeilen, dass nur noch max_size - 1 übrig sind
- füge die neue Zeile vorne an
Code: Alles auswählen
def push(self, row: pd.DataFrame):
assert len(row) == 1
self.data.reset_index(inplace=True)
if(len(self.data) > 0 and row.iloc[0]['time'] == self.data.at[0, 'time']):
self.data = self.data.tail(self.max_size - 1)
self.data = self.data.head(self.max_size - 1)
self.data = pd.concat([row, self.data], ignore_index=True)
self.data.set_index("time", inplace=True)
Code: Alles auswählen
def to_dict(self, index) -> dict:
"""Returns the values of the row with the specified index as dict. \n
Input:
index -- The row-index of data that will be returned. It must be >= 0 and < len(data)."""
assert index >= 0 and index < len(self.data)
res = self.data[index:index+1].reset_index()
return res.to_dict('records')[0]
def at(self, index, field):
"""Returns the value in column field of the row with the specified index. \n
Input:
index -- The row-index of data that will be returned. It must be >= 0 and < len(data).\n
field -- The column name from which the value should be returned. """
return self.to_dict(index)[field]
Eine zweite Frage die sich mir stellt ist, ob der Ansatz mittels pd.DataFrame überhaupt der Richtige ist, oder ob ich das nicht lieber komplett anders umsetzten sollte.
Vielen Dank