Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: numpy.strings.lstrip/strip/rstrip fails with nulls #26390

Closed
lithomas1 opened this issue May 6, 2024 · 3 comments · Fixed by #26392
Closed

BUG: numpy.strings.lstrip/strip/rstrip fails with nulls #26390

lithomas1 opened this issue May 6, 2024 · 3 comments · Fixed by #26392
Labels

Comments

@lithomas1
Copy link
Collaborator

lithomas1 commented May 6, 2024

Describe the issue:

When encountering a null value in numpy 2.0 StringDType'd array, we get

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/thomasli/opt/miniconda3/envs/pandas-dev/lib/python3.10/site-packages/numpy/_core/strings.py", line 881, in rstrip
    return _rstrip_whitespace(a)
ValueError: Cannot strip null values that are not strings

It is expected that the nulls are propagated.

Reproduce the code example:

import numpy as np
a = np.array(["a", "b", np.nan], dtype=np.dtypes.StringDType(na_object=np.nan))
np.strings.rstrip(a)
np.strings.lstrip(a)
np.strings.strip(a)

Error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/thomasli/opt/miniconda3/envs/pandas-dev/lib/python3.10/site-packages/numpy/_core/strings.py", line 881, in rstrip
    return _rstrip_whitespace(a)
ValueError: Cannot strip null values that are not strings

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/thomasli/opt/miniconda3/envs/pandas-dev/lib/python3.10/site-packages/numpy/_core/strings.py", line 926, in strip
    return _strip_whitespace(a)
ValueError: Cannot strip null values that are not strings

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/thomasli/opt/miniconda3/envs/pandas-dev/lib/python3.10/site-packages/numpy/_core/strings.py", line 840, in lstrip
    return _lstrip_whitespace(a)
ValueError: Cannot strip null values that are not strings

Python and NumPy Versions:

2.1.0.dev0+git20240424.f8392ce

Runtime Environment:

No response

Context for the issue:

No response

@ngoldbaum
Copy link
Member

Updated the script to use np.nan, which should work. Thanks for the report.

@ngoldbaum
Copy link
Member

Opened #26392 with a fix.

@lithomas1
Copy link
Collaborator Author

Thx for the quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants