Skip to content

Commit

Permalink
FIX-modin-project#6773: make sure '_to_pandas' return mutable pandas …
Browse files Browse the repository at this point in the history
…objects

Signed-off-by: Anatoly Myachev <anatoly.myachev@intel.com>
  • Loading branch information
anmyachev committed Nov 28, 2023
1 parent 552cad8 commit 30e4c19
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions modin/core/dataframe/pandas/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ def concatenate(dfs):
i, pandas.Categorical(df.iloc[:, i], categories=union.categories)
)
# `ValueError: buffer source array is read-only` if copy==False
if len(dfs) == 1:
# concat doesn't make a copy if len(dfs) == 1,
# so do it explicitly
return dfs[0].copy()
return pandas.concat(dfs, copy=True)
30 changes: 30 additions & 0 deletions modin/pandas/test/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,36 @@ def test_to_pandas_indices(data):
), f"Levels of indices at axis {axis} are different!"


def test_to_pandas_read_only_issue():
df = pd.DataFrame(
[
[np.nan, 2, np.nan, 0],
[3, 4, np.nan, 1],
[np.nan, np.nan, np.nan, np.nan],
[np.nan, 3, np.nan, 4],
],
columns=list("ABCD"),
)
pdf = df._to_pandas()
# there shouldn't be `ValueError: putmask: output array is read-only`
pdf.fillna(0, inplace=True)


def test_to_numpy_read_only_issue():
df = pd.DataFrame(
[
[np.nan, 2, np.nan, 0],
[3, 4, np.nan, 1],
[np.nan, np.nan, np.nan, np.nan],
[np.nan, 3, np.nan, 4],
],
columns=list("ABCD"),
)
arr = df.to_numpy()
# there shouldn't be `ValueError: putmask: output array is read-only`
np.putmask(arr, np.isnan(arr), 0)


def test_create_categorical_dataframe_with_duplicate_column_name():
# This tests for https://github.com/modin-project/modin/issues/4312
pd_df = pandas.DataFrame(
Expand Down

0 comments on commit 30e4c19

Please sign in to comment.