Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,4 @@ API Coverage

Dask-Expr covers almost everything of the Dask DataFrame API. The only missing features are:

- ``melt``
- named GroupBy Aggregations
29 changes: 29 additions & 0 deletions dask_expr/_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3988,6 +3988,35 @@ def to_html(self, max_rows=5):
layers=maybe_pluralize(n_expr, "expression"),
)

@derived_from(pd.DataFrame)
def melt(
self,
id_vars=None,
value_vars=None,
var_name=None,
value_name="value",
col_level=None,
):
meta = make_meta(
meta_nonempty(self._meta).melt(
id_vars=id_vars,
value_vars=value_vars,
var_name=var_name,
value_name=value_name,
col_level=col_level,
)
)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Probably don't need this.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you confirm this? Wouldn't want to merge it if we don't need it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Okay yeah. Default emulation works fine for the tests - Removing the unnecessary logic.

return self.map_partitions(
M.melt,
clear_divisions=True,
meta=meta,
id_vars=id_vars,
value_vars=value_vars,
var_name=var_name,
value_name=value_name,
col_level=col_level,
)

def _repr_data(self):
meta = self._meta
index = self._repr_divisions
Expand Down
27 changes: 27 additions & 0 deletions dask_expr/tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2606,3 +2606,30 @@ def test_shape_integer(df):
result = df.shape[0].compute()
assert isinstance(result, int)
assert result == 100


@pytest.mark.parametrize(
"kwargs",
[
{},
dict(id_vars="int"),
dict(value_vars="int"),
dict(value_vars=["obj", "int"], var_name="myvar"),
dict(id_vars="s1", value_vars=["obj", "int"], value_name="myval"),
dict(value_vars=["obj", "s1"]),
dict(value_vars=["s1", "s2"]),
],
)
def test_melt(kwargs):
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Test copied from dask/dask for now.

pdf = pd.DataFrame(
{
"obj": list("abcd") * 5,
"s1": list("XY") * 10,
"s2": list("abcde") * 4,
"int": np.random.randn(20),
}
)
pdf = pdf.astype({"s1": "string[pyarrow]", "s2": "string[pyarrow]"})
ddf = from_pandas(pdf, 4)

assert_eq(ddf.melt(**kwargs), pdf.melt(**kwargs), check_index=False)