From 025f24474e203f42241b46403b708766463d38b0 Mon Sep 17 00:00:00 2001 From: Théo de la Hogue Date: Tue, 3 May 2022 16:39:32 +0200 Subject: Adding a method to convert a TimeStampedbuffer into panda dataframe. --- src/argaze/DataStructures.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/argaze/DataStructures.py b/src/argaze/DataStructures.py index 7b6ad9b..b1b47f5 100644 --- a/src/argaze/DataStructures.py +++ b/src/argaze/DataStructures.py @@ -100,22 +100,28 @@ class TimeStampedBuffer(collections.OrderedDict): return None def export_as_json(self, filepath): - """Write buffer content into a json file""" + """Write buffer content into a json file.""" try: with open(filepath, 'w', encoding='utf-8') as jsonfile: json.dump(self, jsonfile, ensure_ascii = False, default=vars) except: raise RuntimeError(f'Can\' write {filepath}') + def as_dataframe(self, exclude=[]): + """Convert buffer as pandas dataframe.""" + + df = pandas.DataFrame.from_dict(self.values()) + df.drop(exclude, inplace=True, axis=True) + df['timestamp'] = self.keys() + df.set_index('timestamp', inplace=True) + + return df + def export_as_csv(self, filepath, exclude=[]): - """Write buffer content into a csv file""" + """Write buffer content into a csv file.""" try: - df = pandas.DataFrame.from_dict(self.values()) - df.drop(exclude, inplace=True, axis=True) - df['timestamp'] = self.keys() - df.set_index('timestamp', inplace=True) - df.to_csv(filepath, index=True) + self.as_dataframe(exclude=exclude).to_csv(filepath, index=True) except: raise RuntimeError(f'Can\' write {filepath}') -- cgit v1.1