Skip to content

Commit 51c1e72

Browse files
committed
Handling plot_forecast_from_file doesn't have datetime index
1 parent 3f986e6 commit 51c1e72

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

src/eruption_forecast/plots/forecast_plots.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
def plot_forecast(
4040
df: pd.DataFrame,
41-
label_df: pd.DataFrame | pd.Series,
41+
label_df: pd.DataFrame | pd.Series | None = None,
4242
title: str | None = None,
4343
fig_width: float = 12,
4444
fig_height: float = 3,
@@ -96,7 +96,12 @@ def plot_forecast(
9696
}
9797
)
9898

99-
if label_df is not None:
99+
# Ensure df have pd.DatetimeIndex
100+
if not isinstance(df.index, pd.DatetimeIndex):
101+
if label_df is None or len(label_df) == 0:
102+
raise ValueError(
103+
"label_df is needed since concensus dataframe does not have pd.DatetimeIndex."
104+
)
100105
df = set_datetime_index(label_df, df)
101106

102107
# Maintain backward compatibility
@@ -253,7 +258,7 @@ def plot_forecast(
253258

254259
def plot_forecast_from_file(
255260
consensus_file: str,
256-
label_file: str,
261+
label_file: str | None = None,
257262
title: str | None = None,
258263
fig_width: float = 12,
259264
fig_height: float = 3,
@@ -285,10 +290,12 @@ def plot_forecast_from_file(
285290
Returns:
286291
plt.Figure: Matplotlib figure object with three vertically stacked subplots.
287292
"""
288-
df = pd.read_csv(consensus_file, index_col="id")
289-
label_df = pd.read_csv(label_file, index_col=0, parse_dates=True)
290-
291-
df = set_datetime_index(label_df, df)
293+
df = pd.read_csv(consensus_file, index_col=0, parse_dates=True)
294+
label_df = (
295+
pd.read_csv(label_file, index_col=0, parse_dates=True)
296+
if label_file
297+
else pd.DataFrame()
298+
)
292299

293300
return plot_forecast(
294301
df,

0 commit comments

Comments
 (0)