Quellcode durchsuchen

Store dates from history with y NaN for make_future_dataframe

Ben Letham vor 8 Jahren
Ursprung
Commit
5677d8c7ce
2 geänderte Dateien mit 9 neuen und 5 gelöschten Zeilen
  1. 5 3
      R/R/prophet.R
  2. 4 2
      python/fbprophet/forecaster.py

+ 5 - 3
R/R/prophet.R

@@ -107,7 +107,8 @@ prophet <- function(df = df,
     changepoints.t = NULL,
     stan.fit = NULL,
     params = list(),
-    history = NULL
+    history = NULL,
+    history.dates = NULL
   )
   validate_inputs(m)
   class(m) <- append("prophet", class(m))
@@ -455,6 +456,7 @@ logistic_growth_init <- function(df) {
 fit.prophet <- function(m, df, ...) {
   history <- df %>%
     dplyr::filter(!is.na(y))
+  m$history.dates <- sort(zoo::as.Date(df$ds))
 
   out <- setup_dataframe(m, history, initialize_scales = TRUE)
   history <- out$df
@@ -839,10 +841,10 @@ sample_predictive_trend <- function(model, df, iteration) {
 #' @export
 make_future_dataframe <- function(m, periods, freq = 'd',
                                   include_history = TRUE) {
-  dates <- seq(max(m$history$ds), length.out = periods + 1, by = freq)
+  dates <- seq(max(m$history.dates), length.out = periods + 1, by = freq)
   dates <- dates[2:(periods + 1)]  # Drop the first, which is max(history$ds)
   if (include_history) {
-    dates <- c(m$history$ds, dates)
+    dates <- c(m$history.dates, dates)
   }
   return(data.frame(ds = dates))
 }

+ 4 - 2
python/fbprophet/forecaster.py

@@ -87,6 +87,7 @@ class Prophet(object):
         self.stan_fit = None
         self.params = {}
         self.history = None
+        self.history_dates = None
         self.validate_inputs()
 
     def validate_inputs(self):
@@ -350,6 +351,7 @@ class Prophet(object):
         The fitted Prophet object.
         """
         history = df[df['y'].notnull()].copy()
+        self.history_dates = pd.to_datetime(df['ds']).sort_values()
 
         history = self.setup_dataframe(history, initialize_scales=True)
         self.history = history
@@ -608,7 +610,7 @@ class Prophet(object):
         return trend * self.y_scale
 
     def make_future_dataframe(self, periods, freq='D', include_history=True):
-        last_date = self.history['ds'].max()
+        last_date = self.history_dates.max()
         dates = pd.date_range(
             start=last_date,
             periods=periods + 1,  # An extra in case we include start
@@ -617,7 +619,7 @@ class Prophet(object):
         dates = dates[:periods]  # Return correct number of periods
 
         if include_history:
-            dates = np.concatenate((np.array(self.history['ds']), dates))
+            dates = np.concatenate((np.array(self.history_dates), dates))
 
         return pd.DataFrame({'ds': dates})