Skip to content

Commit

Permalink
Replace legacy np.string_ usage with np.bytes_
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese committed Jun 27, 2024
1 parent adde5c1 commit 41265c7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
12 changes: 6 additions & 6 deletions pyspectral/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,11 @@ def test_get_wave_range(self):
@pytest.mark.parametrize(
("input_value", "exp_except"),
[
(np.string_("hey"), False),
(np.array([np.string_("hey")]), False),
(np.array(np.string_("hey")), False),
(np.bytes_("hey"), False),
(np.array([np.bytes_("hey")]), False),
(np.array(np.bytes_("hey")), False),
("hey", False),
(np.array([np.string_("hey"), np.string_("hey")]), True),
(np.array([np.bytes_("hey"), np.bytes_("hey")]), True),
(5, True),
],
)
Expand All @@ -247,8 +247,8 @@ def test_np2str(input_value, exp_except):
[
(b"Hello", "Hello"),
("Hello", "Hello"),
(np.string_("Hello"), "Hello"),
(np.array(np.string_("Hello")), np.array(np.string_("Hello"))),
(np.bytes_("Hello"), "Hello"),
(np.array(np.bytes_("Hello")), np.array(np.bytes_("Hello"))),
]
)
def test_bytes2string(input_value, exp_result):
Expand Down
9 changes: 6 additions & 3 deletions pyspectral/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,20 +506,23 @@ def convert2str(value):


def np2str(value):
"""Convert an `numpy.string_` to str.
"""Convert an ``numpy.bytes_`` to str.
Note: ``numpy.string_`` was deprecated in numpy 2.0 in favor of
``numpy.bytes_``.
Args:
value (ndarray): scalar or 1-element numpy array to convert
Raises:
ValueError: if value is array larger than 1-element or it is not of
type `numpy.string_` or it is not a numpy array
type `numpy.bytes_` or it is not a numpy array
"""
if isinstance(value, str):
return value

if hasattr(value, 'dtype') and \
issubclass(value.dtype.type, (np.str_, np.string_, np.object_)) \
issubclass(value.dtype.type, (np.str_, np.bytes_, np.object_)) \
and value.size == 1:
value = value.item()
# python 3 - was scalar numpy array of bytes
Expand Down

0 comments on commit 41265c7

Please sign in to comment.