Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions python/prophet/forecaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@
logger.setLevel(logging.INFO)
NANOSECONDS_TO_SECONDS = 1000 * 1000 * 1000


def _to_datetime(values: pd.Series) -> pd.Series:
"""pd.to_datetime that also converts pyarrow-backed timestamps to numpy datetime64.

pd.to_datetime leaves some pyarrow timestamp columns (for example
timestamp[ns][pyarrow] from pd.read_parquet(..., dtype_backend="pyarrow"))
as pyarrow, and the numpy-based code downstream fails on them.
"""
ds = pd.to_datetime(values)
if isinstance(ds.dtype, getattr(pd, "ArrowDtype", ())):
ds = pd.Series(pd.DatetimeIndex(ds), index=ds.index, name=ds.name)
return ds

class Prophet:
"""Prophet forecaster.

Expand Down Expand Up @@ -353,7 +366,7 @@ def setup_dataframe(self, df: pd.DataFrame, initialize_scales: bool = False) ->
raise ValueError('Found infinity in column y.')
if df['ds'].dtype == np.int64:
df['ds'] = df['ds'].astype(str)
df['ds'] = pd.to_datetime(df['ds'])
df['ds'] = _to_datetime(df['ds'])
if df['ds'].dt.tz is not None:
raise ValueError(
'Column ds has timezone specified, which is not supported. '
Expand Down Expand Up @@ -1350,7 +1363,7 @@ def preprocess(self, df: pd.DataFrame, **kwargs: Any) -> ModelInputData:
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(pd.Series(df['ds'].unique(), name='ds')).sort_values()
self.history_dates = _to_datetime(pd.Series(df['ds'].unique(), name='ds')).sort_values()

self.history = self.setup_dataframe(history, initialize_scales=True)
self.set_auto_seasonalities()
Expand Down
23 changes: 23 additions & 0 deletions python/prophet/tests/test_prophet.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,29 @@ def test_auto_daily_seasonality(self, daily_univariate_ts, subdaily_univariate_t
m.fit(daily_univariate_ts)
assert "daily" not in m.seasonalities

@pytest.mark.parametrize("unit", ["ns", "us"])
def test_fit_predict_pyarrow_ds(self, daily_univariate_ts, backend, unit):
# ds backed by pyarrow, e.g. from pd.read_parquet(..., dtype_backend="pyarrow")
pytest.importorskip("pyarrow")
arrow_dtype = f"timestamp[{unit}][pyarrow]"
train = daily_univariate_ts.head(100)
m_numpy = Prophet(stan_backend=backend).fit(train)
m_arrow = Prophet(stan_backend=backend).fit(train.astype({"ds": arrow_dtype}))
assert m_arrow.seasonalities == m_numpy.seasonalities
future = m_numpy.make_future_dataframe(10)
expected = m_numpy.predict(future)["yhat"]
np.testing.assert_allclose(m_arrow.predict(future)["yhat"], expected)
np.testing.assert_allclose(
m_arrow.predict(future.astype({"ds": arrow_dtype}))["yhat"], expected
)

def test_pyarrow_ds_with_timezone(self, daily_univariate_ts, backend):
pytest.importorskip("pyarrow")
train = daily_univariate_ts.head(100).copy()
train["ds"] = train["ds"].dt.tz_localize("UTC").astype("timestamp[ns, tz=UTC][pyarrow]")
with pytest.raises(ValueError, match="timezone"):
Prophet(stan_backend=backend).fit(train)

def test_set_seasonality_mode(self, backend):
# Setting attribute
m = Prophet(stan_backend=backend)
Expand Down