Skip to content

Commit

Permalink
Merge pull request #17 from MarcoGorelli/old-pandas-compat
Browse files Browse the repository at this point in the history
Old pandas compat
  • Loading branch information
MarcoGorelli authored Mar 18, 2024
2 parents f1e2c28 + 2a4a019 commit 3065018
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
tox:
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
os: [windows-latest, ubuntu-latest]

runs-on: ${{ matrix.os }}
Expand All @@ -28,5 +28,7 @@ jobs:
key: ${{ runner.os }}-build-${{ matrix.python-version }}
- name: install-reqs
run: python -m pip install --upgrade tox virtualenv setuptools pip -r requirements-dev.txt
- name: install-modin
run: python -m pip install --upgrade modin[dask]
- name: Run pytest
run: pytest tests --cov=narwhals --cov=tests --cov-fail-under=50
10 changes: 7 additions & 3 deletions narwhals/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,17 @@ def __init__(
elif (pd := get_pandas()) is not None and isinstance(df, pd.DataFrame):
self._dataframe = PandasDataFrame(df, implementation="pandas")
self._implementation = "pandas"
elif (mpd := get_modin()) is not None and isinstance(df, mpd.DataFrame):
elif (mpd := get_modin()) is not None and isinstance(
df, mpd.DataFrame
): # pragma: no cover
self._dataframe = PandasDataFrame(df, implementation="modin")
self._implementation = "modin"
elif (cudf := get_cudf()) is not None and isinstance(
df, cudf.DataFrame
): # pragma: no cover
self._dataframe = PandasDataFrame(df, implementation="cudf")
self._implementation = "cudf"
elif hasattr(df, "__narwhals_dataframe__"):
elif hasattr(df, "__narwhals_dataframe__"): # pragma: no cover
self._dataframe = df.__narwhals_dataframe__()
self._implementation = "custom"
else:
Expand Down Expand Up @@ -222,7 +224,9 @@ def __init__(
elif (pd := get_pandas()) is not None and isinstance(df, pd.DataFrame):
self._dataframe = PandasDataFrame(df, implementation="pandas")
self._implementation = "pandas"
elif (mpd := get_modin()) is not None and isinstance(df, mpd.DataFrame):
elif (mpd := get_modin()) is not None and isinstance(
df, mpd.DataFrame
): # pragma: no cover
self._dataframe = PandasDataFrame(df, implementation="modin")
self._implementation = "modin"
elif (cudf := get_cudf()) is not None and isinstance(
Expand Down
7 changes: 6 additions & 1 deletion narwhals/pandas_like/group_by.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from typing import Callable
from typing import Iterable

from packaging.version import parse

from narwhals.pandas_like.utils import dataframe_from_dict
from narwhals.pandas_like.utils import evaluate_simple_aggregation
from narwhals.pandas_like.utils import horizontal_concat
Expand Down Expand Up @@ -122,7 +124,10 @@ def func(df: Any) -> Any:
out_names.append(result_keys.name)
return pd.Series(out_group, index=out_names)

result_complex = grouped.apply(func, include_groups=False)
if parse(pd.__version__) < parse("2.2.0"):
result_complex = grouped.apply(func)
else:
result_complex = grouped.apply(func, include_groups=False)

if result_simple is not None:
result = pd.concat(
Expand Down
1 change: 0 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
covdefaults
modin[dask]
pandas
polars
pre-commit
Expand Down
21 changes: 15 additions & 6 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import annotations

import os
import warnings
from typing import Any

import modin.pandas as mpd
import numpy as np
import pandas as pd
import polars as pl
Expand All @@ -15,11 +15,17 @@
df_pandas = pd.DataFrame({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]})
df_polars = pl.DataFrame({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]})
df_lazy = pl.LazyFrame({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]})
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=UserWarning)
df_mpd = mpd.DataFrame(
mpd.DataFrame({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]})
)

if os.environ.get("CI", None):
import modin.pandas as mpd

with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=UserWarning)
df_mpd = mpd.DataFrame(
pd.DataFrame({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]})
)
else:
df_mpd = df_pandas.copy()


@pytest.mark.parametrize(
Expand Down Expand Up @@ -234,6 +240,9 @@ def test_convert_pandas(df_raw: Any) -> None:


@pytest.mark.parametrize("df_raw", [df_polars, df_pandas, df_mpd])
@pytest.mark.filterwarnings(
r"ignore:np\.find_common_type is deprecated\.:DeprecationWarning"
)
def test_convert_numpy(df_raw: Any) -> None:
result = nw.DataFrame(df_raw).to_numpy()
expected = np.array([[1, 3, 2], [4, 4, 6], [7.0, 8, 9]]).T
Expand Down

0 comments on commit 3065018

Please sign in to comment.