Selaa lähdekoodia

Fixed pandas typing - added pandas-stubs

Nicholas Schense 2 viikkoa sitten
vanhempi
commit
43a2aee2bf
3 muutettua tiedostoa jossa 41 lisäystä ja 4 poistoa
  1. 16 4
      model/dataset.py
  2. 5 0
      pyproject.toml
  3. 20 0
      uv.lock

+ 16 - 4
model/dataset.py

@@ -2,7 +2,7 @@ import math
 import pathlib as pl
 import random
 import re
-from typing import Callable, Dict, Iterator, List, Tuple
+from typing import Callable, Dict, Iterator, List, Tuple, cast
 
 import nibabel as nib
 import pandas as pd
@@ -47,9 +47,21 @@ def xls_pre(df: pd.DataFrame) -> pd.DataFrame:
 
     # Work on an explicit copy so column assignment writes back reliably instead
     # of raising SettingWithCopyWarning against a view of ``df``.
-    data = df[["Image Data ID", "Sex", "Age (current)"]].copy()
-    data["Sex"] = data["Sex"].str.strip()
-    data = data.replace({"M": 0, "F": 1})
+    #
+    # The ``cast``s are needed because pandas ships no type stubs: basedpyright
+    # widens ``.copy()``/``.replace()``/``__getitem__`` to
+    # ``DataFrame | Series | Unknown``, which then has no ``.str`` accessor and
+    # isn't assignable to the declared return type. ``cast`` asserts the concrete
+    # pandas type at each step without any runtime effect.
+    data = cast(pd.DataFrame, df[["Image Data ID", "Sex", "Age (current)"]].copy())
+
+    # ``.str`` is pandas' vectorized string accessor: it applies a str method
+    # element-wise over every value in the Series. Here it strips surrounding
+    # whitespace from each Sex entry (e.g. " M " -> "M") before encoding.
+    sex_col = cast(pd.Series, data["Sex"])
+    data["Sex"] = sex_col.str.strip()
+
+    data = cast(pd.DataFrame, data.replace({"M": 0, "F": 1}))
 
     return data
 

+ 5 - 0
pyproject.toml

@@ -28,3 +28,8 @@ dependencies = [
 
 [tool.mypy]
 exclude = [".venv/**"]
+
+[dependency-groups]
+dev = [
+    "pandas-stubs>=3.0.3.260530",
+]

+ 20 - 0
uv.lock

@@ -41,6 +41,11 @@ dependencies = [
     { name = "xarray" },
 ]
 
+[package.dev-dependencies]
+dev = [
+    { name = "pandas-stubs" },
+]
+
 [package.metadata]
 requires-dist = [
     { name = "bayesian-torch" },
@@ -62,6 +67,9 @@ requires-dist = [
     { name = "xarray" },
 ]
 
+[package.metadata.requires-dev]
+dev = [{ name = "pandas-stubs", specifier = ">=3.0.3.260530" }]
+
 [[package]]
 name = "anyio"
 version = "4.14.1"
@@ -1510,6 +1518,18 @@ wheels = [
 ]
 
 [[package]]
+name = "pandas-stubs"
+version = "3.0.3.260530"
+source = { registry = "https://pypi.org/simple" }
+dependencies = [
+    { name = "numpy" },
+]
+sdist = { url = "https://files.pythonhosted.org/packages/3d/aa/c41a8a0ff86fd85dbb3ec0c1f3fa488ca64a8b5f82654ae1b07d84acefe5/pandas_stubs-3.0.3.260530.tar.gz", hash = "sha256:d1efe47b2e5a312c047d7feabec5cb7a55365747983420077e9fcbe9ab74f714", size = 113183, upload-time = "2026-05-30T17:47:40.34Z" }
+wheels = [
+    { url = "https://files.pythonhosted.org/packages/0b/e0/99ec5b02203c4e9ce878bc63d8caa06ac1f891e4d63bded9a5ced70fcb4f/pandas_stubs-3.0.3.260530-py3-none-any.whl", hash = "sha256:a6277eb1c8cebf48d9b2413fcd2e9a6b4ff479c934a223c29eacbc3058c4cb55", size = 173780, upload-time = "2026-05-30T17:47:39.13Z" },
+]
+
+[[package]]
 name = "pandocfilters"
 version = "1.5.1"
 source = { registry = "https://pypi.org/simple" }