Skip to content

Commit

Permalink
parse names only
Browse files Browse the repository at this point in the history
  • Loading branch information
FBruzzesi committed Feb 2, 2025
1 parent eac8ca2 commit 060da1d
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 33 deletions.
19 changes: 14 additions & 5 deletions narwhals/_arrow/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,15 +764,24 @@ def sample(

def unpivot(
self: Self,
on: list[str] | None,
index: list[str],
on: str | list[str] | None,
index: str | list[str] | None,
variable_name: str,
value_name: str,
) -> Self:
native_frame = self._native_frame
n_rows = len(self)

on_ = [c for c in self.columns if c not in index] if on is None else on
index_: list[str] = (
[] if index is None else [index] if isinstance(index, str) else index
)
on_: list[str] = (
[c for c in self.columns if c not in index_]
if on is None
else [on]
if isinstance(on, str)
else on
)

promote_kwargs = (
{"promote_options": "permissive"}
Expand All @@ -784,11 +793,11 @@ def unpivot(
[
pa.Table.from_arrays(
[
*[native_frame.column(idx_col) for idx_col in index],
*[native_frame.column(idx_col) for idx_col in index_],
pa.array([on_col] * n_rows, pa.string()),
native_frame.column(on_col),
],
names=[*index, variable_name, value_name],
names=[*index_, variable_name, value_name],
)
for on_col in on_
],
Expand Down
4 changes: 2 additions & 2 deletions narwhals/_dask/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,8 @@ def gather_every(self: Self, n: int, offset: int) -> Self:

def unpivot(
self: Self,
on: list[str] | None,
index: list[str],
on: str | list[str] | None,
index: str | list[str] | None,
variable_name: str,
value_name: str,
) -> Self:
Expand Down
19 changes: 14 additions & 5 deletions narwhals/_duckdb/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,22 @@ def drop_nulls(self: Self, subset: list[str] | None) -> Self:

def unpivot(
self: Self,
on: list[str] | None,
index: list[str],
on: str | list[str] | None,
index: str | list[str] | None,
variable_name: str,
value_name: str,
) -> Self:
index_: list[str] = (
[] if index is None else [index] if isinstance(index, str) else index
)
on_: list[str] = (
[c for c in self.columns if c not in index_]
if on is None
else [on]
if isinstance(on, str)
else on
)

if variable_name == "":
msg = "`variable_name` cannot be empty string for duckdb backend."
raise NotImplementedError(msg)
Expand All @@ -442,10 +453,8 @@ def unpivot(
msg = "`value_name` cannot be empty string for duckdb backend."
raise NotImplementedError(msg)

on_ = [c for c in self.columns if c not in index] if on is None else on

cols_to_select = ", ".join(
f'"{col}"' for col in [*index, variable_name, value_name]
f'"{col}"' for col in [*index_, variable_name, value_name]
)
unpivot_on = ", ".join(f'"{col}"' for col in on_)

Expand Down
8 changes: 4 additions & 4 deletions narwhals/_pandas_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,17 +1057,17 @@ def sample(

def unpivot(
self: Self,
on: list[str] | None,
index: list[str],
on: str | list[str] | None,
index: str | list[str] | None,
variable_name: str,
value_name: str,
) -> Self:
return self._from_native_frame(
self._native_frame.melt(
id_vars=index,
value_vars=on,
var_name=variable_name if variable_name is not None else "variable",
value_name=value_name if value_name is not None else "value",
var_name=variable_name,
value_name=value_name,
)
)

Expand Down
8 changes: 4 additions & 4 deletions narwhals/_polars/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ def drop(self: Self, columns: list[str], strict: bool) -> Self: # noqa: FBT001

def unpivot(
self: Self,
on: list[str] | None,
index: list[str],
on: str | list[str] | None,
index: str | list[str] | None,
variable_name: str,
value_name: str,
) -> Self:
Expand Down Expand Up @@ -506,8 +506,8 @@ def drop(self: Self, columns: list[str], strict: bool) -> Self: # noqa: FBT001

def unpivot(
self: Self,
on: list[str] | None,
index: list[str],
on: str | list[str] | None,
index: str | list[str] | None,
variable_name: str,
value_name: str,
) -> Self:
Expand Down
4 changes: 2 additions & 2 deletions narwhals/_spark_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ def explode(self: Self, columns: str | Sequence[str], *more_columns: str) -> Sel

def unpivot(
self: Self,
on: list[str] | None,
index: list[str],
on: str | list[str] | None,
index: str | list[str] | None,
variable_name: str,
value_name: str,
) -> Self:
Expand Down
13 changes: 2 additions & 11 deletions narwhals/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,22 +330,13 @@ def unpivot(
variable_name: str | None,
value_name: str | None,
) -> Self:
on_ = [on] if isinstance(on, str) else on
index_ = (
[index]
if isinstance(index, str)
else index
if isinstance(index, list)
else []
)

variable_name = variable_name if variable_name is not None else "variable"
value_name = value_name if value_name is not None else "value"

return self._from_compliant_dataframe(
self._compliant_frame.unpivot(
on=on_,
index=index_,
on=on,
index=index,
variable_name=variable_name,
value_name=value_name,
)
Expand Down

0 comments on commit 060da1d

Please sign in to comment.