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

BUG: Groupby not keeping string dtype for empty objects #55619

Merged
merged 4 commits into from Oct 26, 2023

Conversation

phofl
Copy link
Member

@phofl phofl commented Oct 21, 2023

  • 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.

cc @rhshadrach

@phofl phofl added Groupby Strings String extension data type and string data labels Oct 21, 2023
@phofl phofl added this to the 2.1.2 milestone Oct 21, 2023
npvalues = lib.maybe_convert_objects(result, try_float=False)
if preserve_dtype:
out = maybe_cast_pointwise_result(npvalues, obj.dtype, numeric_only=True)
if len(obj) == 0 and len(result) == 0 and isinstance(obj.dtype, ExtensionDtype):
Copy link
Member

Choose a reason for hiding this comment

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

What happens on an empty list of categoricals with observed=False? I think this is the only case where len(obj) == 0 but len(result) > 0.

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 didn't track his down specifically, but the test that was mentioned in the comment is not passing by here anymore

Copy link
Member

Choose a reason for hiding this comment

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

Running on this PR as-is:

func = "min"
dtype = "string[pyarrow_numpy]"
df = DataFrame({"a": ["a"], "b": "a", "c": "a"}, dtype=dtype)
df["a"] = pd.Categorical([1], categories=[1, 2, 3])
df = df.iloc[:0]
result = getattr(df.groupby("a", observed=False), func)()

print(result.dtypes)
# b    float64
# c    float64
# dtype: object

If you remove the condition len(result) == 0 in the line highlighted above, you get string[pyarrow_numpy] for the dtypes instead. However, the values in the DataFrame are still the float NaN. I didn't realize this was possible?

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 new string dtype uses nan as missing value representation to keep numpy semantics, so it will work if you feed it an array with all NaN

Copy link
Member

Choose a reason for hiding this comment

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

Ah - thanks

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 this still needs fixed (but okay not for 2.1.2)

@lithomas1 lithomas1 merged commit 8afd868 into pandas-dev:main Oct 26, 2023
36 of 39 checks passed
@lithomas1
Copy link
Member

thanks @phofl.

@lumberbot-app
Copy link

lumberbot-app bot commented Oct 26, 2023

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.1.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 8afd868106dec889df89e3abed36af09bb7ddf8c
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #55619: BUG: Groupby not keeping string dtype for empty objects'
  1. Push to a named branch:
git push YOURFORK 2.1.x:auto-backport-of-pr-55619-on-2.1.x
  1. Create a PR against branch 2.1.x, I would have named this PR:

"Backport PR #55619 on branch 2.1.x (BUG: Groupby not keeping string dtype for empty objects)"

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.

lithomas1 pushed a commit to lithomas1/pandas that referenced this pull request Oct 26, 2023
…5619)

* BUG: Groupby not keeping string dtype for empty objects

* Fix

---------

Co-authored-by: Thomas Li <47963215+lithomas1@users.noreply.github.com>
(cherry picked from commit 8afd868)
lithomas1 added a commit that referenced this pull request Dec 4, 2023
…type for empty objects) (#55705)

BUG: Groupby not keeping string dtype for empty objects (#55619)

* BUG: Groupby not keeping string dtype for empty objects

* Fix

---------

Co-authored-by: Thomas Li <47963215+lithomas1@users.noreply.github.com>
(cherry picked from commit 8afd868)

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
@lithomas1 lithomas1 mentioned this pull request Dec 4, 2023
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Groupby Strings String extension data type and string data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants