-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaver_func.py
More file actions
38 lines (30 loc) · 1.13 KB
/
saver_func.py
File metadata and controls
38 lines (30 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""Saver function"""
from typing import Union
import pandas as pd
class SaveData:
counter = 0
def __init__(self):
try:
# Initialize the log file
with open("save_log.csv", "x") as f:
f.write("name,time,mem_address\n")
except:
pass
@classmethod
def save(cls, val: Union[pd.DataFrame, pd.Series], suffix: str = ""):
"""Save a pandas DataFrame or Series to a csv file.
Args:
val (Union[pd.DataFrame, pd.Series]): The DataFrame or Series to save.
name (str, optional): Optional suffix for the file. Defaults to "".
"""
assert isinstance(
val, (pd.DataFrame, pd.Series)
), "val must be a DataFrame or Series"
# Only dataframes have .to_html() method
if isinstance(val, pd.Series):
val = val.to_frame()
file_name = f"save_{cls.counter}{'_' + suffix if suffix else ''}"
val.to_html(file_name + ".html")
with open("save_log.csv", "a") as f:
f.write(f"{file_name}, {pd.Timestamp.now()}, {hex(id(val))}\n")
cls.counter += 1