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

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