Skip to content

Commit

Permalink
Merge branch 'main' of github.com:MarcoGorelli/narwhals
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Mar 17, 2024
2 parents 269c7b4 + d53de4f commit 817e70d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
8 changes: 6 additions & 2 deletions narwhals/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ def __init__(
elif (mpd := get_modin()) is not None and isinstance(df, mpd.DataFrame):
self._dataframe = PandasDataFrame(df, implementation="modin")
self._implementation = "modin"
elif (cudf := get_cudf()) is not None and isinstance(df, cudf.DataFrame):
elif (cudf := get_cudf()) is not None and isinstance(
df, cudf.DataFrame
): # pragma: no cover
self._dataframe = PandasDataFrame(df, implementation="cudf")
self._implementation = "cudf"
else:
Expand Down Expand Up @@ -220,7 +222,9 @@ def __init__(
elif (mpd := get_modin()) is not None and isinstance(df, mpd.DataFrame):
self._dataframe = PandasDataFrame(df, implementation="modin")
self._implementation = "modin"
elif (cudf := get_cudf()) is not None and isinstance(df, cudf.DataFrame):
elif (cudf := get_cudf()) is not None and isinstance(
df, cudf.DataFrame
): # pragma: no cover
self._dataframe = PandasDataFrame(df, implementation="cudf")
self._implementation = "cudf"
else:
Expand Down
6 changes: 5 additions & 1 deletion narwhals/pandas_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,8 @@ def to_numpy(self) -> Any:
return self._dataframe.to_numpy()

def to_pandas(self) -> Any:
return self._dataframe
if self._implementation == "pandas":
return self._dataframe
if self._implementation == "modin":
return self._dataframe._to_pandas()
return self._dataframe.to_pandas()
37 changes: 33 additions & 4 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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 @@ -16,7 +17,7 @@
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 = pd.DataFrame(
df_mpd = mpd.DataFrame(
pd.DataFrame({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]})
)

Expand Down Expand Up @@ -184,12 +185,19 @@ def test_columns(df_raw: Any) -> None:
df = nw.LazyFrame(df_raw)
result = df.columns
expected = ["a", "b", "z"]
assert len(result) == len(expected)
assert all(x == y for x, y in zip(result, expected))
assert result == expected


@pytest.mark.parametrize("df_raw", [df_lazy])
@pytest.mark.parametrize("df_raw", [df_polars, df_pandas, df_mpd, df_lazy])
def test_lazy_instantiation(df_raw: Any) -> None:
result = nw.LazyFrame(df_raw)
result_native = nw.to_native(result)
expected = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}
compare_dicts(result_native, expected)


@pytest.mark.parametrize("df_raw", [df_lazy])
def test_lazy_instantiation_error(df_raw: Any) -> None:
with pytest.raises(
TypeError, match="Can't instantiate DataFrame from Polars LazyFrame."
):
Expand All @@ -216,3 +224,24 @@ def test_accepted_dataframes() -> None:
match="Expected pandas-like dataframe, Polars dataframe, or Polars lazyframe, got: <class 'numpy.ndarray'>",
):
nw.LazyFrame(array)


@pytest.mark.parametrize("df_raw", [df_polars, df_pandas, df_mpd])
def test_convert_pandas(df_raw: Any) -> None:
result = nw.DataFrame(df_raw).to_pandas()
expected = pd.DataFrame({"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]})
pd.testing.assert_frame_equal(result, expected)


@pytest.mark.parametrize("df_raw", [df_polars, df_pandas, df_mpd])
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
np.testing.assert_array_equal(result, expected)


@pytest.mark.parametrize("df_raw", [df_polars, df_pandas, df_mpd])
def test_shape(df_raw: Any) -> None:
result = nw.DataFrame(df_raw).shape
expected = (3, 3)
assert result == expected

0 comments on commit 817e70d

Please sign in to comment.