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

[backport 2.3.x] ENH (string dtype): convert string_view columns to future string dtype instead of object dtype in Parquet/Feather IO (#60235) #60291

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 2 additions & 0 deletions pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
pa_version_under14p1,
pa_version_under16p0,
pa_version_under17p0,
pa_version_under18p0,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -191,6 +192,7 @@ def get_bz2_file() -> type[pandas.compat.compressors.BZ2File]:
"pa_version_under14p1",
"pa_version_under16p0",
"pa_version_under17p0",
"pa_version_under18p0",
"HAS_PYARROW",
"IS64",
"ISMUSL",
Expand Down
2 changes: 2 additions & 0 deletions pandas/compat/pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
pa_version_under15p0 = _palv < Version("15.0.0")
pa_version_under16p0 = _palv < Version("16.0.0")
pa_version_under17p0 = _palv < Version("17.0.0")
pa_version_under18p0 = _palv < Version("18.0.0")
HAS_PYARROW = True
except ImportError:
pa_version_under10p1 = True
Expand All @@ -28,4 +29,5 @@
pa_version_under15p0 = True
pa_version_under16p0 = True
pa_version_under17p0 = True
pa_version_under18p0 = False
HAS_PYARROW = False
9 changes: 7 additions & 2 deletions pandas/io/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import numpy as np

from pandas.compat import pa_version_under18p0
from pandas.compat._optional import import_optional_dependency

import pandas as pd
Expand Down Expand Up @@ -32,7 +33,11 @@ def _arrow_dtype_mapping() -> dict:
def arrow_string_types_mapper() -> Callable:
pa = import_optional_dependency("pyarrow")

return {
mapping = {
pa.string(): pd.StringDtype(na_value=np.nan),
pa.large_string(): pd.StringDtype(na_value=np.nan),
}.get
}
if not pa_version_under18p0:
mapping[pa.string_view()] = pd.StringDtype(na_value=np.nan)

return mapping.get
20 changes: 20 additions & 0 deletions pandas/tests/io/test_feather.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import numpy as np
import pytest

from pandas.compat.pyarrow import pa_version_under18p0

import pandas as pd
import pandas._testing as tm

Expand Down Expand Up @@ -250,3 +252,21 @@ def test_string_inference(self, tmp_path):
data={"a": ["x", "y"]}, dtype=pd.StringDtype(na_value=np.nan)
)
tm.assert_frame_equal(result, expected)

@pytest.mark.skipif(pa_version_under18p0, reason="not supported before 18.0")
def test_string_inference_string_view_type(self, tmp_path):
# GH#54798
import pyarrow as pa
from pyarrow import feather

path = tmp_path / "string_view.parquet"
table = pa.table({"a": pa.array([None, "b", "c"], pa.string_view())})
feather.write_feather(table, path)

with pd.option_context("future.infer_string", True):
result = read_feather(path)

expected = pd.DataFrame(
data={"a": [None, "b", "c"]}, dtype=pd.StringDtype(na_value=np.nan)
)
tm.assert_frame_equal(result, expected)
Loading