|
| 1 | +# |
| 2 | +# Copyright (c) 2012-2025 Snowflake Computing Inc. All rights reserved. |
| 3 | +# |
| 4 | + |
| 5 | +import modin.pandas as pd |
| 6 | +import numpy as np |
| 7 | +import pandas as native_pd |
| 8 | +import pytest |
| 9 | + |
| 10 | +import snowflake.snowpark.modin.plugin # noqa: F401 |
| 11 | +from tests.integ.modin.utils import assert_index_equal |
| 12 | +from tests.integ.utils.sql_counter import sql_count_checker |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize("keep", ["first", "last", False]) |
| 16 | +@sql_count_checker(query_count=1, join_count=2) |
| 17 | +def test_drop_duplicates(keep): |
| 18 | + pandas_idx = native_pd.Index(["a", "b", "b", "c", "a"], name="name") |
| 19 | + snow_idx = pd.Index(pandas_idx) |
| 20 | + |
| 21 | + assert_index_equal( |
| 22 | + snow_idx.drop_duplicates(keep=keep), |
| 23 | + pandas_idx.drop_duplicates(keep=keep), |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize("keep", ["first", "last", False]) |
| 28 | +@sql_count_checker(query_count=1, join_count=2) |
| 29 | +def test_drop_duplicates_on_empty_index(keep): |
| 30 | + pandas_idx = native_pd.Index([], name="name") |
| 31 | + snow_idx = pd.Index(pandas_idx) |
| 32 | + |
| 33 | + assert_index_equal( |
| 34 | + snow_idx.drop_duplicates(keep=keep), |
| 35 | + pandas_idx.drop_duplicates(keep=keep), |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize( |
| 40 | + "keep, expected", |
| 41 | + [ |
| 42 | + ("first", native_pd.Index([np.nan, 3])), |
| 43 | + ("last", native_pd.Index([3, np.nan])), |
| 44 | + (False, native_pd.Index([], dtype="float64")), |
| 45 | + ], |
| 46 | +) |
| 47 | +@sql_count_checker(query_count=1, join_count=2) |
| 48 | +def test_drop_duplicates_nan_none(keep, expected): |
| 49 | + # Note that Snowpark pandas treats np.nan and None the same |
| 50 | + idx = pd.Index([np.nan, 3, 3, None, np.nan], dtype=object) |
| 51 | + |
| 52 | + result = idx.drop_duplicates(keep=keep) |
| 53 | + assert_index_equal( |
| 54 | + result, |
| 55 | + expected, |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +@sql_count_checker(query_count=1, join_count=2) |
| 60 | +def test_drop_duplicates_default_keep(): |
| 61 | + pandas_idx = native_pd.Index([], name="name") |
| 62 | + snow_idx = pd.Index(pandas_idx) |
| 63 | + |
| 64 | + assert_index_equal( |
| 65 | + snow_idx.drop_duplicates(), |
| 66 | + pandas_idx.drop_duplicates(), |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +@sql_count_checker(query_count=0, join_count=0) |
| 71 | +def test_drop_duplicates_invalid_keep(): |
| 72 | + snow_idx = pd.Index(["a", "b", "b", "c", "a"], name="name") |
| 73 | + with pytest.raises( |
| 74 | + ValueError, match='keep must be either "first", "last" or False' |
| 75 | + ): |
| 76 | + snow_idx.drop_duplicates(keep="invalid") |
0 commit comments