From 32249a1b8dad0d945ae122a9aacf703da70a61da Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Thu, 18 Jul 2024 14:32:30 -0700 Subject: [PATCH 1/4] Make bool(cudf.DataFrame/cudf.MultiIndex) raise TypeError --- python/cudf/cudf/core/frame.py | 6 ++++++ python/cudf/cudf/core/single_column_frame.py | 6 ------ python/cudf/cudf/tests/test_dataframe.py | 5 +++++ python/cudf/cudf/tests/test_multiindex.py | 5 +++++ 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/python/cudf/cudf/core/frame.py b/python/cudf/cudf/core/frame.py index 802751e47ad..b33c1e2ad25 100644 --- a/python/cudf/cudf/core/frame.py +++ b/python/cudf/cudf/core/frame.py @@ -1587,6 +1587,12 @@ def __pos__(self): def __abs__(self): return self._unaryop("abs") + def __bool__(self): + raise TypeError( + f"The truth value of a {type(self).__name__} is ambiguous. Use " + "a.empty, a.bool(), a.item(), a.any() or a.all()." + ) + # Reductions @classmethod @_performance_tracking diff --git a/python/cudf/cudf/core/single_column_frame.py b/python/cudf/cudf/core/single_column_frame.py index 04c7db7a53c..7efe13d9b45 100644 --- a/python/cudf/cudf/core/single_column_frame.py +++ b/python/cudf/cudf/core/single_column_frame.py @@ -91,12 +91,6 @@ def shape(self) -> tuple[int]: """Get a tuple representing the dimensionality of the Index.""" return (len(self),) - def __bool__(self): - raise TypeError( - f"The truth value of a {type(self)} is ambiguous. Use " - "a.empty, a.bool(), a.item(), a.any() or a.all()." - ) - @property # type: ignore @_performance_tracking def _num_columns(self) -> int: diff --git a/python/cudf/cudf/tests/test_dataframe.py b/python/cudf/cudf/tests/test_dataframe.py index 2009fc49ce5..f5e02fb10b7 100644 --- a/python/cudf/cudf/tests/test_dataframe.py +++ b/python/cudf/cudf/tests/test_dataframe.py @@ -11100,3 +11100,8 @@ def test_from_records_with_index_no_shallow_copy(): data = np.array([(1.0, 2), (3.0, 4)], dtype=[("x", " Date: Thu, 18 Jul 2024 14:35:01 -0700 Subject: [PATCH 2/4] fix for rangeindex --- python/cudf/cudf/core/_base_index.py | 6 ++++++ python/cudf/cudf/tests/test_index.py | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/python/cudf/cudf/core/_base_index.py b/python/cudf/cudf/core/_base_index.py index 479f87bb78b..eeee69c4689 100644 --- a/python/cudf/cudf/core/_base_index.py +++ b/python/cudf/cudf/core/_base_index.py @@ -62,6 +62,12 @@ def copy(self, deep: bool = True) -> Self: def __len__(self): raise NotImplementedError + def __bool__(self): + raise TypeError( + f"The truth value of a {type(self).__name__} is ambiguous. Use " + "a.empty, a.bool(), a.item(), a.any() or a.all()." + ) + @property def size(self): # The size of an index is always its length irrespective of dimension. diff --git a/python/cudf/cudf/tests/test_index.py b/python/cudf/cudf/tests/test_index.py index 9eba6122d26..d76ebe35481 100644 --- a/python/cudf/cudf/tests/test_index.py +++ b/python/cudf/cudf/tests/test_index.py @@ -3294,3 +3294,8 @@ def test_index_assignment_no_shallow_copy(index): df = cudf.DataFrame(range(1)) df.index = index assert df.index is index + + +def test_bool_rangeindex_raises(): + with pytest.raises(TypeError): + bool(cudf.RangeIndex(0)) From c90efa7afd3b1fe4903c5667ec4eddd61c664823 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Thu, 18 Jul 2024 14:49:28 -0700 Subject: [PATCH 3/4] Use assert_exceptions_equal --- python/cudf/cudf/core/_base_index.py | 2 +- python/cudf/cudf/core/frame.py | 2 +- python/cudf/cudf/tests/test_dataframe.py | 8 ++++++-- python/cudf/cudf/tests/test_index.py | 8 ++++++-- python/cudf/cudf/tests/test_multiindex.py | 8 ++++++-- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/python/cudf/cudf/core/_base_index.py b/python/cudf/cudf/core/_base_index.py index eeee69c4689..657acc41b18 100644 --- a/python/cudf/cudf/core/_base_index.py +++ b/python/cudf/cudf/core/_base_index.py @@ -63,7 +63,7 @@ def __len__(self): raise NotImplementedError def __bool__(self): - raise TypeError( + raise ValueError( f"The truth value of a {type(self).__name__} is ambiguous. Use " "a.empty, a.bool(), a.item(), a.any() or a.all()." ) diff --git a/python/cudf/cudf/core/frame.py b/python/cudf/cudf/core/frame.py index b33c1e2ad25..19a2b4da4f3 100644 --- a/python/cudf/cudf/core/frame.py +++ b/python/cudf/cudf/core/frame.py @@ -1588,7 +1588,7 @@ def __abs__(self): return self._unaryop("abs") def __bool__(self): - raise TypeError( + raise ValueError( f"The truth value of a {type(self).__name__} is ambiguous. Use " "a.empty, a.bool(), a.item(), a.any() or a.all()." ) diff --git a/python/cudf/cudf/tests/test_dataframe.py b/python/cudf/cudf/tests/test_dataframe.py index f5e02fb10b7..53ed5d728cb 100644 --- a/python/cudf/cudf/tests/test_dataframe.py +++ b/python/cudf/cudf/tests/test_dataframe.py @@ -11103,5 +11103,9 @@ def test_from_records_with_index_no_shallow_copy(): def test_bool_raises(): - with pytest.raises(TypeError): - bool(cudf.DataFrame()) + assert_exceptions_equal( + lfunc=bool, + rfunc=bool, + lfunc_args_and_kwargs=[[cudf.DataFrame()]], + rfunc_args_and_kwargs=[[pd.DataFrame()]], + ) diff --git a/python/cudf/cudf/tests/test_index.py b/python/cudf/cudf/tests/test_index.py index d76ebe35481..722a64cb553 100644 --- a/python/cudf/cudf/tests/test_index.py +++ b/python/cudf/cudf/tests/test_index.py @@ -3297,5 +3297,9 @@ def test_index_assignment_no_shallow_copy(index): def test_bool_rangeindex_raises(): - with pytest.raises(TypeError): - bool(cudf.RangeIndex(0)) + assert_exceptions_equal( + lfunc=bool, + rfunc=bool, + lfunc_args_and_kwargs=[[pd.RangeIndex(0)]], + rfunc_args_and_kwargs=[[cudf.RangeIndex(0)]], + ) diff --git a/python/cudf/cudf/tests/test_multiindex.py b/python/cudf/cudf/tests/test_multiindex.py index 22c3ddacb3d..2c00d48266c 100644 --- a/python/cudf/cudf/tests/test_multiindex.py +++ b/python/cudf/cudf/tests/test_multiindex.py @@ -2164,5 +2164,9 @@ def test_nunique(array, dropna): def test_bool_raises(): - with pytest.raises(TypeError): - bool(cudf.MultiIndex.from_arrays([range(1)])) + assert_exceptions_equal( + lfunc=bool, + rfunc=bool, + lfunc_args_and_kwargs=[[cudf.MultiIndex.from_arrays([range(1)])]], + rfunc_args_and_kwargs=[[pd.MultiIndex.from_arrays([range(1)])]], + ) From b7386352b0fb53714c2a7024157cff9cc539a9ad Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Fri, 19 Jul 2024 11:19:39 -0700 Subject: [PATCH 4/4] Fix csv test --- python/cudf/cudf/tests/test_csv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/cudf/cudf/tests/test_csv.py b/python/cudf/cudf/tests/test_csv.py index a22a627523f..0525b02b698 100644 --- a/python/cudf/cudf/tests/test_csv.py +++ b/python/cudf/cudf/tests/test_csv.py @@ -1617,7 +1617,7 @@ def test_csv_reader_partial_dtype(dtype): StringIO('"A","B","C"\n0,1,2'), dtype=dtype, usecols=["A", "C"] ) - assert names_df == header_df + assert_eq(names_df, header_df) assert all(names_df.dtypes == ["int16", "int64"])