11import datetime as dt
2+ from pathlib import Path
23
34import pandas as pd
45
@@ -33,7 +34,7 @@ def get_dymo_time_index(df):
3334 return list (pd .Series (sec_dt ).cumsum ())
3435
3536
36- def df_to_combitimetable (df , filename ):
37+ def write_combitt_from_df (df : pd . DataFrame , file_path : Path | str ):
3738 """
3839 Write a text file compatible with modelica Combitimetables object from a
3940 Pandas DataFrame with a DatetimeIndex. DataFrames with non monotonically increasing
@@ -45,18 +46,15 @@ def df_to_combitimetable(df, filename):
4546 """
4647 if not isinstance (df , pd .DataFrame ):
4748 raise ValueError (f"df must be an instance of pandas DataFrame. Got { type (df )} " )
48- if not isinstance (df .index , pd .DatetimeIndex ):
49- raise ValueError (
50- f"DataFrame index must be an instance of DatetimeIndex. " f"Got { type (df )} "
51- )
49+
5250 if not df .index .is_monotonic_increasing :
5351 raise ValueError (
5452 "df DateTimeIndex is not monotonically increasing, this will"
5553 "cause Modelica to crash."
5654 )
5755
5856 df = df .copy ()
59- with open (filename , "w" ) as file :
57+ with open (file_path , "w" ) as file :
6058 file .write ("#1 \n " )
6159 line = ""
6260 line += f"double table1({ df .shape [0 ]} , { df .shape [1 ] + 1 } )\n "
@@ -65,6 +63,7 @@ def df_to_combitimetable(df, filename):
6563 line += f"\t ({ i + 1 } ){ col } "
6664 file .write (f"{ line } \n " )
6765
68- df .index = datetime_to_seconds (df .index )
66+ if isinstance (df .index , pd .DatetimeIndex ):
67+ df .index = datetime_to_seconds (df .index )
6968
7069 file .write (df .to_csv (header = False , sep = "\t " , lineterminator = "\n " ))
0 commit comments