Type-check pandas with pandas-stubs
All checks were successful
CI / Checks (Python 3.13) (push) Successful in 1m54s

This commit is contained in:
Alexander Hess 2026-12-01 00:31:21 +01:00
commit 2bc2408694
Signed by: alexander
GPG key ID: B46EDB4DFC26805D
4 changed files with 28 additions and 9 deletions

View file

@ -220,6 +220,7 @@ def lint_code(session: nox.Session) -> None:
"flake8-unused-arguments",
"isort",
"mypy",
"pandas-stubs",
"pandas-vet",
"pep8-naming",
"pydoclint[flake8]",

View file

@ -119,6 +119,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-stubs (>=3.1.0.261029,<4.0.0)",
"pandas-vet (>=2023.8.2)",
"pep8-naming (>=0.15.1,<0.16.0)",
"pydoclint[flake8] (>=0.12.0,<0.13.0)",
@ -468,7 +469,6 @@ warn_unused_ignores = true
module = [
"geopy.*",
"googlemaps.*",
"pandas",
"rpy2.*",
"statsmodels.*",
"utm.*",

View file

@ -3,6 +3,7 @@
from __future__ import annotations
import datetime as dt
import typing as t
import pandas as pd
import sqlalchemy as sa
@ -42,7 +43,7 @@ class OrderHistory:
self._n_daily_time_steps = int(n_daily_time_steps)
# The `_data` are populated by `.aggregate_orders()`.
self._data = None
self._data: pd.DataFrame | None = None
@property
def time_step(self) -> int:
@ -148,15 +149,18 @@ class OrderHistory:
if data.empty:
return data
# The `index` holds the "pixel_id" and "start_at" levels
index = t.cast(pd.MultiIndex, data.index)
# Calculate the first and last "start_at" value ...
start_day = data.index.levels[1].min().date()
start_day = index.levels[1].min().date()
start = dt.datetime(
start_day.year,
start_day.month,
start_day.day,
pkg_config.SERVICE_START,
)
end_day = data.index.levels[1].max().date()
end_day = index.levels[1].max().date()
end = dt.datetime(
end_day.year,
end_day.month,
@ -167,7 +171,7 @@ class OrderHistory:
# The "start_at" values must lie within the operating hours.
gen = (
(pixel_id, start_at)
for pixel_id in sorted(data.index.levels[0])
for pixel_id in sorted(index.levels[0])
for start_at in pd.date_range(start, end, freq=f"{self._time_step}min")
if pkg_config.SERVICE_START <= start_at.hour < pkg_config.SERVICE_END
)
@ -317,7 +321,7 @@ class OrderHistory:
msg = "Not enough historic data for `predict_at`"
raise RuntimeError(msg)
actuals_ts = intra_pixel.loc[[predict_at], "n_orders"]
actuals_ts = intra_pixel.loc[intra_pixel.index == predict_at, "n_orders"]
if not len(actuals_ts): # pragma: no cover
msg = "`predict_at` is not in the order history"
raise LookupError(msg)
@ -510,7 +514,7 @@ class OrderHistory:
msg = "Not enough historic data for `predict_day`"
raise RuntimeError(msg)
actuals_ts = intra_pixel.loc[[predict_at], "n_orders"]
actuals_ts = intra_pixel.loc[intra_pixel.index == predict_at, "n_orders"]
if not len(actuals_ts): # pragma: no cover
msg = "`predict_at` is not in the order history"
raise LookupError(msg)

18
uv.lock generated
View file

@ -3,10 +3,10 @@ revision = 5
requires-python = ">=3.13, <4.0"
resolution-markers = [
"python_full_version >= '3.15' and sys_platform == 'win32'",
"python_full_version == '3.14.*' and sys_platform == 'win32'",
"python_full_version >= '3.15' and sys_platform == 'emscripten'",
"python_full_version == '3.14.*' and sys_platform == 'emscripten'",
"python_full_version >= '3.15' and sys_platform != 'emscripten' and sys_platform != 'win32'",
"python_full_version == '3.14.*' and sys_platform == 'win32'",
"python_full_version == '3.14.*' and sys_platform == 'emscripten'",
"python_full_version == '3.14.*' and sys_platform != 'emscripten' and sys_platform != 'win32'",
"python_full_version < '3.14' and sys_platform == 'win32'",
"python_full_version < '3.14' and sys_platform == 'emscripten'",
@ -2502,6 +2502,18 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/53/d0/fb6a3def92ab15333b7755e1a5e637a9b9aa8683ed7a29b59fd14d490b38/pandas-3.1.2-cp315-cp315t-win_arm64.whl", hash = "sha256:677cb21de2defd3a4f14096e6a04280a8687bcef91a1b18338508178a78ca338", size = 10616613, upload-time = "2026-11-30T17:15:16.113Z" },
]
[[package]]
name = "pandas-stubs"
version = "3.1.0.261029"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "numpy" },
]
sdist = { url = "https://files.pythonhosted.org/packages/f9/f8/0346629264426a6693affbc9a0fb52143acfa38112d569f68da514ea503a/pandas_stubs-3.1.0.261029.tar.gz", hash = "sha256:dcfb5b14d399beae72cce558db6d2a0d89c10301275557f1c00d5393e9cb190e", size = 119466, upload-time = "2026-10-29T17:09:31.352Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/c0/2c/ecbc04b2a8d05d737351a54398f843223cb66edf42ec5c0fcc6198f17d3a/pandas_stubs-3.1.0.261029-py3-none-any.whl", hash = "sha256:70f631f2d0781c4500b1cd5f84cab7d7279a3d114674d69655536a4a99a3a67f", size = 179851, upload-time = "2026-10-29T17:09:30.112Z" },
]
[[package]]
name = "pandas-vet"
version = "2023.8.2"
@ -3973,6 +3985,7 @@ dev = [
{ name = "isort" },
{ name = "mypy" },
{ name = "nox" },
{ name = "pandas-stubs" },
{ name = "pandas-vet" },
{ name = "pep8-naming" },
{ name = "pre-commit" },
@ -4053,6 +4066,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-stubs", specifier = ">=3.1.0.261029,<4.0.0" },
{ 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" },