Skip to content
Merged
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
30 changes: 30 additions & 0 deletions .github/workflows/pandas-compatibility.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: pandas compatibility

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

jobs:
pandas:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
# Exercise the oldest pandas 3 interpreter and the newest supported one.
python-version: ["3.11", "3.14"]
pandas-spec: ["pandas>=2,<3", "pandas>=3,<4"]

steps:
- uses: actions/checkout@v6
- uses: astral-sh/setup-uv@v7
with:
python-version: ${{ matrix.python-version }}
- name: Install the compatibility environment
run: |
uv venv
# Test the DuckDB minimum and both Python/Arrow string backends.
uv pip install -e '.[test]' '${{ matrix.pandas-spec }}' 'duckdb==1.4.4' pyarrow
- name: Run tests without restoring the locked pandas version
run: uv run --no-sync pytest -q
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ authors = [
{ name = "Alan F. Munoz", email = "amunozgo@broadinstitute.org" }
]
dependencies = [
"pandas<3",
"pandas",
"tqdm",
"statsmodels",
"duckdb>=1.4.0",
"duckdb>=1.4.4",
]

[project.optional-dependencies]
Expand Down
10 changes: 10 additions & 0 deletions src/copairs/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,16 @@ def find_pairs_multilabel(
)

df = dframe.reset_index()
# pandas groupby().unique() can produce ExtensionArray cells, which DuckDB
# otherwise infers as VARCHAR instead of LIST. Keep NumPy arrays and missing
# cells intact; list(ndarray) would leave unsupported NumPy scalar elements.
df[multilabel_col] = df[multilabel_col].map(
lambda labels: (
labels.tolist()
if isinstance(labels, pd.api.extensions.ExtensionArray)
else labels
)
)

if multilabel_col in sameby:
sameby = copy(sameby)
Expand Down
86 changes: 86 additions & 0 deletions tests/test_pandas_compatibility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"""Regression tests for pandas string and multilabel representations."""

import numpy as np
import pandas as pd
import pytest

from copairs.matching import find_pairs, find_pairs_multilabel


def _check_multilabel_pairs(dframe, shared_label):
"""Check pairs, label counts, and input preservation for three profiles."""
original = dframe.copy(deep=True)
pairs, keys, counts = find_pairs_multilabel(dframe, ["labels"], [], "labels")
np.testing.assert_array_equal(pairs, [[0, 1]])
np.testing.assert_array_equal(keys, [shared_label])
np.testing.assert_array_equal(counts, [1])

pairs = find_pairs_multilabel(dframe, [], ["labels"], "labels")
assert set(map(tuple, pairs)) == {(0, 2), (1, 2)}
pd.testing.assert_frame_equal(dframe, original)


@pytest.mark.parametrize("storage", ["python", "pyarrow"])
def test_inferred_string_columns(storage):
"""DuckDB must accept pandas 3's inferred str dtype with either backend."""
if storage == "pyarrow":
pytest.importorskip("pyarrow")
with pd.option_context("mode.string_storage", storage):
dframe = pd.DataFrame({"label": ["a", "a", "b"]})
np.testing.assert_array_equal(find_pairs(dframe, ["label"], []), [[0, 1]])
pairs = find_pairs(dframe, [], ["label"])
assert set(map(tuple, pairs)) == {(0, 2), (1, 2)}


@pytest.mark.parametrize("dtype", [None, "object", "string[python]", "string[pyarrow]"])
def test_groupby_unique_string_labels(dtype):
"""Normalize StringArray cells produced by groupby.unique, not just ndarrays."""
if dtype == "string[pyarrow]":
pytest.importorskip("pyarrow")
dframe = pd.DataFrame(
{
"sample": [0, 0, 1, 2],
"labels": pd.Series(["a", "b", "a", "c"], dtype=dtype),
}
)
dframe = dframe.groupby("sample")["labels"].unique().reset_index(drop=True)
_check_multilabel_pairs(dframe.to_frame(), "a")


@pytest.mark.parametrize("dtype", ["int32", "int64", "float32", "bool"])
def test_numpy_label_arrays(dtype):
"""Do not turn numeric arrays into lists of unsupported NumPy scalars."""
dframe = pd.DataFrame(
{"labels": [np.array([value], dtype=dtype) for value in [1, 1, 0]]}
)
_check_multilabel_pairs(dframe, 1)


@pytest.mark.parametrize("dtype", ["Int64", "Float32", "boolean"])
def test_nullable_label_arrays(dtype):
"""Convert extension arrays to native lists while preserving inner nulls."""
dframe = pd.DataFrame(
{
"labels": [
pd.array([1, pd.NA], dtype=dtype),
pd.array([1], dtype=dtype),
pd.array([0], dtype=dtype),
]
}
)
_check_multilabel_pairs(dframe, 1)


@pytest.mark.parametrize(
"missing",
[
pytest.param(None, id="none"),
pytest.param(np.nan, id="nan"),
pytest.param(pd.NA, id="pd-na"),
pytest.param([], id="empty-list"),
],
)
def test_missing_or_empty_label_cells(missing):
"""Preserve existing SQL behavior for missing and empty multilabel cells."""
dframe = pd.DataFrame({"labels": [["a"], ["a"], missing]})
_check_multilabel_pairs(dframe, "a")
Loading
Loading