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
12 changes: 5 additions & 7 deletions tests/dataframe/all_rowwise_test.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
from __future__ import annotations

import pandas as pd
import pytest

from tests.utils import bool_dataframe_1
from tests.utils import interchange_to_pandas
from tests.utils import compare_dataframe_with_reference


def test_all_horizontal(library: str) -> None:
df = bool_dataframe_1(library)
namespace = df.__dataframe_namespace__()
mask = namespace.all_horizontal(*[df.col(col_name) for col_name in df.column_names])
ns = df.__dataframe_namespace__()
mask = ns.all_horizontal(*[df.col(col_name) for col_name in df.column_names])
result = df.filter(mask)
result_pd = interchange_to_pandas(result)
expected = pd.DataFrame({"a": [True, True], "b": [True, True]})
pd.testing.assert_frame_equal(result_pd, expected)
expected = {"a": [True, True], "b": [True, True]}
compare_dataframe_with_reference(result, expected, dtype=ns.Bool)


def test_all_horizontal_invalid(library: str) -> None:
Expand Down
16 changes: 7 additions & 9 deletions tests/dataframe/and_test.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
from __future__ import annotations

import pandas as pd

from tests.utils import bool_dataframe_1
from tests.utils import interchange_to_pandas
from tests.utils import compare_dataframe_with_reference


def test_and_with_scalar(library: str) -> None:
df = bool_dataframe_1(library)
ns = df.__dataframe_namespace__()
other = True
result = df & other
result_pd = interchange_to_pandas(result)
expected = pd.DataFrame({"a": [True, True, False], "b": [True, True, True]})
pd.testing.assert_frame_equal(result_pd, expected)
expected = {"a": [True, True, False], "b": [True, True, True]}
compare_dataframe_with_reference(result, expected, ns.Bool)


def test_rand_with_scalar(library: str) -> None:
df = bool_dataframe_1(library)
ns = df.__dataframe_namespace__()
other = True
result = other & df
result_pd = interchange_to_pandas(result)
expected = pd.DataFrame({"a": [True, True, False], "b": [True, True, True]})
pd.testing.assert_frame_equal(result_pd, expected)
expected = {"a": [True, True, False], "b": [True, True, True]}
compare_dataframe_with_reference(result, expected, ns.Bool)
20 changes: 9 additions & 11 deletions tests/dataframe/any_all_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from __future__ import annotations

import pandas as pd
import pytest

from tests.utils import bool_dataframe_1
from tests.utils import bool_dataframe_3
from tests.utils import interchange_to_pandas
from tests.utils import compare_dataframe_with_reference


@pytest.mark.parametrize(
Expand All @@ -21,23 +20,22 @@ def test_reductions(
expected_data: dict[str, object],
) -> None:
df = bool_dataframe_1(library)
ns = df.__dataframe_namespace__()
result = getattr(df, reduction)()
result_pd = interchange_to_pandas(result)
expected = pd.DataFrame(expected_data)
pd.testing.assert_frame_equal(result_pd, expected)
compare_dataframe_with_reference(result, expected_data, dtype=ns.Bool) # type: ignore[arg-type]


def test_any(library: str) -> None:
df = bool_dataframe_3(library)
ns = df.__dataframe_namespace__()
result = df.any()
result_pd = interchange_to_pandas(result)
expected = pd.DataFrame({"a": [False], "b": [True], "c": [True]})
pd.testing.assert_frame_equal(result_pd, expected)
expected = {"a": [False], "b": [True], "c": [True]}
compare_dataframe_with_reference(result, expected, dtype=ns.Bool)


def test_all(library: str) -> None:
df = bool_dataframe_3(library)
ns = df.__dataframe_namespace__()
result = df.all()
result_pd = interchange_to_pandas(result)
expected = pd.DataFrame({"a": [False], "b": [False], "c": [True]})
pd.testing.assert_frame_equal(result_pd, expected)
expected = {"a": [False], "b": [False], "c": [True]}
compare_dataframe_with_reference(result, expected, dtype=ns.Bool)
12 changes: 5 additions & 7 deletions tests/dataframe/any_rowwise_test.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
from __future__ import annotations

import pandas as pd
import pytest

from tests.utils import bool_dataframe_1
from tests.utils import interchange_to_pandas
from tests.utils import compare_dataframe_with_reference


def test_any_horizontal(library: str) -> None:
df = bool_dataframe_1(library)
namespace = df.__dataframe_namespace__()
mask = namespace.any_horizontal(*[df.col(col_name) for col_name in df.column_names])
ns = df.__dataframe_namespace__()
mask = ns.any_horizontal(*[df.col(col_name) for col_name in df.column_names])
result = df.filter(mask)
result_pd = interchange_to_pandas(result)
expected = pd.DataFrame({"a": [True, True, False], "b": [True, True, True]})
pd.testing.assert_frame_equal(result_pd, expected)
expected = {"a": [True, True, False], "b": [True, True, True]}
compare_dataframe_with_reference(result, expected, dtype=ns.Bool)


def test_any_horizontal_invalid(library: str) -> None:
Expand Down
42 changes: 16 additions & 26 deletions tests/dataframe/assign_test.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
from __future__ import annotations

import pandas as pd
import pytest

from tests.utils import compare_dataframe_with_reference
from tests.utils import integer_dataframe_1
from tests.utils import interchange_to_pandas


def test_insert_columns(library: str) -> None:
df = integer_dataframe_1(library, api_version="2023.09-beta")
df.__dataframe_namespace__()
ns = df.__dataframe_namespace__()
new_col = (df.col("b") + 3).rename("result")
result = df.assign(new_col.rename("c"))
result_pd = interchange_to_pandas(result)
expected = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
pd.testing.assert_frame_equal(result_pd, expected)
expected = {"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}
compare_dataframe_with_reference(result, expected, dtype=ns.Int64)
# check original df didn't change
df_pd = interchange_to_pandas(df)
expected = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
pd.testing.assert_frame_equal(df_pd, expected)
expected = {"a": [1, 2, 3], "b": [4, 5, 6]}
compare_dataframe_with_reference(df, expected, dtype=ns.Int64)


def test_insert_multiple_columns(library: str) -> None:
df = integer_dataframe_1(library, api_version="2023.09-beta")
df.__dataframe_namespace__()
ns = df.__dataframe_namespace__()
new_col = (df.col("b") + 3).rename("result")
result = df.assign(new_col.rename("c"), new_col.rename("d"))
result_pd = interchange_to_pandas(result)
expected = pd.DataFrame(
{"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9], "d": [7, 8, 9]},
)
pd.testing.assert_frame_equal(result_pd, expected)
expected = {"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9], "d": [7, 8, 9]}
compare_dataframe_with_reference(result, expected, dtype=ns.Int64)
# check original df didn't change
df_pd = interchange_to_pandas(df)
expected = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
pd.testing.assert_frame_equal(df_pd, expected)
expected = {"a": [1, 2, 3], "b": [4, 5, 6]}
compare_dataframe_with_reference(df, expected, dtype=ns.Int64)


def test_insert_multiple_columns_invalid(library: str) -> None:
Expand All @@ -47,14 +40,11 @@ def test_insert_multiple_columns_invalid(library: str) -> None:

def test_insert_eager_columns(library: str) -> None:
df = integer_dataframe_1(library, api_version="2023.09-beta")
ns = df.__dataframe_namespace__()
new_col = (df.col("b") + 3).rename("result")
result = df.assign(new_col.rename("c"), new_col.rename("d"))
result_pd = interchange_to_pandas(result)
expected = pd.DataFrame(
{"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9], "d": [7, 8, 9]},
)
pd.testing.assert_frame_equal(result_pd, expected)
expected = {"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9], "d": [7, 8, 9]}
compare_dataframe_with_reference(result, expected, dtype=ns.Int64)
# check original df didn't change
df_pd = interchange_to_pandas(df)
expected = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
pd.testing.assert_frame_equal(df_pd, expected)
expected = {"a": [1, 2, 3], "b": [4, 5, 6]}
compare_dataframe_with_reference(df, expected, dtype=ns.Int64)
16 changes: 6 additions & 10 deletions tests/dataframe/cast_test.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import pandas as pd

from tests.utils import compare_dataframe_with_reference
from tests.utils import integer_dataframe_1
from tests.utils import interchange_to_pandas


def test_cast_integers(library: str) -> None:
df = integer_dataframe_1(library)
pdx = df.__dataframe_namespace__()
result = df.cast({"a": pdx.Int32()})
expected = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}).astype(
{"a": "int32", "b": "int64"},
)
result_pd = interchange_to_pandas(result)
pd.testing.assert_frame_equal(result_pd, expected)
ns = df.__dataframe_namespace__()
result = df.cast({"a": ns.Int32()})
expected = {"a": [1, 2, 3], "b": [4, 5, 6]}
expected_dtype = {"a": ns.Int32, "b": ns.Int64}
compare_dataframe_with_reference(result, expected, dtype=expected_dtype)
18 changes: 7 additions & 11 deletions tests/dataframe/columns_iter_test.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import pandas as pd

from tests.utils import compare_dataframe_with_reference
from tests.utils import integer_dataframe_1
from tests.utils import interchange_to_pandas


def test_iter_columns(library: str) -> None:
df = integer_dataframe_1(library)
ns = df.__dataframe_namespace__()
result = df.assign(
*[col / col.mean() for col in df.iter_columns()],
)
expected = pd.DataFrame(
{
"a": [0.5, 1.0, 1.5],
"b": [0.8, 1.0, 1.2],
},
)
result_pd = interchange_to_pandas(result)
pd.testing.assert_frame_equal(result_pd, expected)
expected = {
"a": [0.5, 1.0, 1.5],
"b": [0.8, 1.0, 1.2],
}
compare_dataframe_with_reference(result, expected, dtype=ns.Float64)
43 changes: 21 additions & 22 deletions tests/dataframe/comparisons_test.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
from __future__ import annotations

import pandas as pd
import pytest

from tests.utils import compare_dataframe_with_reference
from tests.utils import integer_dataframe_1
from tests.utils import interchange_to_pandas


@pytest.mark.parametrize(
("comparison", "expected_data"),
("comparison", "expected_data", "expected_dtype"),
[
("__eq__", {"a": [False, True, False], "b": [False, False, False]}),
("__ne__", {"a": [True, False, True], "b": [True, True, True]}),
("__ge__", {"a": [False, True, True], "b": [True, True, True]}),
("__gt__", {"a": [False, False, True], "b": [True, True, True]}),
("__le__", {"a": [True, True, False], "b": [False, False, False]}),
("__lt__", {"a": [True, False, False], "b": [False, False, False]}),
("__add__", {"a": [3, 4, 5], "b": [6, 7, 8]}),
("__sub__", {"a": [-1, 0, 1], "b": [2, 3, 4]}),
("__mul__", {"a": [2, 4, 6], "b": [8, 10, 12]}),
("__truediv__", {"a": [0.5, 1, 1.5], "b": [2, 2.5, 3]}),
("__floordiv__", {"a": [0, 1, 1], "b": [2, 2, 3]}),
("__pow__", {"a": [1, 4, 9], "b": [16, 25, 36]}),
("__mod__", {"a": [1, 0, 1], "b": [0, 1, 0]}),
("__eq__", {"a": [False, True, False], "b": [False, False, False]}, "Bool"),
("__ne__", {"a": [True, False, True], "b": [True, True, True]}, "Bool"),
("__ge__", {"a": [False, True, True], "b": [True, True, True]}, "Bool"),
("__gt__", {"a": [False, False, True], "b": [True, True, True]}, "Bool"),
("__le__", {"a": [True, True, False], "b": [False, False, False]}, "Bool"),
("__lt__", {"a": [True, False, False], "b": [False, False, False]}, "Bool"),
("__add__", {"a": [3, 4, 5], "b": [6, 7, 8]}, "Int64"),
("__sub__", {"a": [-1, 0, 1], "b": [2, 3, 4]}, "Int64"),
("__mul__", {"a": [2, 4, 6], "b": [8, 10, 12]}, "Int64"),
("__truediv__", {"a": [0.5, 1, 1.5], "b": [2, 2.5, 3]}, "Float64"),
("__floordiv__", {"a": [0, 1, 1], "b": [2, 2, 3]}, "Int64"),
("__pow__", {"a": [1, 4, 9], "b": [16, 25, 36]}, "Int64"),
("__mod__", {"a": [1, 0, 1], "b": [0, 1, 0]}, "Int64"),
],
)
def test_comparisons_with_scalar(
library: str,
comparison: str,
expected_data: dict[str, object],
expected_dtype: str,
) -> None:
df = integer_dataframe_1(library)
ns = df.__dataframe_namespace__()
other = 2
result = getattr(df, comparison)(other)
result_pd = interchange_to_pandas(result)
expected = pd.DataFrame(expected_data)
pd.testing.assert_frame_equal(result_pd, expected)
expected_ns_dtype = getattr(ns, expected_dtype)
compare_dataframe_with_reference(result, expected_data, dtype=expected_ns_dtype) # type: ignore[arg-type]


@pytest.mark.parametrize(
Expand All @@ -52,8 +52,7 @@ def test_rcomparisons_with_scalar(
expected_data: dict[str, object],
) -> None:
df = integer_dataframe_1(library)
ns = df.__dataframe_namespace__()
other = 2
result = getattr(df, comparison)(other)
result_pd = interchange_to_pandas(result)
expected = pd.DataFrame(expected_data)
pd.testing.assert_frame_equal(result_pd, expected)
compare_dataframe_with_reference(result, expected_data, dtype=ns.Int64) # type: ignore[arg-type]
15 changes: 6 additions & 9 deletions tests/dataframe/divmod_test.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
from __future__ import annotations

import pandas as pd

from tests.utils import compare_dataframe_with_reference
from tests.utils import integer_dataframe_1
from tests.utils import interchange_to_pandas


def test_divmod_with_scalar(library: str) -> None:
df = integer_dataframe_1(library)
ns = df.__dataframe_namespace__()
other = 2
result_quotient, result_remainder = df.__divmod__(other)
result_quotient_pd = interchange_to_pandas(result_quotient)
result_remainder_pd = interchange_to_pandas(result_remainder)
expected_quotient = pd.DataFrame({"a": [0, 1, 1], "b": [2, 2, 3]})
expected_remainder = pd.DataFrame({"a": [1, 0, 1], "b": [0, 1, 0]})
pd.testing.assert_frame_equal(result_quotient_pd, expected_quotient)
pd.testing.assert_frame_equal(result_remainder_pd, expected_remainder)
expected_quotient = {"a": [0, 1, 1], "b": [2, 2, 3]}
expected_remainder = {"a": [1, 0, 1], "b": [0, 1, 0]}
compare_dataframe_with_reference(result_quotient, expected_quotient, dtype=ns.Int64)
compare_dataframe_with_reference(result_remainder, expected_remainder, dtype=ns.Int64)
10 changes: 4 additions & 6 deletions tests/dataframe/drop_column_test.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from __future__ import annotations

import pandas as pd

from tests.utils import compare_dataframe_with_reference
from tests.utils import integer_dataframe_1
from tests.utils import interchange_to_pandas


def test_drop_column(library: str) -> None:
df = integer_dataframe_1(library)
ns = df.__dataframe_namespace__()
result = df.drop("a")
result_pd = interchange_to_pandas(result)
expected = pd.DataFrame({"b": [4, 5, 6]})
pd.testing.assert_frame_equal(result_pd, expected)
expected = {"b": [4, 5, 6]}
compare_dataframe_with_reference(result, expected, dtype=ns.Int64)
10 changes: 4 additions & 6 deletions tests/dataframe/drop_nulls_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import pandas as pd

from tests.utils import interchange_to_pandas
from tests.utils import compare_dataframe_with_reference
from tests.utils import null_dataframe_1


def test_drop_nulls(library: str) -> None:
df = null_dataframe_1(library)
ns = df.__dataframe_namespace__()
result = df.drop_nulls()
expected = pd.DataFrame({"a": [1.0, 2.0]})
result_pd = interchange_to_pandas(result)
pd.testing.assert_frame_equal(result_pd, expected)
expected = {"a": [1.0, 2.0]}
compare_dataframe_with_reference(result, expected, dtype=ns.Float64)
Loading