Skip to content

String dtype: implement object-dtype based StringArray variant with NumPy semantics #58451

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

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
63a7fc5
String dtype: implement object-dtype based StringArray variant with N…
jorisvandenbossche Apr 27, 2024
0eee625
fix constructor to not convert to NA
jorisvandenbossche Apr 27, 2024
607b95e
fix typing
jorisvandenbossche Apr 27, 2024
bca157d
improve logic in str_map
jorisvandenbossche Apr 27, 2024
79eb3b4
Merge remote-tracking branch 'upstream/main' into string-dtype-object
jorisvandenbossche Jul 26, 2024
c063298
Merge remote-tracking branch 'upstream/main' into string-dtype-object
jorisvandenbossche Jul 30, 2024
ab96aa4
remove most usage of python_numpy
jorisvandenbossche Jul 30, 2024
bae8d65
update tests to avoid string[python_numpy]
jorisvandenbossche Jul 30, 2024
31f1c33
Merge remote-tracking branch 'upstream/main' into string-dtype-object
jorisvandenbossche Jul 31, 2024
cbd0820
Merge remote-tracking branch 'upstream/main' into string-dtype-object
jorisvandenbossche Aug 1, 2024
864c166
remove all python_numpy usage
jorisvandenbossche Aug 1, 2024
d3ad7b0
remove hardcoded storage
jorisvandenbossche Aug 2, 2024
028dc2c
implement any/all reductions
jorisvandenbossche Aug 2, 2024
1750bcb
Merge remote-tracking branch 'upstream/main' into string-dtype-object
jorisvandenbossche Aug 3, 2024
7f4baf7
fix typing
jorisvandenbossche Aug 3, 2024
fdf1454
Merge remote-tracking branch 'upstream/main' into string-dtype-object
jorisvandenbossche Aug 7, 2024
fe6fce6
Update pandas/core/arrays/string_.py
jorisvandenbossche Aug 7, 2024
70325d4
update todo comment
jorisvandenbossche Aug 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2692,7 +2692,7 @@ def maybe_convert_objects(ndarray[object] objects,
if using_pyarrow_string_dtype() and is_string_array(objects, skipna=True):
from pandas.core.arrays.string_ import StringDtype

dtype = StringDtype(storage="pyarrow_numpy")
dtype = StringDtype()
return dtype.construct_array_type()._from_sequence(objects, dtype=dtype)

elif convert_to_nullable_dtype and is_string_array(objects, skipna=True):
Expand Down
10 changes: 10 additions & 0 deletions pandas/_testing/asserters.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,16 @@ def assert_extension_array_equal(
left_na, right_na, obj=f"{obj} NA mask", index_values=index_values
)

# Specifically for StringArrayNumpySemantics, validate here we have a valid array
if isinstance(left.dtype, StringDtype) and left.dtype.storage == "python_numpy":
assert np.all(
[np.isnan(val) for val in left._ndarray[left_na]] # type: ignore[attr-defined]
), "wrong missing value sentinels"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit a custom check (and we don't do anything similarly for other types), but given I initially overlooked a case where we were creating string arrays with the wrong missing value sentinel because the tests don't actually catch that (two arrays with different missing value sentinels still pass as equal in case of EAs), I would prefer keeping this in at least on the short term.

if isinstance(right.dtype, StringDtype) and right.dtype.storage == "python_numpy":
assert np.all(
[np.isnan(val) for val in right._ndarray[right_na]] # type: ignore[attr-defined]
), "wrong missing value sentinels"

left_valid = left[~left_na].to_numpy(dtype=object)
right_valid = right[~right_na].to_numpy(dtype=object)
if check_exact:
Expand Down
2 changes: 2 additions & 0 deletions pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import pandas.compat.compressors
from pandas.compat.numpy import is_numpy_dev
from pandas.compat.pyarrow import (
HAS_PYARROW,
pa_version_under10p1,
pa_version_under11p0,
pa_version_under13p0,
Expand Down Expand Up @@ -189,6 +190,7 @@ def get_bz2_file() -> type[pandas.compat.compressors.BZ2File]:
"pa_version_under14p0",
"pa_version_under14p1",
"pa_version_under16p0",
"HAS_PYARROW",
"IS64",
"ISMUSL",
"PY310",
Expand Down
2 changes: 2 additions & 0 deletions pandas/compat/pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
pa_version_under14p1 = _palv < Version("14.0.1")
pa_version_under15p0 = _palv < Version("15.0.0")
pa_version_under16p0 = _palv < Version("16.0.0")
HAS_PYARROW = True
except ImportError:
pa_version_under10p1 = True
pa_version_under11p0 = True
Expand All @@ -25,3 +26,4 @@
pa_version_under14p1 = True
pa_version_under15p0 = True
pa_version_under16p0 = True
HAS_PYARROW = False
2 changes: 2 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,7 @@ def nullable_string_dtype(request):
@pytest.fixture(
params=[
"python",
"python_numpy",
pytest.param("pyarrow", marks=td.skip_if_no("pyarrow")),
pytest.param("pyarrow_numpy", marks=td.skip_if_no("pyarrow")),
]
Expand Down Expand Up @@ -1353,6 +1354,7 @@ def object_dtype(request):
params=[
"object",
"string[python]",
"string[python_numpy]",
pytest.param("string[pyarrow]", marks=td.skip_if_no("pyarrow")),
pytest.param("string[pyarrow_numpy]", marks=td.skip_if_no("pyarrow")),
]
Expand Down
Loading
Loading