diff --git a/modin/core/dataframe/pandas/utils.py b/modin/core/dataframe/pandas/utils.py index bb8d018e6e2..c1703d6f2db 100644 --- a/modin/core/dataframe/pandas/utils.py +++ b/modin/core/dataframe/pandas/utils.py @@ -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) diff --git a/modin/pandas/test/test_general.py b/modin/pandas/test/test_general.py index 2f6f331f4d4..2fec3371ca9 100644 --- a/modin/pandas/test/test_general.py +++ b/modin/pandas/test/test_general.py @@ -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(