Skip to content

Commit e8cf9e2

Browse files
author
raisa
committed
add more tests for narwhals.dataframe
1 parent 6b74350 commit e8cf9e2

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Diff for: tests/test_common.py

+46
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from typing import Any
44

5+
import numpy as np
56
import pandas as pd
67
import polars as pl
78
import pytest
@@ -119,3 +120,48 @@ def test_double_selected(df_raw: Any) -> None:
119120
result_native = nw.to_native(result)
120121
expected = {"a": [2, 6, 4], "b": [8, 8, 12]}
121122
compare_dicts(result_native, expected)
123+
124+
125+
@pytest.mark.parametrize("df_raw", [df_pandas, df_polars, df_lazy])
126+
def test_rename(df_raw: Any) -> None:
127+
df = nw.DataFrame(df_raw)
128+
result = df.rename({"a": "x", "b": "y"})
129+
result_native = nw.to_native(result)
130+
expected = {"x": [1, 3, 2], "y": [4, 4, 6], "z": [7.0, 8, 9]}
131+
compare_dicts(result_native, expected)
132+
133+
134+
@pytest.mark.parametrize("df_raw", [df_pandas, df_polars, df_lazy])
135+
def test_join(df_raw: Any) -> None:
136+
df = nw.DataFrame(df_raw)
137+
df_right = df.rename({"z": "z_right"})
138+
result = df.join(df_right, left_on=["a", "b"], right_on=["a", "b"], how="inner")
139+
result_native = nw.to_native(result)
140+
expected = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9], "z_right": [7.0, 8, 9]}
141+
compare_dicts(result_native, expected)
142+
143+
144+
@pytest.mark.parametrize("df_raw", [df_pandas, df_polars, df_lazy])
145+
def test_schema(df_raw: Any) -> None:
146+
df = nw.DataFrame(df_raw)
147+
result = df.schema
148+
expected = {"a": nw.dtypes.Int64, "b": nw.dtypes.Int64, "z": nw.dtypes.Float64}
149+
assert result == expected
150+
151+
152+
@pytest.mark.parametrize("df_raw", [df_pandas, df_polars, df_lazy])
153+
def test_columns(df_raw: Any) -> None:
154+
df = nw.DataFrame(df_raw)
155+
result = df.columns
156+
expected = ["a", "b", "z"]
157+
assert len(result) == len(expected)
158+
assert all(x == y for x, y in zip(result, expected))
159+
160+
161+
def test_accepted_dataframes() -> None:
162+
array = np.array([[0, 4.0], [2, 5]])
163+
with pytest.raises(
164+
TypeError,
165+
match="Expected pandas or Polars dataframe or lazyframe, got: <class 'numpy.ndarray'>",
166+
):
167+
nw.DataFrame(array)

0 commit comments

Comments
 (0)