From 95f08bc96df9463e1dbc89ac6d221e4f5e7205ba Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Mon, 30 Nov 2026 22:11:10 +0100 Subject: [PATCH] Enable PD in `flake8` and `ruff` --- noxfile.py | 1 + pyproject.toml | 7 +++++++ src/velofy/forecasts/methods/arima.py | 2 +- src/velofy/forecasts/methods/decomposition.py | 2 +- src/velofy/forecasts/methods/ets.py | 2 +- .../forecasts/methods/extrapolate_season.py | 4 ++-- .../forecasts/models/tactical/horizontal.py | 8 +++----- src/velofy/forecasts/models/tactical/other.py | 2 +- src/velofy/forecasts/models/tactical/realtime.py | 2 +- src/velofy/forecasts/models/tactical/vertical.py | 2 +- tests/forecasts/test_models.py | 4 ++-- uv.lock | 15 +++++++++++++++ 12 files changed, 36 insertions(+), 15 deletions(-) diff --git a/noxfile.py b/noxfile.py index 9b6fdff..c7227cf 100644 --- a/noxfile.py +++ b/noxfile.py @@ -212,6 +212,7 @@ def lint_code(session: nox.Session) -> None: "flake8-unused-arguments", "isort", "mypy", + "pandas-vet", "pep8-naming", "pydoclint[flake8]", "ruff", diff --git a/pyproject.toml b/pyproject.toml index 9353a20..aa90de3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -111,6 +111,7 @@ dev = [ "flake8-todos (>=0.3.1,<0.4.0)", "flake8-unused-arguments (>=0.0.14,<0.1.0)", "mypy (>=2.4.1,<3.0.0)", + "pandas-vet (>=2023.8.2)", "pep8-naming (>=0.15.1,<0.16.0)", "pydoclint[flake8] (>=0.12.0,<0.13.0)", "ruff (>=0.17.6,<0.18.0)", @@ -239,6 +240,8 @@ select = [ "T00", # flake8-unused-arguments => declared function arguments must be used "U100", + # pandas-vet => Idiomatic `pandas` + "PD0", # pep8-naming => PEP8 naming conventions "N8", # pydoclint => docstring matches implementation @@ -272,6 +275,8 @@ extend-ignore = [ # Comply with black's style. # Source: https://github.com/psf/black/blob/master/docs/compatible_configs.md#flake8 "E203", "W503", + # Every `.add()` looks like `pandas` to this rule (`ruff` omits it). + "PD005", # Let's not do `@pytest.mark.no_cover()` instead of `@pytest.mark.no_cover`. "PT023", # `ruff`'s RET504 does this better, as it allows statements in between. @@ -501,6 +506,8 @@ select = [ "ISC", # pep8-naming => PEP8 naming conventions "N", + # pandas-vet => Idiomatic `pandas` + "PD", # flake8-pytest-style => enforce a consistent style with pytest "PT", # flake8-quotes => use double quotes everywhere diff --git a/src/velofy/forecasts/methods/arima.py b/src/velofy/forecasts/methods/arima.py index 7aa88f6..4d916ac 100644 --- a/src/velofy/forecasts/methods/arima.py +++ b/src/velofy/forecasts/methods/arima.py @@ -38,7 +38,7 @@ def predict( # Re-seed R every time it is used to ensure reproducibility. robjects.r("set.seed(42)") - if training_ts.isnull().any(): + if training_ts.isna().any(): raise ValueError("`training_ts` must not contain `NaN` values") # Copy the data from Python to R. diff --git a/src/velofy/forecasts/methods/decomposition.py b/src/velofy/forecasts/methods/decomposition.py index 05f4e75..5a0a771 100644 --- a/src/velofy/forecasts/methods/decomposition.py +++ b/src/velofy/forecasts/methods/decomposition.py @@ -93,7 +93,7 @@ def stl( # noqa:C901,PLR0912,PLR0913,PLR0915 """ # Validate all arguments and set default values. - if time_series.isnull().any(): + if time_series.isna().any(): raise ValueError("`time_series` must not contain `NaN` values") if ns % 2 == 0 or ns < 7: diff --git a/src/velofy/forecasts/methods/ets.py b/src/velofy/forecasts/methods/ets.py index 39c443a..ce404bc 100644 --- a/src/velofy/forecasts/methods/ets.py +++ b/src/velofy/forecasts/methods/ets.py @@ -39,7 +39,7 @@ def predict( # Re-seed R every time it is used to ensure reproducibility. robjects.r("set.seed(42)") - if training_ts.isnull().any(): + if training_ts.isna().any(): raise ValueError("`training_ts` must not contain `NaN` values") # Copy the data from Python to R. diff --git a/src/velofy/forecasts/methods/extrapolate_season.py b/src/velofy/forecasts/methods/extrapolate_season.py index 7d70af7..6a5b846 100644 --- a/src/velofy/forecasts/methods/extrapolate_season.py +++ b/src/velofy/forecasts/methods/extrapolate_season.py @@ -36,7 +36,7 @@ def predict( ValueError: if `training_ts` contains `NaN` values or some predictions could not be made for time steps in the `forecast_interval` """ - if training_ts.isnull().any(): + if training_ts.isna().any(): raise ValueError("`training_ts` must not contain `NaN` values") extrapolated_ts = pd.Series(index=forecast_interval, dtype=float) @@ -60,7 +60,7 @@ def predict( idx += seasonal_lag # Sanity check. - if extrapolated_ts.isnull().any(): # pragma: no cover + if extrapolated_ts.isna().any(): # pragma: no cover raise ValueError("missing predictions in the `forecast_interval`") return pd.DataFrame( diff --git a/src/velofy/forecasts/models/tactical/horizontal.py b/src/velofy/forecasts/models/tactical/horizontal.py index 5d461a4..158f60d 100644 --- a/src/velofy/forecasts/models/tactical/horizontal.py +++ b/src/velofy/forecasts/models/tactical/horizontal.py @@ -65,7 +65,7 @@ class HorizontalETSModel(base.ForecastingModelABC): predictions.insert(loc=0, column="actual", value=actuals_ts) # Sanity checks. - if predictions.isnull().sum().any(): # pragma: no cover + if predictions.isna().sum().any(): # pragma: no cover raise RuntimeError("missing predictions in hets model") if predict_at not in predictions.index: # pragma: no cover raise RuntimeError("missing prediction for `predict_at`") @@ -121,7 +121,7 @@ class HorizontalSMAModel(base.ForecastingModelABC): predictions = pd.DataFrame( data={ "actual": actuals_ts, - "prediction": training_ts.values.mean(), + "prediction": training_ts.to_numpy().mean(), "low80": float("NaN"), "high80": float("NaN"), "low95": float("NaN"), @@ -131,9 +131,7 @@ class HorizontalSMAModel(base.ForecastingModelABC): ) # Sanity checks. - if ( - predictions[["actual", "prediction"]].isnull().any().any() - ): # pragma: no cover + if predictions[["actual", "prediction"]].isna().any().any(): # pragma: no cover raise RuntimeError("missing predictions in hsma model") if predict_at not in predictions.index: # pragma: no cover raise RuntimeError("missing prediction for `predict_at`") diff --git a/src/velofy/forecasts/models/tactical/other.py b/src/velofy/forecasts/models/tactical/other.py index f515365..1e617b6 100644 --- a/src/velofy/forecasts/models/tactical/other.py +++ b/src/velofy/forecasts/models/tactical/other.py @@ -73,7 +73,7 @@ class TrivialModel(base.ForecastingModelABC): ) # Sanity checks. - if predictions["actual"].isnull().any(): # pragma: no cover + if predictions["actual"].isna().any(): # pragma: no cover raise RuntimeError("missing actuals in trivial model") if predict_at not in predictions.index: # pragma: no cover raise RuntimeError("missing prediction for `predict_at`") diff --git a/src/velofy/forecasts/models/tactical/realtime.py b/src/velofy/forecasts/models/tactical/realtime.py index e1399e6..c97b75c 100644 --- a/src/velofy/forecasts/models/tactical/realtime.py +++ b/src/velofy/forecasts/models/tactical/realtime.py @@ -115,7 +115,7 @@ class RealtimeARIMAModel(base.ForecastingModelABC): # Sanity checks. if len(predictions) != 1: # pragma: no cover raise RuntimeError("real-time models should predict exactly one time step") - if predictions.isnull().sum().any(): # pragma: no cover + if predictions.isna().sum().any(): # pragma: no cover raise RuntimeError("missing predictions in rtarima model") if predict_at not in predictions.index: # pragma: no cover raise RuntimeError("missing prediction for `predict_at`") diff --git a/src/velofy/forecasts/models/tactical/vertical.py b/src/velofy/forecasts/models/tactical/vertical.py index d41da24..c24c9f4 100644 --- a/src/velofy/forecasts/models/tactical/vertical.py +++ b/src/velofy/forecasts/models/tactical/vertical.py @@ -115,7 +115,7 @@ class VerticalARIMAModel(base.ForecastingModelABC): # Sanity checks. if len(predictions) <= 1: # pragma: no cover raise RuntimeError("vertical models should predict several time steps") - if predictions.isnull().sum().any(): # pragma: no cover + if predictions.isna().sum().any(): # pragma: no cover raise RuntimeError("missing predictions in varima model") if predict_at not in predictions.index: # pragma: no cover raise RuntimeError("missing prediction for `predict_at`") diff --git a/tests/forecasts/test_models.py b/tests/forecasts/test_models.py index f741ba1..30b3f35 100644 --- a/tests/forecasts/test_models.py +++ b/tests/forecasts/test_models.py @@ -131,8 +131,8 @@ class TestGenericForecastingModelProperties: train_horizon=test_config.LONG_TRAIN_HORIZON, ) - assert not result["actual"].isnull().any() - assert not result["prediction"].isnull().any() + assert not result["actual"].isna().any() + assert not result["prediction"].isna().any() @pytest.mark.db @pytest.mark.r diff --git a/uv.lock b/uv.lock index 3cd83de..ead69a0 100644 --- a/uv.lock +++ b/uv.lock @@ -2409,6 +2409,19 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/08/ab/cce173d73200410f06306a89dbbb5f2125bd64f66ca5eff393d53d813288/pandas-3.1.1-cp315-cp315t-win_arm64.whl", hash = "sha256:4e6f9a3036fc6a40c5ba00efe4092a45844212bdb6a63eda4ad5a5a4c8da45eb", size = 10604158, upload-time = "2026-11-04T15:07:59.733Z" }, ] +[[package]] +name = "pandas-vet" +version = "2023.8.2" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "attrs" }, + { name = "flake8" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/f5/57/2c8350ff48cece1f6009012a0f42413e9e6d5ca54bec7f3b5726b6e33e1b/pandas_vet-2023.8.2.tar.gz", hash = "sha256:ee6b80b40f5a74de41c29393e4dce373447b363faaa3e0677e4f36799ea865c3", size = 9029, upload-time = "2023-08-11T15:09:03.945Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/17/dd/128ade78c67d4ac1a6202f320112aaa7934eaf8f7b0d861517e7c960dd3f/pandas_vet-2023.8.2-py3-none-any.whl", hash = "sha256:761c07b4479f24fc507465047111c7d21374ec678b7aba58e454fd5d3f8055fe", size = 7396, upload-time = "2023-08-11T15:09:04.842Z" }, +] + [[package]] name = "pandocfilters" version = "1.5.1" @@ -3846,6 +3859,7 @@ dev = [ { name = "isort" }, { name = "mypy" }, { name = "nox" }, + { name = "pandas-vet" }, { name = "pep8-naming" }, { name = "pre-commit" }, { name = "pydoclint", extra = ["flake8"] }, @@ -3916,6 +3930,7 @@ dev = [ { name = "isort", specifier = ">=9.0.2,<10.0.0" }, { name = "mypy", specifier = ">=2.4.1,<3.0.0" }, { name = "nox", specifier = ">=2026.10.20" }, + { name = "pandas-vet", specifier = ">=2023.8.2" }, { name = "pep8-naming", specifier = ">=0.15.1,<0.16.0" }, { name = "pre-commit", specifier = ">=4.7.0,<5.0.0" }, { name = "pydoclint", extras = ["flake8"], specifier = ">=0.12.0,<0.13.0" },