Skip to content

Commit

Permalink
Minor fixups for future NumPy 2 compatibility (#15590)
Browse files Browse the repository at this point in the history
These are some small fixes to be compatible with NumPy 2 python changes, as pointed out by the `ruff` with the `"NPY201"` rule-set.

I am not really happy with the `_NUMPY_SCTYPES` (reaching into what is now private, but figured that others will do so also for a while; feels like we should add a better way to do this in NumPy before removing it).
Listing the full set is also a bit ugly/convoluted, but happy to do so instead.

(I was hoping to get a bit further with testing against the NumPy 2rc, but unfortunately the `numba` dependency makes that at least difficult.)

Authors:
  - Sebastian Berg (https://github.com/seberg)
  - GALI PREM SAGAR (https://github.com/galipremsagar)

Approvers:
  - GALI PREM SAGAR (https://github.com/galipremsagar)

URL: #15590
  • Loading branch information
seberg authored Apr 26, 2024
1 parent c62c5f6 commit 79cd473
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 56 deletions.
2 changes: 1 addition & 1 deletion python/cudf/cudf/core/column/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ def contains(
Returning an Index of booleans using only a literal pattern.
>>> data = ['Mouse', 'dog', 'house and parrot', '23.0', np.NaN]
>>> data = ['Mouse', 'dog', 'house and parrot', '23.0', np.nan]
>>> idx = cudf.Index(data)
>>> idx
Index(['Mouse', 'dog', 'house and parrot', '23.0', None], dtype='object')
Expand Down
12 changes: 6 additions & 6 deletions python/cudf/cudf/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ def isna(self):
>>> import cudf
>>> import numpy as np
>>> import pandas as pd
>>> df = cudf.DataFrame({'age': [5, 6, np.NaN],
>>> df = cudf.DataFrame({'age': [5, 6, np.nan],
... 'born': [pd.NaT, pd.Timestamp('1939-05-27'),
... pd.Timestamp('1940-04-25')],
... 'name': ['Alfred', 'Batman', ''],
Expand All @@ -1095,7 +1095,7 @@ def isna(self):
Show which entries in a Series are NA.
>>> ser = cudf.Series([5, 6, np.NaN, np.inf, -np.inf])
>>> ser = cudf.Series([5, 6, np.nan, np.inf, -np.inf])
>>> ser
0 5.0
1 6.0
Expand All @@ -1113,7 +1113,7 @@ def isna(self):
Show which entries in an Index are NA.
>>> idx = cudf.Index([1, 2, None, np.NaN, 0.32, np.inf])
>>> idx = cudf.Index([1, 2, None, np.nan, 0.32, np.inf])
>>> idx
Index([1.0, 2.0, <NA>, <NA>, 0.32, Inf], dtype='float64')
>>> idx.isna()
Expand Down Expand Up @@ -1156,7 +1156,7 @@ def notna(self):
>>> import cudf
>>> import numpy as np
>>> import pandas as pd
>>> df = cudf.DataFrame({'age': [5, 6, np.NaN],
>>> df = cudf.DataFrame({'age': [5, 6, np.nan],
... 'born': [pd.NaT, pd.Timestamp('1939-05-27'),
... pd.Timestamp('1940-04-25')],
... 'name': ['Alfred', 'Batman', ''],
Expand All @@ -1174,7 +1174,7 @@ def notna(self):
Show which entries in a Series are NA.
>>> ser = cudf.Series([5, 6, np.NaN, np.inf, -np.inf])
>>> ser = cudf.Series([5, 6, np.nan, np.inf, -np.inf])
>>> ser
0 5.0
1 6.0
Expand All @@ -1192,7 +1192,7 @@ def notna(self):
Show which entries in an Index are NA.
>>> idx = cudf.Index([1, 2, None, np.NaN, 0.32, np.inf])
>>> idx = cudf.Index([1, 2, None, np.nan, 0.32, np.inf])
>>> idx
Index([1.0, 2.0, <NA>, <NA>, 0.32, Inf], dtype='float64')
>>> idx.notna()
Expand Down
6 changes: 5 additions & 1 deletion python/cudf/cudf/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
from cudf.core.single_column_frame import SingleColumnFrame
from cudf.utils.docutils import copy_docstring
from cudf.utils.dtypes import (
_NUMPY_SCTYPES,
_maybe_convert_to_default_type,
find_common_type,
is_mixed_with_object_dtype,
Expand Down Expand Up @@ -344,7 +345,10 @@ def _data(self):
@_cudf_nvtx_annotate
def __contains__(self, item):
if isinstance(item, bool) or not isinstance(
item, tuple(np.sctypes["int"] + np.sctypes["float"] + [int, float])
item,
tuple(
_NUMPY_SCTYPES["int"] + _NUMPY_SCTYPES["float"] + [int, float]
),
):
return False
try:
Expand Down
33 changes: 0 additions & 33 deletions python/cudf/cudf/tests/test_api_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
(np.float64, False),
(np.complex128, False),
(np.str_, False),
(np.unicode_, False),
(np.datetime64, False),
(np.timedelta64, False),
# NumPy scalars.
Expand All @@ -42,7 +41,6 @@
(np.float64(), False),
(np.complex128(), False),
(np.str_(), False),
(np.unicode_(), False),
(np.datetime64(), False),
(np.timedelta64(), False),
# NumPy dtype objects.
Expand All @@ -61,7 +59,6 @@
(np.array([], dtype=np.float64), False),
(np.array([], dtype=np.complex128), False),
(np.array([], dtype=np.str_), False),
(np.array([], dtype=np.unicode_), False),
(np.array([], dtype=np.datetime64), False),
(np.array([], dtype=np.timedelta64), False),
(np.array([], dtype=object), False),
Expand Down Expand Up @@ -142,7 +139,6 @@ def test_is_categorical_dtype(obj, expect):
(np.float64, True),
(np.complex128, True),
(np.str_, False),
(np.unicode_, False),
(np.datetime64, False),
(np.timedelta64, False),
# NumPy scalars.
Expand All @@ -151,7 +147,6 @@ def test_is_categorical_dtype(obj, expect):
(np.float64(), True),
(np.complex128(), True),
(np.str_(), False),
(np.unicode_(), False),
(np.datetime64(), False),
(np.timedelta64(), False),
# NumPy dtype objects.
Expand All @@ -170,7 +165,6 @@ def test_is_categorical_dtype(obj, expect):
(np.array([], dtype=np.float64), True),
(np.array([], dtype=np.complex128), True),
(np.array([], dtype=np.str_), False),
(np.array([], dtype=np.unicode_), False),
(np.array([], dtype=np.datetime64), False),
(np.array([], dtype=np.timedelta64), False),
(np.array([], dtype=object), False),
Expand Down Expand Up @@ -247,7 +241,6 @@ def test_is_numeric_dtype(obj, expect):
(np.float64, False),
(np.complex128, False),
(np.str_, False),
(np.unicode_, False),
(np.datetime64, False),
(np.timedelta64, False),
# NumPy scalars.
Expand All @@ -256,7 +249,6 @@ def test_is_numeric_dtype(obj, expect):
(np.float64(), False),
(np.complex128(), False),
(np.str_(), False),
(np.unicode_(), False),
(np.datetime64(), False),
(np.timedelta64(), False),
# NumPy dtype objects.
Expand All @@ -275,7 +267,6 @@ def test_is_numeric_dtype(obj, expect):
(np.array([], dtype=np.float64), False),
(np.array([], dtype=np.complex128), False),
(np.array([], dtype=np.str_), False),
(np.array([], dtype=np.unicode_), False),
(np.array([], dtype=np.datetime64), False),
(np.array([], dtype=np.timedelta64), False),
(np.array([], dtype=object), False),
Expand Down Expand Up @@ -352,7 +343,6 @@ def test_is_integer_dtype(obj, expect):
(np.float64, False),
(np.complex128, False),
(np.str_, False),
(np.unicode_, False),
(np.datetime64, False),
(np.timedelta64, False),
# NumPy scalars.
Expand All @@ -361,7 +351,6 @@ def test_is_integer_dtype(obj, expect):
(np.float64(), False),
(np.complex128(), False),
(np.str_(), False),
(np.unicode_(), False),
(np.datetime64(), False),
(np.timedelta64(), False),
# NumPy dtype objects.
Expand All @@ -380,7 +369,6 @@ def test_is_integer_dtype(obj, expect):
(np.array([], dtype=np.float64), False),
(np.array([], dtype=np.complex128), False),
(np.array([], dtype=np.str_), False),
(np.array([], dtype=np.unicode_), False),
(np.array([], dtype=np.datetime64), False),
(np.array([], dtype=np.timedelta64), False),
(np.array([], dtype=object), False),
Expand Down Expand Up @@ -458,7 +446,6 @@ def test_is_integer(obj, expect):
(np.float64, False),
(np.complex128, False),
(np.str_, True),
(np.unicode_, True),
(np.datetime64, False),
(np.timedelta64, False),
# NumPy scalars.
Expand All @@ -467,7 +454,6 @@ def test_is_integer(obj, expect):
(np.float64(), False),
(np.complex128(), False),
(np.str_(), True),
(np.unicode_(), True),
(np.datetime64(), False),
(np.timedelta64(), False),
# NumPy dtype objects.
Expand All @@ -486,7 +472,6 @@ def test_is_integer(obj, expect):
(np.array([], dtype=np.float64), False),
(np.array([], dtype=np.complex128), False),
(np.array([], dtype=np.str_), True),
(np.array([], dtype=np.unicode_), True),
(np.array([], dtype=np.datetime64), False),
(np.array([], dtype=np.timedelta64), False),
# (np.array([], dtype=object), False),
Expand Down Expand Up @@ -577,7 +562,6 @@ def test_is_string_dtype(obj, expect):
(np.float64, False),
(np.complex128, False),
(np.str_, False),
(np.unicode_, False),
(np.datetime64, True),
(np.timedelta64, False),
# NumPy scalars.
Expand All @@ -586,7 +570,6 @@ def test_is_string_dtype(obj, expect):
(np.float64(), False),
(np.complex128(), False),
(np.str_(), False),
(np.unicode_(), False),
(np.datetime64(), True),
(np.timedelta64(), False),
# NumPy dtype objects.
Expand All @@ -605,7 +588,6 @@ def test_is_string_dtype(obj, expect):
(np.array([], dtype=np.float64), False),
(np.array([], dtype=np.complex128), False),
(np.array([], dtype=np.str_), False),
(np.array([], dtype=np.unicode_), False),
(np.array([], dtype=np.datetime64), True),
(np.array([], dtype=np.timedelta64), False),
(np.array([], dtype=object), False),
Expand Down Expand Up @@ -682,7 +664,6 @@ def test_is_datetime_dtype(obj, expect):
(np.float64, False),
(np.complex128, False),
(np.str_, False),
(np.unicode_, False),
(np.datetime64, False),
(np.timedelta64, False),
# NumPy scalars.
Expand All @@ -691,7 +672,6 @@ def test_is_datetime_dtype(obj, expect):
(np.float64(), False),
(np.complex128(), False),
(np.str_(), False),
(np.unicode_(), False),
(np.datetime64(), False),
(np.timedelta64(), False),
# NumPy dtype objects.
Expand All @@ -710,7 +690,6 @@ def test_is_datetime_dtype(obj, expect):
(np.array([], dtype=np.float64), False),
(np.array([], dtype=np.complex128), False),
(np.array([], dtype=np.str_), False),
(np.array([], dtype=np.unicode_), False),
(np.array([], dtype=np.datetime64), False),
(np.array([], dtype=np.timedelta64), False),
(np.array([], dtype=object), False),
Expand Down Expand Up @@ -787,7 +766,6 @@ def test_is_list_dtype(obj, expect):
(np.float64, False),
(np.complex128, False),
(np.str_, False),
(np.unicode_, False),
(np.datetime64, False),
(np.timedelta64, False),
# NumPy scalars.
Expand All @@ -796,7 +774,6 @@ def test_is_list_dtype(obj, expect):
(np.float64(), False),
(np.complex128(), False),
(np.str_(), False),
(np.unicode_(), False),
(np.datetime64(), False),
(np.timedelta64(), False),
# NumPy dtype objects.
Expand All @@ -815,7 +792,6 @@ def test_is_list_dtype(obj, expect):
(np.array([], dtype=np.float64), False),
(np.array([], dtype=np.complex128), False),
(np.array([], dtype=np.str_), False),
(np.array([], dtype=np.unicode_), False),
(np.array([], dtype=np.datetime64), False),
(np.array([], dtype=np.timedelta64), False),
(np.array([], dtype=object), False),
Expand Down Expand Up @@ -895,7 +871,6 @@ def test_is_struct_dtype(obj, expect):
(np.float64, False),
(np.complex128, False),
(np.str_, False),
(np.unicode_, False),
(np.datetime64, False),
(np.timedelta64, False),
# NumPy scalars.
Expand All @@ -904,7 +879,6 @@ def test_is_struct_dtype(obj, expect):
(np.float64(), False),
(np.complex128(), False),
(np.str_(), False),
(np.unicode_(), False),
(np.datetime64(), False),
(np.timedelta64(), False),
# NumPy dtype objects.
Expand All @@ -923,7 +897,6 @@ def test_is_struct_dtype(obj, expect):
(np.array([], dtype=np.float64), False),
(np.array([], dtype=np.complex128), False),
(np.array([], dtype=np.str_), False),
(np.array([], dtype=np.unicode_), False),
(np.array([], dtype=np.datetime64), False),
(np.array([], dtype=np.timedelta64), False),
(np.array([], dtype=object), False),
Expand Down Expand Up @@ -1004,7 +977,6 @@ def test_is_decimal_dtype(obj, expect):
np.float64,
np.complex128,
np.str_,
np.unicode_,
np.datetime64,
np.timedelta64,
# NumPy scalars.
Expand All @@ -1013,7 +985,6 @@ def test_is_decimal_dtype(obj, expect):
np.float64(),
np.complex128(),
np.str_(),
np.unicode_(),
np.datetime64(),
np.timedelta64(),
# NumPy dtype objects.
Expand All @@ -1032,7 +1003,6 @@ def test_is_decimal_dtype(obj, expect):
np.array([], dtype=np.float64),
np.array([], dtype=np.complex128),
np.array([], dtype=np.str_),
np.array([], dtype=np.unicode_),
np.array([], dtype=np.datetime64),
np.array([], dtype=np.timedelta64),
np.array([], dtype=object),
Expand Down Expand Up @@ -1088,7 +1058,6 @@ def test_pandas_agreement(obj):
np.float64,
np.complex128,
np.str_,
np.unicode_,
np.datetime64,
np.timedelta64,
# NumPy scalars.
Expand All @@ -1097,7 +1066,6 @@ def test_pandas_agreement(obj):
np.float64(),
np.complex128(),
np.str_(),
np.unicode_(),
np.datetime64(),
np.timedelta64(),
# NumPy dtype objects.
Expand All @@ -1116,7 +1084,6 @@ def test_pandas_agreement(obj):
np.array([], dtype=np.float64),
np.array([], dtype=np.complex128),
np.array([], dtype=np.str_),
np.array([], dtype=np.unicode_),
np.array([], dtype=np.datetime64),
np.array([], dtype=np.timedelta64),
np.array([], dtype=object),
Expand Down
4 changes: 2 additions & 2 deletions python/cudf/cudf/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ def test_categorical_dataframe_slice_copy():
pd.Series(["1.0", "2.5", "3.001", None, "9"], dtype="category"),
pd.Series(["a", "b", "c", "c", "b", "a", "b", "b"]),
pd.Series(["aa", "b", "c", "c", "bb", "bb", "a", "b", "b"]),
pd.Series([1, 2, 3, 89, None, np.nan, np.NaN], dtype="float64"),
pd.Series([1, 2, 3, 89, None, np.nan, np.nan], dtype="float64"),
pd.Series([1, 2, 3, 89], dtype="float64"),
pd.Series([1, 2.5, 3.001, 89], dtype="float64"),
pd.Series([None, None, None]),
Expand Down Expand Up @@ -493,7 +493,7 @@ def test_categorical_typecast(data, cat_type):
pd.Series([1, 2, 3, 89]),
pd.Series(["a", "b", "c", "c", "b", "a", "b", "b"]),
pd.Series(["aa", "b", "c", "c", "bb", "bb", "a", "b", "b"]),
pd.Series([1, 2, 3, 89, None, np.nan, np.NaN], dtype="float64"),
pd.Series([1, 2, 3, 89, None, np.nan, np.nan], dtype="float64"),
pd.Series([1, 2, 3, 89], dtype="float64"),
pd.Series([1, 2.5, 3.001, 89], dtype="float64"),
pd.Series([None, None, None]),
Expand Down
Loading

0 comments on commit 79cd473

Please sign in to comment.