From d847b82d94dae601dc6c301d2a4551b7c27926b9 Mon Sep 17 00:00:00 2001 From: Martin Fleischmann Date: Thu, 12 Mar 2020 22:46:22 +0000 Subject: [PATCH 1/6] reintroduce check_series_type --- pandas/_testing.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index dff15c66750ac..4cb81fb7dd4bf 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -1050,6 +1050,7 @@ def assert_series_equal( right, check_dtype=True, check_index_type="equiv", + check_series_type=True, check_less_precise=False, check_names=True, check_exact=False, @@ -1070,6 +1071,8 @@ def assert_series_equal( check_index_type : bool or {'equiv'}, default 'equiv' Whether to check the Index class, dtype and inferred_type are identical. + check_series_type : bool, default True + Whether to check the Series class is identical. check_less_precise : bool or int, default False Specify comparison precision. Only used when check_exact is False. 5 digits (False) or 3 digits (True) after decimal points are compared. @@ -1101,10 +1104,11 @@ def assert_series_equal( # instance validation _check_isinstance(left, right, Series) - # TODO: There are some tests using rhs is sparse - # lhs is dense. Should use assert_class_equal in future - assert isinstance(left, type(right)) - # assert_class_equal(left, right, obj=obj) + if check_series_type: + # TODO: There are some tests using rhs is sparse + # lhs is dense. Should use assert_class_equal in future + assert isinstance(left, type(right)) + # assert_class_equal(left, right, obj=obj) # length comparison if len(left) != len(right): From e44f44b3ac9552478d31e9b4084e880ee781cbfe Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 13 Mar 2020 11:59:41 +0100 Subject: [PATCH 2/6] add msg and test --- pandas/_testing.py | 4 +++- pandas/tests/util/test_assert_series_equal.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index 4cb81fb7dd4bf..7ddc4884e7a7a 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -1107,7 +1107,9 @@ def assert_series_equal( if check_series_type: # TODO: There are some tests using rhs is sparse # lhs is dense. Should use assert_class_equal in future - assert isinstance(left, type(right)) + assert isinstance( + left, type(right) + ), f"Series type not equal: {type(left)} vs {type(right)}" # assert_class_equal(left, right, obj=obj) # length comparison diff --git a/pandas/tests/util/test_assert_series_equal.py b/pandas/tests/util/test_assert_series_equal.py index eaf0824f52927..3ac2306e89c47 100644 --- a/pandas/tests/util/test_assert_series_equal.py +++ b/pandas/tests/util/test_assert_series_equal.py @@ -194,3 +194,20 @@ def test_series_equal_categorical_mismatch(check_categorical): tm.assert_series_equal(s1, s2, check_categorical=check_categorical) else: _assert_series_equal_both(s1, s2, check_categorical=check_categorical) + + +def test_series_equal_series_type(): + class MySeries(Series): + pass + + s1 = Series([1, 2]) + s2 = Series([1, 2]) + s3 = MySeries([1, 2]) + + tm.assert_series_equal(s1, s2, check_series_type=False) + tm.assert_series_equal(s1, s2, check_series_type=True) + + tm.assert_series_equal(s1, s3, check_series_type=False) + + with pytest.raises(AssertionError, match="Series type not equal"): + tm.assert_series_equal(s1, s3, check_series_type=True) From 89250c1ffcdadda6122322e9acaefb3c0563539f Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 13 Mar 2020 13:22:07 +0100 Subject: [PATCH 3/6] fix inverse order --- pandas/_testing.py | 4 ++-- pandas/tests/util/test_assert_series_equal.py | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index 7ddc4884e7a7a..c3af0594c6965 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -1107,8 +1107,8 @@ def assert_series_equal( if check_series_type: # TODO: There are some tests using rhs is sparse # lhs is dense. Should use assert_class_equal in future - assert isinstance( - left, type(right) + assert type(left) is type( + right ), f"Series type not equal: {type(left)} vs {type(right)}" # assert_class_equal(left, right, obj=obj) diff --git a/pandas/tests/util/test_assert_series_equal.py b/pandas/tests/util/test_assert_series_equal.py index 3ac2306e89c47..f765bb4e06cf3 100644 --- a/pandas/tests/util/test_assert_series_equal.py +++ b/pandas/tests/util/test_assert_series_equal.py @@ -208,6 +208,10 @@ class MySeries(Series): tm.assert_series_equal(s1, s2, check_series_type=True) tm.assert_series_equal(s1, s3, check_series_type=False) + tm.assert_series_equal(s3, s1, check_series_type=False) with pytest.raises(AssertionError, match="Series type not equal"): tm.assert_series_equal(s1, s3, check_series_type=True) + + with pytest.raises(AssertionError, match="Series type not equal"): + tm.assert_series_equal(s3, s1, check_series_type=True) From 2e2586e621dc3018e0def0b8bfc58e108dc270c6 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 16 Mar 2020 12:47:18 +0100 Subject: [PATCH 4/6] fix test --- pandas/tests/frame/test_subclass.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/tests/frame/test_subclass.py b/pandas/tests/frame/test_subclass.py index a2e7dc527c4b8..16bf651829a04 100644 --- a/pandas/tests/frame/test_subclass.py +++ b/pandas/tests/frame/test_subclass.py @@ -163,12 +163,14 @@ def test_subclass_align_combinations(self): # frame + series res1, res2 = df.align(s, axis=0) - exp1 = pd.DataFrame( + exp1 = tm.SubclassedDataFrame( {"a": [1, np.nan, 3, np.nan, 5], "b": [1, np.nan, 3, np.nan, 5]}, index=list("ABCDE"), ) # name is lost when - exp2 = pd.Series([1, 2, np.nan, 4, np.nan], index=list("ABCDE"), name="x") + exp2 = tm.SubclassedSeries( + [1, 2, np.nan, 4, np.nan], index=list("ABCDE"), name="x" + ) assert isinstance(res1, tm.SubclassedDataFrame) tm.assert_frame_equal(res1, exp1) From b0f9cfbae6bfa8c6fc09f64aaedc583647a9b0ee Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 16 Mar 2020 14:09:42 +0100 Subject: [PATCH 5/6] Clean-up comments, use assert_clas_equal --- pandas/_testing.py | 7 +------ pandas/tests/util/test_assert_series_equal.py | 4 ++-- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index c3af0594c6965..d473b453d77d2 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -1105,12 +1105,7 @@ def assert_series_equal( _check_isinstance(left, right, Series) if check_series_type: - # TODO: There are some tests using rhs is sparse - # lhs is dense. Should use assert_class_equal in future - assert type(left) is type( - right - ), f"Series type not equal: {type(left)} vs {type(right)}" - # assert_class_equal(left, right, obj=obj) + assert_class_equal(left, right, obj=obj) # length comparison if len(left) != len(right): diff --git a/pandas/tests/util/test_assert_series_equal.py b/pandas/tests/util/test_assert_series_equal.py index f765bb4e06cf3..2550b32446055 100644 --- a/pandas/tests/util/test_assert_series_equal.py +++ b/pandas/tests/util/test_assert_series_equal.py @@ -210,8 +210,8 @@ class MySeries(Series): tm.assert_series_equal(s1, s3, check_series_type=False) tm.assert_series_equal(s3, s1, check_series_type=False) - with pytest.raises(AssertionError, match="Series type not equal"): + with pytest.raises(AssertionError, match="Series classes are different"): tm.assert_series_equal(s1, s3, check_series_type=True) - with pytest.raises(AssertionError, match="Series type not equal"): + with pytest.raises(AssertionError, match="Series classes are different"): tm.assert_series_equal(s3, s1, check_series_type=True) From d07388e7872de7aeb03bed9793de5adc5e333d8f Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 16 Mar 2020 14:12:09 +0100 Subject: [PATCH 6/6] add whatsnew --- doc/source/whatsnew/v1.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 9be994ee7104f..7f86366eb6ae3 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -395,6 +395,7 @@ Other - Set operations on an object-dtype :class:`Index` now always return object-dtype results (:issue:`31401`) - Bug in :meth:`AbstractHolidayCalendar.holidays` when no rules were defined (:issue:`31415`) - Bug in :meth:`DataFrame.to_records` incorrectly losing timezone information in timezone-aware ``datetime64`` columns (:issue:`32535`) +- Fixed :func:`pandas.testing.assert_series_equal` to correctly raise if left object is a different subclass with ``check_series_type=True`` (:issue:`32670`). .. ---------------------------------------------------------------------------