-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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: Enable pytables to round-trip with StringDtype #60663
ENH: Enable pytables to round-trip with StringDtype #60663
Conversation
pandas/io/pytables.py
Outdated
if dtype == "str[python]": | ||
dtype = StringDtype("python", np.nan) | ||
elif dtype == "string[python]": | ||
dtype = StringDtype("python", NA) | ||
elif dtype == "str[pyarrow]": | ||
dtype = StringDtype("pyarrow", np.nan) | ||
else: | ||
assert dtype == "string[pyarrow]" | ||
dtype = StringDtype("pyarrow", NA) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if there is a better approach here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StringDtype.construct_from_string
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This wouldn't allow round-tripping if you e.g. write out a Python-backed string with NaN-semantics, and read it in an environment with PyArrow installed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool thanks for getting this started. I guess this should be added as an enhancement to 2.3?
pandas/io/pytables.py
Outdated
vlarr = self._handle.create_vlarray(self.group, key, _tables().ObjectAtom()) | ||
vlarr.append(value.to_numpy()) | ||
node = getattr(self.group, key) | ||
if value.dtype == StringDtype("python", np.nan): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need all the branches here or can you just do str(values.dtype)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as #60663 (comment)
if using_string_dtype() and is_string_array(values, skipna=True): | ||
if ( | ||
using_string_dtype() | ||
and isinstance(values, np.ndarray) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming read_array
can return a non-ndarray type here? Does this prevent against pyarrow arrays?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming
read_array
can return a non-ndarray type here?
Correct - datetime and string currently.
More general question here: do we actually need (or want) to fully roundtrip the string dtype in HDF IO? I don't think we do that in other IO methods (except for pickle, I assume). It seems we don't support other nullable or pyarrow dtypes right now in HDF IO, so can't compare with how we roundtrip those. But for example for Parquet we right now only (will, with the latest pyarrow) roundtrip "str" vs "string", but not the exact storage. I think in many cases it is good to "just" get the default string dtype for a file where a column has been stored "as a string column" (for example assume we already had StringDtype by default for a while but would now be changing the default storage from python to payrrow, then I think ideally many people just start getting that default storage when reading existing files. As long as the idea is that the difference should not matter in 99.9% of the cases (except for performance benefits etc, of course)). |
That's a good question - yes generally I don't think it is important to restore the storage of the string types through I/O operations (even for Pickle). I think that even hurts the portability of pandas by potentially encoding information about the execution environment into the I/O storage |
I take this to mean store sentinel values like What if str / string is written out but then read in with |
Yes, but then it could also be (now, I am fine with distinguishing those two, but personally I would also be fine with just storing string data the same and reading them the same depending on the option being set)
Good question. I should check to be sure but for parquet I think it will then still use StringDtype, as in general it will try to preserve any extension dtype. I am personally fine with doing the same here, but also OK to explicitly not restore |
I think this proposal makes sense and works in both current and future versions of pandas. +1 |
@WillAyd @jorisvandenbossche - updated the logic here. Any thoughts on the whatsnew line? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool - this implementation looks great
Co-authored-by: William Ayd <[email protected]>
Thanks @rhshadrach! |
Co-authored-by: William Ayd <[email protected]> (cherry picked from commit 60325b8)
Backport -> #60771 |
…60663) (#60771) ENH: Enable pytables to round-trip with StringDtype (#60663) Co-authored-by: William Ayd <[email protected]> (cherry picked from commit 60325b8) Co-authored-by: Richard Shadrach <[email protected]>
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.Replaces #60625
Writing out with object dtype still infers
str
wheninfer_string=True
.