Let me check it out.
Firstly, with pd.Series objects.
In [750]: s = pd.Series([1, 2, 3]) In [751]: v = s.values In [752]: v[0] = 10000 In [753]: s Out[753]: 0 10000 1 2 2 3 dtype: int64
Now for DataFrame objects. First, consider non-mixed dtypes -
In [780]: df = pd.DataFrame(1 - np.eye(3, dtype=int)) In [781]: df Out[781]: 0 1 2 0 0 1 1 1 1 0 1 2 1 1 0 In [782]: v = df.values In [783]: v[0] = 12345 In [784]: df Out[784]: 0 1 2 0 12345 12345 12345 1 1 0 1 2 1 1 0
Modifications are made, therefore .values means return the view.
Now consider a mixed dtypes -
In [755]: df = pd.DataFrame({'A' :[1, 2], 'B' : ['ccc', 'ddd']}) In [756]: df Out[756]: AB 0 1 ccc 1 2 ddd In [757]: v = df.values In [758]: v[0] = 123 In [759]: v[0, 1] = 'zzxxx' In [760]: df Out[760]: AB 0 1 ccc 1 2 ddd
Here .values returns a copy.
Observation
.values for Series returns a view regardless of the type of each row, whereas for DataFrames it depends. For homogeneous types, the view is returned. Otherwise, a copy.
source share