Skip to content

Commit 338361e

Browse files
committed
time_series: allow variable number of columns in a file
This commit allows an extra 10 columns to be added to the output of time series in the middle of the run compared to the start of the run. Prior to that commit, increasing the number of columns mid-run would result in a truncated output from the legacy parser. This commit also correctly attributes the names from the header line for columns that are not defined in `phyvars.TIME`, extraneous columns added mid-run being simply numbered.
1 parent 05023e5 commit 338361e

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

src/stagpy/stagyyparsers.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,24 @@ def time_series(timefile: Path, colnames: list[str]) -> DataFrame | None:
6969
"""
7070
if not timefile.is_file():
7171
return None
72+
73+
with timefile.open() as fid:
74+
names_in_file = fid.readline().strip().split()
75+
_tidy_names(
76+
colnames,
77+
len(names_in_file) + 9, # extra columns in case some were added mid-run
78+
extra_names=names_in_file[len(colnames) + 1 :],
79+
)
80+
colnames.insert(0, "istep")
81+
7282
data = pd.read_csv(
7383
timefile,
7484
sep=r"\s+",
7585
dtype=str,
7686
header=None,
87+
names=colnames,
7788
skiprows=1,
78-
index_col=0,
89+
index_col="istep",
7990
engine="c",
8091
memory_map=True,
8192
on_bad_lines="skip",
@@ -94,10 +105,7 @@ def time_series(timefile: Path, colnames: list[str]) -> DataFrame | None:
94105
if rows_to_del:
95106
rows_to_keep = set(range(len(data))) - set(rows_to_del)
96107
data = data.take(list(rows_to_keep))
97-
98-
ncols = data.shape[1]
99-
_tidy_names(colnames, ncols)
100-
data.columns = pd.Index(colnames)
108+
data.dropna(axis="columns", how="all", inplace=True)
101109

102110
return data
103111

0 commit comments

Comments
 (0)