From 0bc2f1e9c17b355b20f764641c917b0591e018b0 Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Sat, 11 Jul 2020 19:37:47 +0200 Subject: [PATCH 1/3] TST: GH20676 Verify equals operator for list of Numpy arrays --- pandas/tests/series/methods/test_equals.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 pandas/tests/series/methods/test_equals.py diff --git a/pandas/tests/series/methods/test_equals.py b/pandas/tests/series/methods/test_equals.py new file mode 100644 index 0000000000000..cda888fd39306 --- /dev/null +++ b/pandas/tests/series/methods/test_equals.py @@ -0,0 +1,13 @@ +import numpy as np +import pytest + +from pandas import Series + + +@pytest.mark.parametrize( + "input_data", [[np.array([1, 2]), np.array([1, 2])]], +) +def test_equals_list_array(input_data): + # GH20676 Verify equals operator for list of Numpy arrays + ser = Series(input_data) + assert ser.equals(ser) From 5305f1d764eac667a46962a40e9ae4e57ce3b9ec Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Sun, 12 Jul 2020 13:02:15 +0200 Subject: [PATCH 2/3] CLN: inline test data --- pandas/tests/series/methods/test_equals.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/pandas/tests/series/methods/test_equals.py b/pandas/tests/series/methods/test_equals.py index cda888fd39306..d71ad753dfcbb 100644 --- a/pandas/tests/series/methods/test_equals.py +++ b/pandas/tests/series/methods/test_equals.py @@ -1,13 +1,10 @@ import numpy as np -import pytest from pandas import Series -@pytest.mark.parametrize( - "input_data", [[np.array([1, 2]), np.array([1, 2])]], -) -def test_equals_list_array(input_data): +def test_equals_list_array(): # GH20676 Verify equals operator for list of Numpy arrays - ser = Series(input_data) + arr = np.array([1, 2]) + ser = Series([arr, arr]) assert ser.equals(ser) From 33df25994db637bc9a6988b4493b7db2410310a2 Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Sun, 12 Jul 2020 21:23:27 +0200 Subject: [PATCH 3/3] Move existing Series.equals tests from generic/test_generic.py to series/methods/test_equals.py --- pandas/tests/generic/test_generic.py | 37 +--------------- pandas/tests/series/methods/test_equals.py | 51 ++++++++++++++++++++-- 2 files changed, 49 insertions(+), 39 deletions(-) diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py index 94747a52136c4..5e66925a38ec6 100644 --- a/pandas/tests/generic/test_generic.py +++ b/pandas/tests/generic/test_generic.py @@ -8,7 +8,7 @@ from pandas.core.dtypes.common import is_scalar import pandas as pd -from pandas import DataFrame, MultiIndex, Series, date_range +from pandas import DataFrame, Series, date_range import pandas._testing as tm import pandas.core.common as com @@ -785,26 +785,6 @@ def test_depr_take_kwarg_is_copy(self, is_copy): s.take([0, 1], is_copy=is_copy) def test_equals(self): - s1 = pd.Series([1, 2, 3], index=[0, 2, 1]) - s2 = s1.copy() - assert s1.equals(s2) - - s1[1] = 99 - assert not s1.equals(s2) - - # NaNs compare as equal - s1 = pd.Series([1, np.nan, 3, np.nan], index=[0, 2, 1, 3]) - s2 = s1.copy() - assert s1.equals(s2) - - s2[0] = 9.9 - assert not s1.equals(s2) - - idx = MultiIndex.from_tuples([(0, "a"), (1, "b"), (2, "c")]) - s1 = Series([1, 2, np.nan], index=idx) - s2 = s1.copy() - assert s1.equals(s2) - # Add object dtype column with nans index = np.random.random(10) df1 = DataFrame(np.random.random(10), index=index, columns=["floats"]) @@ -857,21 +837,6 @@ def test_equals(self): df2 = df1.set_index(["floats"], append=True) assert df3.equals(df2) - # GH 8437 - a = pd.Series([False, np.nan]) - b = pd.Series([False, np.nan]) - c = pd.Series(index=range(2), dtype=object) - d = c.copy() - e = c.copy() - f = c.copy() - c[:-1] = d[:-1] = e[0] = f[0] = False - assert a.equals(a) - assert a.equals(b) - assert a.equals(c) - assert a.equals(d) - assert a.equals(e) - assert e.equals(f) - def test_pipe(self): df = DataFrame({"A": [1, 2, 3]}) f = lambda x, y: x ** y diff --git a/pandas/tests/series/methods/test_equals.py b/pandas/tests/series/methods/test_equals.py index d71ad753dfcbb..600154adfcda3 100644 --- a/pandas/tests/series/methods/test_equals.py +++ b/pandas/tests/series/methods/test_equals.py @@ -1,10 +1,55 @@ import numpy as np +import pytest -from pandas import Series +from pandas import MultiIndex, Series + + +@pytest.mark.parametrize( + "arr, idx", + [ + ([1, 2, 3, 4], [0, 2, 1, 3]), + ([1, np.nan, 3, np.nan], [0, 2, 1, 3]), + ( + [1, np.nan, 3, np.nan], + MultiIndex.from_tuples([(0, "a"), (1, "b"), (2, "c"), (3, "c")]), + ), + ], +) +def test_equals(arr, idx): + s1 = Series(arr, index=idx) + s2 = s1.copy() + assert s1.equals(s2) + + s1[1] = 9 + assert not s1.equals(s2) def test_equals_list_array(): # GH20676 Verify equals operator for list of Numpy arrays arr = np.array([1, 2]) - ser = Series([arr, arr]) - assert ser.equals(ser) + s1 = Series([arr, arr]) + s2 = s1.copy() + assert s1.equals(s2) + + # TODO: Series equals should also work between single value and list + # s1[1] = 9 + # assert not s1.equals(s2) + + +def test_equals_false_negative(): + # GH8437 Verify false negative behavior of equals function for dtype object + arr = [False, np.nan] + s1 = Series(arr) + s2 = s1.copy() + s3 = Series(index=range(2), dtype=object) + s4 = s3.copy() + s5 = s3.copy() + s6 = s3.copy() + + s3[:-1] = s4[:-1] = s5[0] = s6[0] = False + assert s1.equals(s1) + assert s1.equals(s2) + assert s1.equals(s3) + assert s1.equals(s4) + assert s1.equals(s5) + assert s5.equals(s6)