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

ENH(string dtype): fallback for HDF5 with UTF-8 surrogates #60993

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
54 changes: 39 additions & 15 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3170,12 +3170,28 @@ def read_index_node(
**kwargs,
)
else:
index = factory(
_unconvert_index(
data, kind, encoding=self.encoding, errors=self.errors
),
**kwargs,
)
try:
index = factory(
_unconvert_index(
data, kind, encoding=self.encoding, errors=self.errors
),
**kwargs,
)
except UnicodeEncodeError as err:
if (
self.errors == "surrogatepass"
and get_option("future.infer_string")
and str(err).endswith("surrogates not allowed")
):
index = factory(
_unconvert_index(
data, kind, encoding=self.encoding, errors=self.errors
),
dtype="object",
**kwargs,
)
else:
raise

index.name = name

Expand Down Expand Up @@ -3311,13 +3327,19 @@ def read(
self.validate_read(columns, where)
index = self.read_index("index", start=start, stop=stop)
values = self.read_array("values", start=start, stop=stop)
result = Series(values, index=index, name=self.name, copy=False)
if (
using_string_dtype()
and isinstance(values, np.ndarray)
and is_string_array(values, skipna=True)
):
result = result.astype(StringDtype(na_value=np.nan))
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 was added back in #54431. I do not believe it is no longer necessary - we will infer string in the Series constructor.

try:
result = Series(values, index=index, name=self.name, copy=False)
except UnicodeEncodeError as err:
if (
self.errors == "surrogatepass"
and using_string_dtype()
and str(err).endswith("surrogates not allowed")
):
result = Series(
values, index=index, name=self.name, copy=False, dtype="object"
)
else:
raise
return result

def write(self, obj, **kwargs) -> None:
Expand Down Expand Up @@ -5224,7 +5246,7 @@ def _convert_string_array(data: np.ndarray, encoding: str, errors: str) -> np.nd
# encode if needed
if len(data):
data = (
Series(data.ravel(), copy=False)
Series(data.ravel(), copy=False, dtype="object")
Copy link
Member Author

Choose a reason for hiding this comment

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

We go immediately back to NumPy, so no reason to use string dtypes here.

.str.encode(encoding, errors)
._values.reshape(data.shape)
)
Expand Down Expand Up @@ -5264,7 +5286,9 @@ def _unconvert_string_array(
dtype = f"U{itemsize}"

if isinstance(data[0], bytes):
ser = Series(data, copy=False).str.decode(encoding, errors=errors)
ser = Series(data, copy=False).str.decode(
encoding, errors=errors, dtype="object"
)
Comment on lines +5321 to +5323
Copy link
Member Author

Choose a reason for hiding this comment

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

We go immediately back to NumPy, so no reason to use string dtypes here.

data = ser.to_numpy()
data.flags.writeable = True
else:
Expand Down
9 changes: 1 addition & 8 deletions pandas/tests/io/pytables/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import numpy as np
import pytest

from pandas._config import using_string_dtype

from pandas.compat import PY312

import pandas as pd
Expand All @@ -25,7 +23,6 @@
timedelta_range,
)
import pandas._testing as tm
from pandas.conftest import has_pyarrow
from pandas.tests.io.pytables.common import (
_maybe_remove,
ensure_clean_store,
Expand Down Expand Up @@ -385,14 +382,10 @@ def test_to_hdf_with_min_itemsize(tmp_path, setup_path):
tm.assert_series_equal(read_hdf(path, "ss4"), concat([df["B"], df2["B"]]))


@pytest.mark.xfail(
using_string_dtype() and has_pyarrow,
reason="TODO(infer_string): can't encode '\ud800': surrogates not allowed",
)
@pytest.mark.parametrize("format", ["fixed", "table"])
def test_to_hdf_errors(tmp_path, format, setup_path):
data = ["\ud800foo"]
ser = Series(data, index=Index(data))
ser = Series(data, index=Index(data, dtype="object"), dtype="object")
path = tmp_path / setup_path
# GH 20835
ser.to_hdf(path, key="table", format=format, errors="surrogatepass")
Expand Down
Loading