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): clean-up assorted xfails #60345

Conversation

jorisvandenbossche
Copy link
Member

An bunch of assorted xfails that were no longer needed or had a trivial fix

xref #54792

@jorisvandenbossche jorisvandenbossche added the Strings String extension data type and string data label Nov 17, 2024
@jorisvandenbossche jorisvandenbossche added this to the 2.3 milestone Nov 17, 2024
@jorisvandenbossche jorisvandenbossche merged commit e7d1964 into pandas-dev:main Nov 17, 2024
54 of 55 checks passed
@jorisvandenbossche jorisvandenbossche deleted the string-dtype-tests-assorted branch November 17, 2024 12:41
Copy link

lumberbot-app bot commented Nov 17, 2024

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 e7d1964ab7405d54d919bb289318d01e9eb72cd1
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #60345: TST (string dtype): clean-up assorted xfails'
  1. Push to a named branch:
git push YOURFORK 2.3.x:auto-backport-of-pr-60345-on-2.3.x
  1. Create a PR against branch 2.3.x, I would have named this PR:

"Backport PR #60345 on branch 2.3.x (TST (string dtype): clean-up assorted xfails)"

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.

@jorisvandenbossche
Copy link
Member Author

Manual backport -> #60349

assert np.shares_memory(arr, result) is False
else:
assert np.shares_memory(arr, result) is True

result = obj.to_numpy(copy=False)
if using_infer_string and arr.dtype == object:
if using_infer_string and arr.dtype == object and obj.dtype.storage == "pyarrow":
Copy link
Member

Choose a reason for hiding this comment

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

Why if a user specifically requests not to make a copy are we converting the numpy array to a pyarrow arrow baked string array for what is an immutable index? There are other performance benefits?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's the other way around, the pyarrow array (stored under the hood in obj) is being converted to a numpy array, and that can just never be done without a copy (different memory layout)

Copy link
Member

Choose a reason for hiding this comment

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

the first line of the test is obj = pd.Index(arr, copy=False)

so if we have a numpy arr and specify copy=False for the immutable index we get a pyarrow backed index and a copy is made? and then the .to_numpy() method makes another copy?

>>> pd.options.future.infer_string = True
>>> arr = np.array(["a", "b", "c"], dtype=object)
>>> arr
array(['a', 'b', 'c'], dtype=object)
>>> 
>>> idx = pd.Index(arr, copy=False)
>>> idx
Index(['a', 'b', 'c'], dtype='str')
>>> 

so the question is perhaps should idx = pd.Index(arr, copy=False) return an Index with a string dtype, perhaps raise like numpy now do for __array__ when a copy can't be made or is this a moot point when CoW is extended to the Indexes as the copy keyword would be irrelevant?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, sorry, I assumed you were commenting on what is being tested here, i.e. the obj.to_numpy(copy=False) copying or not.

For pd.Index(arr, copy=False): in general our copy keywords in constructors are not strict, but only mean to avoid a copy at "best effort" (e.g. also if you pass a python list, it will make a copy regardless of that keyword). If we would want to change that more generally, that's a bigger topic to discuss.

this a moot point when CoW is extended to the Indexes as the copy keyword would be irrelevant?

It's certainly not a moot point, because with CoW we actually copy more often on input with non-pandas objects. Although it seems we didn't make that change for Index(..), where the default is still copy=False

def test_union_with_na_when_constructing_dataframe():
# GH43222
series1 = Series(
(1,),
index=MultiIndex.from_arrays(
[Series([None], dtype="string"), Series([None], dtype="string")]
[Series([None], dtype="str"), Series([None], dtype="str")]
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 does fix the test. Is the behavior if this change to the test is not made correct?

i.e. the series1.index.dtypes are object and the series2.index.dtypes are str and the resulting dtype for the columns index using the DataFrame constructor is object. Would we not expect the DataFrame constructor to return a str index for the columns in this case?

Copy link
Member

Choose a reason for hiding this comment

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

perhaps related to #60338?

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.e. the series1.index.dtypes are object and the series2.index.dtypes are str and the resulting dtype for the columns index using the DataFrame constructor is object.

With the above fix (and when infer_string is enabled), the test uses str dtype for the index levels of both series1 and series2, and then also the expected result gets created with that.

So it's only testing that the NaNs are properly matched when creating the rows from Series objects with a MultiIndex, it does not test having different dtypes in series1 vs series2.

That's also something we could test, though (and then if you have object dtype index and str dtype index, one would expect the result to be object dtype index (since that is the "common" dtype), but was not how the test was currently set up.

perhaps related to #60338?

I suppose not because here we don't actually have empty objects. Both series1 and series2 have data and have a non-empty index.

request.applymarker(pytest.mark.xfail("object and strings dont match"))
request.applymarker(
pytest.mark.xfail(
reason="TDOD(infer_string) object and strings dont match"
Copy link
Member

Choose a reason for hiding this comment

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

There's a typo here that may mean this gets missed when grepping the TODOs.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, good catch! Yes, I do grep for that so good to fix that typo

jorisvandenbossche added a commit that referenced this pull request Dec 22, 2024
#60349)

TST (string dtype): clean-up assorted xfails (#60345)

(cherry picked from commit e7d1964)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Strings String extension data type and string data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants