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

feat: <DataFrame>$partition_by() #898

Merged
merged 7 commits into from
Mar 11, 2024
Merged

feat: <DataFrame>$partition_by() #898

merged 7 commits into from
Mar 11, 2024

Conversation

eitsupi
Copy link
Collaborator

@eitsupi eitsupi commented Mar 5, 2024

Close #891

Perhaps rather than adding a method to GroupBy, DataFrame's partition_by is sufficient.

library(polars)

df <- readr::read_csv(I("
id,timestamp,timezone
1,2019-01-01T00:00:00Z,UTC
2,2019-01-01T00:00:00Z,Asia/Tokyo
3,2019-01-01T20:00:00Z,UTC
4,2019-01-01T20:00:00Z,Asia/Tokyo
"), show_col_types = FALSE)

as_polars_df(df)$partition_by(
  "timezone",
  maintain_order = FALSE, as_nested_list = TRUE
) |>
  lapply(
    \(l) l$data$with_columns(
      naive_time = pl$col("timestamp")$dt$convert_time_zone(
        l$key$timezone
      )$dt$replace_time_zone(NULL)
    )
  ) |>
  pl$concat()
#> shape: (4, 4)
#> ┌─────┬─────────────────────────┬────────────┬─────────────────────┐
#> │ id  ┆ timestamp               ┆ timezone   ┆ naive_time          │
#> │ --- ┆ ---                     ┆ ---        ┆ ---                 │
#> │ f64 ┆ datetime[ms, UTC]       ┆ str        ┆ datetime[ms]        │
#> ╞═════╪═════════════════════════╪════════════╪═════════════════════╡
#> │ 2.0 ┆ 2019-01-01 00:00:00 UTC ┆ Asia/Tokyo ┆ 2019-01-01 09:00:00 │
#> │ 4.0 ┆ 2019-01-01 20:00:00 UTC ┆ Asia/Tokyo ┆ 2019-01-02 05:00:00 │
#> │ 1.0 ┆ 2019-01-01 00:00:00 UTC ┆ UTC        ┆ 2019-01-01 00:00:00 │
#> │ 3.0 ┆ 2019-01-01 20:00:00 UTC ┆ UTC        ┆ 2019-01-01 20:00:00 │
#> └─────┴─────────────────────────┴────────────┴─────────────────────┘

Created on 2024-03-09 with reprex v2.1.0

@eitsupi eitsupi force-pushed the partition-by branch 2 times, most recently from fa79a0b to bb344c8 Compare March 9, 2024 15:57
@eitsupi eitsupi requested a review from etiennebacher March 9, 2024 15:58
R/dataframe__frame.R Show resolved Hide resolved
...,
maintain_order = TRUE,
include_key = TRUE,
as_nested_list = FALSE) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is this name appropriate?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes I think it is the best equivalent to Python's dict

@eitsupi eitsupi marked this pull request as ready for review March 9, 2024 16:00
@eitsupi eitsupi changed the title WIP feat: <DataFrame>$partition_by() [skip ci] feat: <DataFrame>$partition_by() Mar 9, 2024
Copy link
Collaborator

@etiennebacher etiennebacher left a comment

Choose a reason for hiding this comment

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

Thanks, some comments. Can you also bump NEWS?

R/dataframe__frame.R Outdated Show resolved Hide resolved
R/dataframe__frame.R Outdated Show resolved Hide resolved
R/dataframe__frame.R Outdated Show resolved Hide resolved
R/dataframe__frame.R Outdated Show resolved Hide resolved
...,
maintain_order = TRUE,
include_key = TRUE,
as_nested_list = FALSE) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes I think it is the best equivalent to Python's dict

R/dataframe__frame.R Show resolved Hide resolved
R/dataframe__frame.R Outdated Show resolved Hide resolved
Comment on lines +84 to +91
#' Convert dots to a character vector of column names
#' @param .df [RPolarsDataFrame]
#' @param ... Arguments to pass to [`pl$col()`][pl_col]
#' @noRd
dots_to_colnames = function(.df, ..., .call = sys.call(1L)) {
result(pl$DataFrame(schema = .df$schema)$select(pl$col(...))$columns) |>
unwrap(call = .call)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm surprised this wasn't needed before , I think DataFrame$drop() should have a similar input as DataFrame$partition_by() based on the py-polars API

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@etiennebacher Could you update $drop()?

@eitsupi
Copy link
Collaborator Author

eitsupi commented Mar 10, 2024

@etiennebacher Thanks for your review.
I think I have addressed your comments.
(Can't merge because it hasn't been approved/you can approve with minor comments instead of requiring changes to reduce everyone's workload)

@eitsupi eitsupi added this to the 0.15 milestone Mar 11, 2024
@eitsupi eitsupi mentioned this pull request Mar 11, 2024
@etiennebacher etiennebacher merged commit a4c9a9f into main Mar 11, 2024
34 checks passed
@etiennebacher etiennebacher deleted the partition-by branch March 11, 2024 11:42
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.

<GroupBy>$to_list() or something similar
2 participants