Ver código fonte

Graceful error on empty input dataframes

Ben Letham 8 anos atrás
pai
commit
439efb7209
2 arquivos alterados com 10 adições e 0 exclusões
  1. 6 0
      R/R/prophet.R
  2. 4 0
      python/fbprophet/forecaster.py

+ 6 - 0
R/R/prophet.R

@@ -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
   }

+ 4 - 0
python/fbprophet/forecaster.py

@@ -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)