Skip to content

Commit

Permalink
Support callables in DataFrame.assign (#14142)
Browse files Browse the repository at this point in the history
While here, change the way the initial copied frame is constructed:
callables are allowed to refer to columns already in the dataframe,
even if they overwrite them.

- Closes #12936

Authors:
  - Lawrence Mitchell (https://github.com/wence-)

Approvers:
  - Matthew Roeschke (https://github.com/mroeschke)

URL: #14142
  • Loading branch information
wence- authored Sep 22, 2023
1 parent f865c87 commit a6d014e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
23 changes: 14 additions & 9 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1390,10 +1390,21 @@ def _get_numeric_data(self):
return self[columns]

@_cudf_nvtx_annotate
def assign(self, **kwargs):
def assign(self, **kwargs: Union[Callable[[Self], Any], Any]):
"""
Assign columns to DataFrame from keyword arguments.
Parameters
----------
**kwargs: dict mapping string column names to values
The value for each key can either be a literal column (or
something that can be converted to a column), or
a callable of one argument that will be given the
dataframe as an argument and should return the new column
(without modifying the input argument).
Columns are added in-order, so callables can refer to
column names constructed in the assignment.
Examples
--------
>>> import cudf
Expand All @@ -1405,15 +1416,9 @@ def assign(self, **kwargs):
1 1 4
2 2 5
"""
new_df = cudf.DataFrame(index=self.index.copy())
for name, col in self._data.items():
if name in kwargs:
new_df[name] = kwargs.pop(name)
else:
new_df._data[name] = col.copy()

new_df = self.copy(deep=False)
for k, v in kwargs.items():
new_df[k] = v
new_df[k] = v(new_df) if callable(v) else v
return new_df

@classmethod
Expand Down
19 changes: 19 additions & 0 deletions python/cudf/cudf/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,25 @@ def test_assign():
np.testing.assert_equal(gdf2.y.to_numpy(), [2, 3, 4])


@pytest.mark.parametrize(
"mapping",
[
{"y": 1, "z": lambda df: df["x"] + df["y"]},
{
"x": lambda df: df["x"] * 2,
"y": lambda df: 2,
"z": lambda df: df["x"] / df["y"],
},
],
)
def test_assign_callable(mapping):
df = pd.DataFrame({"x": [1, 2, 3]})
cdf = cudf.from_pandas(df)
expect = df.assign(**mapping)
actual = cdf.assign(**mapping)
assert_eq(expect, actual)


@pytest.mark.parametrize("nrows", [1, 8, 100, 1000])
@pytest.mark.parametrize("method", ["murmur3", "md5"])
@pytest.mark.parametrize("seed", [None, 42])
Expand Down

0 comments on commit a6d014e

Please sign in to comment.