Graceful error on empty input dataframes

This commit is contained in:
Ben Letham 2017-09-01 15:50:19 -07:00
parent 8066634cb4
commit 439efb7209
2 changed files with 10 additions and 0 deletions

View file

@ -926,6 +926,9 @@ fit.prophet <- function(m, df, ...) {
}
history <- df %>%
dplyr::filter(!is.na(y))
if (nrow(history) < 2) {
stop("Dataframe has less than 2 non-NA rows.")
}
m$history.dates <- sort(set_date(df$ds))
out <- setup_dataframe(m, history, initialize_scales = TRUE)
@ -1047,6 +1050,9 @@ predict.prophet <- function(object, df = NULL, ...) {
if (is.null(df)) {
df <- object$history
} else {
if (nrow(df) == 0) {
stop("Dataframe has no rows.")
}
out <- setup_dataframe(object, df)
df <- out$df
}

View file

@ -769,6 +769,8 @@ class Prophet(object):
raise Exception('Prophet object can only be fit once. '
'Instantiate a new object.')
history = df[df['y'].notnull()].copy()
if history.shape[0] < 2:
raise ValueError('Dataframe has less than 2 non-NaN rows.')
self.history_dates = pd.to_datetime(df['ds']).sort_values()
history = self.setup_dataframe(history, initialize_scales=True)
@ -863,6 +865,8 @@ class Prophet(object):
if df is None:
df = self.history.copy()
else:
if df.shape[0] == 0:
raise ValueError('Dataframe has no rows.')
df = self.setup_dataframe(df.copy())
df['trend'] = self.predict_trend(df)