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 ability to specify prefix for unnest #16068

Closed
david-waterworth opened this issue May 6, 2024 · 2 comments
Closed

Add ability to specify prefix for unnest #16068

david-waterworth opened this issue May 6, 2024 · 2 comments
Labels
enhancement New feature or an improvement of an existing feature

Comments

@david-waterworth
Copy link

david-waterworth commented May 6, 2024

Description

I find myself regularly un-nesting structures such as

{
    equipment: { name=XXX, level=YYY, zone=ZZZ, ...}
    sensor: {name=ABC, level=123, ... }
}

i.e. the names of the exploded columns for equipment and sensor clash. This requires code like

.unnest("equipment").rename({"name":"equipment_name", "level":"equipment_level" ... })
.unnest("sensor").rename({"name":"sensor_name", "level":"sensor_level" ... })

It would be nice if rename had arguments to generate the names. The simplest option would be a prefix, i.e.

.unnest("equipment", prefix="equipment_").unnest("sensor", prefix="sensor_")

Another option would be to infer the prefix from the base column name, i.e.

.unnest("equipment", add_prefix=True).unnest("sensor", add_prefix=True)

'

would add the prefix based on the first argument etc.

@david-waterworth david-waterworth added the enhancement New feature or an improvement of an existing feature label May 6, 2024
@cmdlineluser
Copy link
Contributor

Some struct methods were recently added to .name which could help.

df.with_columns(
   pl.col("equipment").name.prefix_fields("equipment_"),
   pl.col("sensor").name.prefix_fields("sensor_")
).unnest("equipment", "sensor")

Also related: #12353

@david-waterworth
Copy link
Author

Thanks @cmdlineluser that works for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants