From c32548c24759f0f4cffa3471030d241ee1eff7bf Mon Sep 17 00:00:00 2001 From: Calvin Ho <18680207+calvh@users.noreply.github.com> Date: Sat, 21 Aug 2021 17:26:34 -0400 Subject: [PATCH 1/4] TST: Test inequality w/ complex dtypes (GH28050) Should raise TypeError --- pandas/tests/dtypes/test_dtypes.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index abb29ce66fd34..4f968dd7200da 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -1095,3 +1095,16 @@ def test_period_dtype_compare_to_string(): dtype = PeriodDtype(freq="M") assert (dtype == "period[M]") is True assert (dtype != "period[M]") is False + + +def test_compare_complex_dtypes(): + # GH 28050 + # https://github.com/pandas-dev/pandas/issues/28050 + arr = np.arange(5).astype(np.complex128) + df = pd.DataFrame(arr) + + with pytest.raises( + TypeError, + match="'<' not supported between instances of 'complex' and 'complex'", + ): + df.lt(df.astype(object)) From 807f5470396e5d333aa41d64d08388b783f5bb9d Mon Sep 17 00:00:00 2001 From: Calvin Ho <18680207+calvh@users.noreply.github.com> Date: Sat, 21 Aug 2021 17:35:46 -0400 Subject: [PATCH 2/4] Add test for complex dtype comparison using '<' --- pandas/tests/dtypes/test_dtypes.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index 4f968dd7200da..37c3584cfa154 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -1103,6 +1103,12 @@ def test_compare_complex_dtypes(): arr = np.arange(5).astype(np.complex128) df = pd.DataFrame(arr) + with pytest.raises( + TypeError, + match="'<' not supported between instances of 'complex' and 'complex'", + ): + df < df.astype(object) + with pytest.raises( TypeError, match="'<' not supported between instances of 'complex' and 'complex'", From 3b5bf729b3a9659f343eebc9f179587c198a73ff Mon Sep 17 00:00:00 2001 From: Calvin Ho <18680207+calvh@users.noreply.github.com> Date: Sat, 21 Aug 2021 21:26:00 -0400 Subject: [PATCH 3/4] Refactor code to reduce repetition Remove https link to issue (keep issue number) Save match message in variable for multiple use --- pandas/tests/dtypes/test_dtypes.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index 37c3584cfa154..7729f95214bed 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -1099,18 +1099,12 @@ def test_period_dtype_compare_to_string(): def test_compare_complex_dtypes(): # GH 28050 - # https://github.com/pandas-dev/pandas/issues/28050 arr = np.arange(5).astype(np.complex128) df = pd.DataFrame(arr) + msg = "'<' not supported between instances of 'complex' and 'complex'" - with pytest.raises( - TypeError, - match="'<' not supported between instances of 'complex' and 'complex'", - ): + with pytest.raises(TypeError, match=msg): df < df.astype(object) - with pytest.raises( - TypeError, - match="'<' not supported between instances of 'complex' and 'complex'", - ): + with pytest.raises(TypeError, match=msg): df.lt(df.astype(object)) From ff265c1efa06723beabd623aa1d759ee614564fd Mon Sep 17 00:00:00 2001 From: Calvin Ho <18680207+calvh@users.noreply.github.com> Date: Sun, 22 Aug 2021 09:50:25 -0400 Subject: [PATCH 4/4] Combine arange call with DataFrame call in test --- pandas/tests/dtypes/test_dtypes.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index 7729f95214bed..30447de874aaa 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -1099,8 +1099,7 @@ def test_period_dtype_compare_to_string(): def test_compare_complex_dtypes(): # GH 28050 - arr = np.arange(5).astype(np.complex128) - df = pd.DataFrame(arr) + df = pd.DataFrame(np.arange(5).astype(np.complex128)) msg = "'<' not supported between instances of 'complex' and 'complex'" with pytest.raises(TypeError, match=msg):