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

TST(string dtype): Resolve xfails in pytables #60795

Merged
merged 11 commits into from
Feb 10, 2025

Conversation

rhshadrach
Copy link
Member

@rhshadrach rhshadrach commented Jan 26, 2025

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

Looks like using where that results in empty will still give object dtype. xfailing those tests here and plan to tackle in a followup.

@rhshadrach rhshadrach added Testing pandas testing functions or related to the test suite IO HDF5 read_hdf, HDFStore Strings String extension data type and string data labels Jan 26, 2025
@rhshadrach rhshadrach added this to the 2.3 milestone Jan 26, 2025
@rhshadrach rhshadrach marked this pull request as draft January 26, 2025 13:47
Comment on lines 257 to 260
if using_infer_string:
# TODO: Test is incorrect when not using_infer_string.
# Should take the last 4 rows uncondiationally.
expected = expected[16:]
Copy link
Member Author

Choose a reason for hiding this comment

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

Would like to make sure this is correct.

Copy link
Member

Choose a reason for hiding this comment

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

So the string type is just truncating the last 4 rows? Is it an invalid unicode sequence?

Copy link
Member Author

Choose a reason for hiding this comment

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

The DataFrame is all NaN in rows 0-15 inclusive (L250 in this PR), and it is being appended to the store with dropna=True.

Copy link
Member

Choose a reason for hiding this comment

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

So this is currently a bug then? (that indirectly gets fixed somehow by the presence of a string dtype? do you know if this also happens if there is no string column?)

Copy link
Member Author

Choose a reason for hiding this comment

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

@jorisvandenbossche - the cases in this test above show that when dropna=True, the NA rows are dropped for e.g. floats.

I missed L239:

# nan some entire rows (string are still written!)

This goes back to #3013. I am wondering if at one point the line df.ix[0:15,:] = np.nan did not put the NaNs in string columns, in which case it makes sense to not drop these rows?

In any case, the behavior for string dtype is now working equivalently to other dtypes.

@rhshadrach rhshadrach marked this pull request as ready for review February 2, 2025 21:19
Comment on lines 257 to 260
if using_infer_string:
# TODO: Test is incorrect when not using_infer_string.
# Should take the last 4 rows uncondiationally.
expected = expected[16:]
Copy link
Member

Choose a reason for hiding this comment

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

So the string type is just truncating the last 4 rows? Is it an invalid unicode sequence?

@@ -822,10 +826,11 @@ def test_append_raise(setup_path):
df["foo"] = Timestamp("20130101")
store.append("df", df)
df["foo"] = "bar"
shape = "(30,)" if using_infer_string else "(1, 30)"
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this still a bug? Not sure why we would expect a different shape with the string data types?

Copy link
Member Author

Choose a reason for hiding this comment

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

I assumed it was due to array-backed vs 1d ndarray backed data. But I haven't checked too deeply.

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 error message got changed in #60829; we no longer report on the shape.

"Cannot serialize the column [datetime1]\nbecause its data "
"contents are not [string] but [date] object dtype"
),
re.escape("[date] is not implemented as a table column"),
Copy link
Member

Choose a reason for hiding this comment

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

I think the original error message here is much clearer - is there no way to catch and raise that for the string types?

Copy link
Member Author

@rhshadrach rhshadrach Feb 4, 2025

Choose a reason for hiding this comment

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

Not sure what you mean, this error is not raised for string types. It's being raised for date types.

When infer_string=False, this function is passed a single object block with a mix of strings and dates. In this case, the data of the block is inferred as mixed, and then checked column-by-column. This is where the top message (which I think is confusing) is raised. When infer_string=True, each string array is fed into this function individually and does not raise. Then the object block is fed in containing only dates. This is inferred as dates, and the corresponding error message is raised.

@rhshadrach
Copy link
Member Author

@WillAyd - friendly ping

Comment on lines +388 to +391
@pytest.mark.xfail(
using_string_dtype() and has_pyarrow,
reason="TODO(infer_string): can't encode '\ud800': surrogates not allowed",
)
Copy link
Member

Choose a reason for hiding this comment

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

Do you know if the error happens when writing or reading? (I suppose reading because even if it was object dtype we try to read it as strings?)

This is probably not something we can fix? Wondering (not for this PR) if we should fall back to object dtype in such a case (or at least give that option), so the user can still load the data and inspect it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I hadn't looked into this one - currently it raises on data creation. Fixing that by specifying object dtype, you're right it's the reading that raises here. The error happens in Series.str.decode(encoding, errors=errors). I am wondering if we should add a dtype argument to decode so that you can force object dtype - that seems generally useful to alleviate this pain-point, and then we could use it to provide a fallback in pytables.

Copy link
Member Author

Choose a reason for hiding this comment

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

PyArrow strings not supporting surrogates is another interesting case in regards to PDEP-13. #58455

Copy link
Member

Choose a reason for hiding this comment

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

I am wondering if we should add a dtype argument to decode so that you can force object dtype - that seems generally useful to alleviate this pain-point, and then we could use it to provide a fallback in pytables.

+1

Copy link
Member

Choose a reason for hiding this comment

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

I listed this issue in #59328 as one of the breaking changes when users upgrade to the string dtype (in this case at least when using pyarrow).

But so we should also list this as one of the differences between the pyarrow and python engines.

Copy link
Member

Choose a reason for hiding this comment

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

I do wonder if we should start nudging users down the path of referring to our strings as utf8. For other encodings, the binary type is the best bet for now, and probably for a while (I'm not aware of any non-utf8 Arrow array work, but maybe its out there)

@WillAyd WillAyd merged commit 4511251 into pandas-dev:main Feb 10, 2025
42 checks passed
@WillAyd
Copy link
Member

WillAyd commented Feb 10, 2025

Great work thanks @rhshadrach

Copy link

lumberbot-app bot commented Feb 10, 2025

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout 2.3.x
git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 4511251ccf409f2ba71cab0283bdf751697ee539
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #60795: TST(string dtype): Resolve xfails in pytables'
  1. Push to a named branch:
git push YOURFORK 2.3.x:auto-backport-of-pr-60795-on-2.3.x
  1. Create a PR against branch 2.3.x, I would have named this PR:

"Backport PR #60795 on branch 2.3.x (TST(string dtype): Resolve xfails in pytables)"

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove the Still Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free to suggest an improvement.

@rhshadrach
Copy link
Member Author

@meeseeksdev backport to 2.3.x

Copy link

lumberbot-app bot commented Feb 11, 2025

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout 2.3.x
git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 4511251ccf409f2ba71cab0283bdf751697ee539
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #60795: TST(string dtype): Resolve xfails in pytables'
  1. Push to a named branch:
git push YOURFORK 2.3.x:auto-backport-of-pr-60795-on-2.3.x
  1. Create a PR against branch 2.3.x, I would have named this PR:

"Backport PR #60795 on branch 2.3.x (TST(string dtype): Resolve xfails in pytables)"

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove the Still Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free to suggest an improvement.

rhshadrach added a commit to rhshadrach/pandas that referenced this pull request Feb 11, 2025
@rhshadrach
Copy link
Member Author

Backport PR: #60916

mroeschke pushed a commit that referenced this pull request Feb 16, 2025
…0916)

* ENH: Improved error message and raise new error for small-string NaN edge case in HDFStore.append (#60829)

* Add clearer error messages for datatype mismatch in HDFStore.append. Raise ValueError when nan_rep too large for pytable column. Add and modify applicable test code.

* Fix missed tests and correct mistake in error message.

* Remove excess comments. Reverse error type change to avoid api changes. Move nan_rep tests into separate function.

(cherry picked from commit 57340ec)

* TST(string dtype): Resolve xfails in pytables (#60795)

(cherry picked from commit 4511251)

* Adjust test

---------

Co-authored-by: Jake Thomas Trevallion <[email protected]>
mroeschke pushed a commit that referenced this pull request Feb 20, 2025
…ading with condition (#60967)

* ENH: Improved error message and raise new error for small-string NaN edge case in HDFStore.append (#60829)

* Add clearer error messages for datatype mismatch in HDFStore.append. Raise ValueError when nan_rep too large for pytable column. Add and modify applicable test code.

* Fix missed tests and correct mistake in error message.

* Remove excess comments. Reverse error type change to avoid api changes. Move nan_rep tests into separate function.

(cherry picked from commit 57340ec)

* TST(string dtype): Resolve xfails in pytables (#60795)

(cherry picked from commit 4511251)

* BUG(string dtype): Resolve pytables xfail when reading with condition (#60943)

(cherry picked from commit 0ec5f26)

---------

Co-authored-by: Jake Thomas Trevallion <[email protected]>
mroeschke pushed a commit that referenced this pull request Feb 20, 2025
* ENH: Improved error message and raise new error for small-string NaN edge case in HDFStore.append (#60829)

* Add clearer error messages for datatype mismatch in HDFStore.append. Raise ValueError when nan_rep too large for pytable column. Add and modify applicable test code.

* Fix missed tests and correct mistake in error message.

* Remove excess comments. Reverse error type change to avoid api changes. Move nan_rep tests into separate function.

(cherry picked from commit 57340ec)

* TST(string dtype): Resolve xfails in pytables (#60795)

(cherry picked from commit 4511251)

* BUG(string dtype): Resolve pytables xfail when reading with condition (#60943)

(cherry picked from commit 0ec5f26)

* Backport PR #60940: ENH: Add dtype argument to str.decode

---------

Co-authored-by: Jake Thomas Trevallion <[email protected]>
mroeschke pushed a commit that referenced this pull request Feb 22, 2025
…cked strings (#60984)

* ENH: Improved error message and raise new error for small-string NaN edge case in HDFStore.append (#60829)

* Add clearer error messages for datatype mismatch in HDFStore.append. Raise ValueError when nan_rep too large for pytable column. Add and modify applicable test code.

* Fix missed tests and correct mistake in error message.

* Remove excess comments. Reverse error type change to avoid api changes. Move nan_rep tests into separate function.

(cherry picked from commit 57340ec)

* TST(string dtype): Resolve xfails in pytables (#60795)

(cherry picked from commit 4511251)

* BUG(string dtype): Resolve pytables xfail when reading with condition (#60943)

(cherry picked from commit 0ec5f26)

* Backport PR #60940: ENH: Add dtype argument to str.decode

* Backport PR #60938: ENH(string dtype): Implement cumsum for Python-backed strings

---------

Co-authored-by: Jake Thomas Trevallion <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO HDF5 read_hdf, HDFStore Still Needs Manual Backport Strings String extension data type and string data Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants