Skip to content

np.asarray(PandasSeries), with default copy=None returns read-only array (pandas 3.0.0) #1119

Description

@NimaSarajpoor

Summary

As reported in this comment, some tests are failing.

The error shows:

# in core.preprocess_non_normalized

T[~np.isfinite(T)] = np.nan
        ^^^^^^^^^^^^^^^^^^
E       ValueError: assignment destination is read-only

This error occurs because the array T that is returned by the following line of the function core.preprocess_non_normalized is read-only when its input is pandas.Series.

T = _preprocess(T, copy)


Minimum Reproducible Example:

import pandas as pd 
import numpy as np

print(pd.__version__)  # 3.0.0
print(np.__version__)  # 2.3.5


T_B = np.array([ 584.,  -11.,   23.,   79., 1001.,    0.,  -19.])
ps = pd.Series(T_B)
T = np.asarray(ps)
T[0] = 100

# Error 
# ValueError: assignment destination is read-only

Investigation
Further investigation shows that np.shares_memory(T, ps.array) returns True for the example above, and according to read-only array section in pandas Copy-on-Write (CoW):

Accessing the underlying NumPy array of a DataFrame will return a read-only array if the array shares data with the initial DataFrame:

Activity

  1. seanlaw commented on Jan 22, 2026

    @seanlaw
    Contributor

    Whatever solution we come up with, we should create a test to check this. This is gonna be tricky.

    We'll need to confirm whether there are any instances where we set copy=False? If not, maybe we simply remove the option and always make a costly copy? I don't know

    1. In nearly all cases (maybe 100%??), copy is set to True so whether it is a pandas dataframe, numpy array, or polars dataframe, we are already making a copy in nearly all cases and it should never affect the user's original dataframe unless copy is set to False
    2. Given the point above, I think it is actually safe to set T.flags.writeable = True after the T = np.asarray(T) line because if T was not a copy then whomever called the function must have deliberately/purposely set copy=False and they must accept that their pandas dataframe can be overwritten! However, if copy=True then we've made a copy of T (even as a dataframe) and that can certainly be (safely) overwritten.
  2. seanlaw commented on Jan 22, 2026

    @seanlaw
    Contributor

    Okay, I tried this:

    T = np.asarray(T)
    T.flags.writeable = True
    

    but it failed with:

    ValueError: cannot set WRITEABLE flag to True of this array
    

    So, maybe simply check the state and make a copy as needed:

    T = np.asarray(T)
    if not T.flags.writeable:
        T = T.copy()
    

    And then we should update all of the docs that have copy as a parameter that this does NOT control the output array but, instead, only whether we make a copy of the input

  3. added a commit that references this issue on Jan 23, 2026
    ce05903
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions