Repository navigation
fix: convert pyarrow-backed ds to datetime64 before fitting - #2780
Open
vishalhabib99 wants to merge 1 commit into
Open
vishalhabib99 wants to merge 1 commit into
vishalhabib99 wants to merge 1 commit into
Conversation
A ds column of type timestamp[ns][pyarrow] (for example from pd.read_parquet(..., dtype_backend="pyarrow")) stays pyarrow after pd.to_datetime, and fit then fails in set_auto_seasonalities with "'ArrowExtensionArray' object has no attribute 'nonzero'", and in fourier_series because pyarrow cannot cast the nanosecond values to float. Convert pyarrow-backed timestamps to numpy datetime64 where ds is parsed (setup_dataframe and history_dates). Timezone-aware columns keep their timezone, so the existing timezone check still raises.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fitfails whendsis a pyarrow-backed timestamp column, which is whatpd.read_parquet(..., dtype_backend="pyarrow")returns for a datetime column written by pandas.Why
setup_dataframeparsesdswithpd.to_datetime, which leavestimestamp[ns][pyarrow]as pyarrow (with pandas 3.1.1 it convertstimestamp[us][pyarrow]todatetime64[us], but not thensvariant). Two places then assume a numpy-backed column:set_auto_seasonalitiescalls.nonzero()ondt.values, which is anArrowExtensionArray.fourier_seriescalls.dt.total_seconds(), which on pyarrow durations casts the nanosecond integers to double and raisesArrowInvalid: Integer value ... not in rangebecause they're above 2**53.Fix
A small
_to_datetimehelper runspd.to_datetimeand, when the result has anArrowDtype, wraps it inpd.DatetimeIndexto get a numpydatetime64column. It's used in the two places that parseds:setup_dataframe(fit and predict) andhistory_datesinpreprocess. A timezone-aware pyarrow column becomesdatetime64[ns, tz], so the existing "Column ds has timezone specified" check still raises. For anything that isn't pyarrow-backed the behavior is unchanged. Thegetattr(pd, "ArrowDtype", ())keeps it working on pandas versions before 1.5.With the change, a model fit on the pyarrow column gives the same seasonalities and the same
yhatas one fit on the numpy column.predict,cross_validation,performance_metrics,model_to_json/model_from_json,plotandplot_componentsall work on the pyarrow-backed data.Tests
test_fit_predict_pyarrow_ds[ns|us]: fits ontimestamp[ns]andtimestamp[us]pyarrowds, checks seasonalities andyhatagainst the numpy-backed model, and also predicts on a pyarrow-backed future frame. Thenscase fails on main with theAttributeErrorabove. Theuscase already passes on main becausepd.to_datetimeconverts that one, and it's there to keep it working.test_pyarrow_ds_with_timezone: a tz-aware pyarrowdsstill raises the timezoneValueError.pytest.importorskip("pyarrow"). CI installs.[dev,parallel], which brings in pyarrow throughdask[dataframe], so they run there.Full suite (
test_prophet,test_diagnostics,test_serialize,test_utilities,test_metrics,test_plot,test_backend_loading): 141 passed, 6 skipped with the change; on main the only failure is the newnstest.pyrefly checkreports the same 5 errors before and after, all missing optional modules in my environment (dask, plotly).Tested on macOS arm64, Python 3.12, pandas 3.1.1, pyarrow 26.0.1, cmdstanpy backend, running main's Python code on the 1.5.0 wheel's compiled Stan model.