Skip to content

Commit

Permalink
Merge dev into samukweku/non-equi-join-improve
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmjl authored Sep 23, 2024
2 parents cc66145 + c04dcca commit 0eaef6d
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions janitor/polars/pivot_longer.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,24 +150,27 @@ def pivot_longer_spec(
others = [
label for label in spec_columns if label not in {".name", ".value"}
]

if (len(others) == 1) & (spec.get_column(others[0]).dtype == pl.String):
# shortcut that avoids the implode/explode approach - and is faster
# if the requirements are met
# inspired by https://github.com/pola-rs/polars/pull/18519#issue-2500860927
return _pivot_longer_dot_value_string(
df=df,
index=index,
spec=spec,
variable_name=others[0],
)
variable_name = "".join(df_columns + spec_columns)
variable_name = f"{variable_name}_"
if others:
if (len(others) == 1) & (
spec.get_column(others[0]).dtype == pl.String
):
# shortcut that avoids the implode/explode approach - and is faster
# if the requirements are met
# inspired by https://github.com/pola-rs/polars/pull/18519#issue-2500860927
return _pivot_longer_dot_value_string(
df=df,
index=index,
spec=spec,
variable_name=others[0],
)
variable_name = "".join(df_columns + spec_columns)
variable_name = f"{variable_name}_"
dot_value_only = False
expression = pl.struct(others).alias(variable_name)
spec = spec.select(".name", ".value", expression)
else:
variable_name = "".join(df_columns + spec_columns)
variable_name = f"{variable_name}_"
dot_value_only = True
expression = pl.cum_count(".value").over(".value").alias(variable_name)
spec = spec.with_columns(expression)
Expand Down

0 comments on commit 0eaef6d

Please sign in to comment.