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

Add keep: :none argument to mutate functions #966

Merged
merged 6 commits into from
Aug 19, 2024

Conversation

brendon9x
Copy link
Contributor

This PR adds a new keep option to the mutate and mutate_with functions in the DataFrame module. This option allows users to control which columns are retained in the output DataFrame after a mutation operation. Closes #965

Examples of usage

Regular DataFrame:

df = Explorer.DataFrame.new(a: ["a", "b", "c"], b: [1, 2, 3])
Explorer.DataFrame.mutate(df, [c: b + 1], keep: :none)
#Explorer.DataFrame<
  Polars[3 x 1]
  c s64 [2, 3, 4]
>

Grouped DataFrame:

df = Explorer.Datasets.iris()
grouped = Explorer.DataFrame.group_by(df, "species")
Explorer.DataFrame.mutate(grouped, [petal_length_avg: mean(petal_length)], keep: :none)
#Explorer.DataFrame<
  Polars[150 x 2]
  Groups: ["species"]
  species string ["Iris-setosa", "Iris-setosa", "Iris-setosa", ...]
  petal_length_avg f64 [1.464, 1.464, 1.464, ...]
>

Considerations

  1. I decided to use a keyword argument (:all or :none) instead of a boolean for maximum forwards compatibility.
  2. I was going to add unit tests, but the doctests seemed sufficient to cover the new functionality and its interactions with existing features.

Copy link
Member

@cigrainger cigrainger left a comment

Choose a reason for hiding this comment

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

I'm happy with this. IIUC it's 100% opt-in (doesn't change the current API) and provides a nice bit of sugar to avoid an additional select after mutate.

lib/explorer/data_frame.ex Outdated Show resolved Hide resolved
Co-authored-by: Christopher Grainger <[email protected]>
@josevalim josevalim merged commit fde3b22 into elixir-explorer:main Aug 19, 2024
3 checks passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

@brendon9x
Copy link
Contributor Author

@cigrainger @josevalim – this repo was a joy to work in. The contributor docs were great and the mix tasks worked flawlessly (and tests are super fast). I wouldn't have been able to push this over the line if any of that wasn't true. Thanks for the awesome project.

@brendon9x brendon9x deleted the add-keep-option-to-mutate branch August 19, 2024 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support dplyr-like keep argument for mutate functions
3 participants