Skip to content

ENH: Add new implementation of DataFrame.stack #53921

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

Merged
merged 15 commits into from
Aug 2, 2023

Conversation

rhshadrach
Copy link
Member

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

We can either deprecate the current implementation in 2.1, or wait until 2.2. It's slightly more gradual to wait until 2.2; users in 2.1 could opt into the new behavior without any warnings.

@rhshadrach rhshadrach added Enhancement Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Jun 29, 2023
@jorisvandenbossche
Copy link
Member

How do you see the deprecation/update process? If we don't yet warn in 2.1, I doubt that many users will already try out v3=True. And for warning, do you think it would be possible to detect when to warn? Or were you planning to warn for all cases to say that people should specify v3=True to get rid of the warning? (ideally we would only warn if v3 would actually give a different result, otherwise it will be a noisy annoying warning for those (the majority?) use cases that don't need it)

@jbrockmendel
Copy link
Member

Instead of a v3 keyword could use the "future" option from #53025

@rhshadrach
Copy link
Member Author

rhshadrach commented Jun 29, 2023

@jorisvandenbossche

How do you see the deprecation/update process? If we don't yet warn in 2.1, I doubt that many users will already try out v3=True. And for warning, do you think it would be possible to detect when to warn?

I was thinking we add the deprecation in 2.2; I agree not many will use v3=True in 2.1, but I don't see any negatives of waiting until 2.2 to deprecate.

For doing the deprecation, we could get really complicated. I think behavior is the same when:

  • stacking where columns are a non-MultiIndex;
  • stacking where columns are a MultiIndex and every combination between stacked and non-stacked levels occurs and dropna=False;
  • stacking where columns are a MultiIndex and every combination between stacked and non-stacked levels occurs and dropna=True and the input does not have any NAs; or
  • stacking where columns are a MultiIndex, dtypes are all float, dropna=True, and the input does not have NaN values

However, since we'd be deprecating dropna we'd still need to warn in the 2nd bullet above.

@jbrockmendel

Instead of a v3 keyword could use the "future" option from #53025

I'd definitely be on board with this.

@rhshadrach rhshadrach marked this pull request as ready for review July 13, 2023 21:22
@rhshadrach
Copy link
Member Author

@jbrockmendel @jorisvandenbossche @mroeschke assuming the CI agrees I think this is ready for review.

@jbrockmendel - regarding using a future.* option here, I think I'd prefer the additional argument since this is just on a single method. But if we're wanting something like "all opting into future behaviors is done via future.* options", I'd be on board with that and happy to rework that here. Thoughts?

@jbrockmendel
Copy link
Member

But if we're wanting something like "all opting into future behaviors is done via future.* options"

Definitely not

@@ -582,7 +587,7 @@ def stack(frame: DataFrame, level=-1, dropna: bool = True, sort: bool = True):
stacked : Series or DataFrame
"""

def factorize(index):
def stack_factorize(index):
Copy link
Member Author

Choose a reason for hiding this comment

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

linter didn't like that this was named factorize now that we import factorize as well, and rightfully so.

@rhshadrach
Copy link
Member Author

Thanks @mroeschke - I think this is good for another look.

Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

Looks pretty good. Just a code check failure

@mroeschke mroeschke added this to the 2.1 milestone Aug 2, 2023
@mroeschke mroeschke merged commit 263828c into pandas-dev:main Aug 2, 2023
@mroeschke
Copy link
Member

Awesome, thanks @rhshadrach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants