Skip to content

Commit

Permalink
Don't reindex an new value on setitem if the original dataframe was e…
Browse files Browse the repository at this point in the history
…mpty (#8026)

This PR resolves #8023. The pandas compatibility patch in #7948 (and the backport to 0.19 in #7957) also needs to handle the case of assigning to an empty DataFrame.

Url: #8026
  • Loading branch information
vyasr authored Apr 22, 2021
1 parent cdf7704 commit 86897fb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -7953,7 +7953,7 @@ def _setitem_with_dataframe(
"Number of Input Columns must be same replacement Dataframe"
)

if not input_df.index.equals(replace_df.index):
if len(input_df) != 0 and not input_df.index.equals(replace_df.index):
replace_df = replace_df.reindex(input_df.index)

for col_1, col_2 in zip(input_cols, replace_df.columns):
Expand Down
7 changes: 7 additions & 0 deletions python/cudf/cudf/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,13 @@ def test_dataframe_setitem_index_len1():
np.testing.assert_equal(gdf.b.to_array(), [0])


def test_empty_dataframe_setitem_df():
gdf1 = cudf.DataFrame()
gdf2 = cudf.DataFrame({"a": [1, 2, 3, 4, 5]})
gdf1["a"] = gdf2["a"]
assert_eq(gdf1, gdf2)


def test_assign():
gdf = cudf.DataFrame({"x": [1, 2, 3]})
gdf2 = gdf.assign(y=gdf.x + 1)
Expand Down

0 comments on commit 86897fb

Please sign in to comment.