'What is the difference between `pandas.Series.ravel()`, `pandas.Series.to_numpy()`, `pandas.Series.values` and `pandas.Series.array`?
Basically the title sums it up. I have created a dummy pandas.Series
object and looked up all these properties and methods. Documentation states that all of them except maybe pandas.array
return numpy.ndarray
objects. What is the difference then and when should I use one method or attribute or another? I've read that for example there is some difference between numpy.flatten()
and numpy.ravel()
, the former returns new array and the latter returns the view. It is the same with pandas
?
import pandas as pd
s = pd.Series(range(10))
s.ravel() # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
s.to_numpy() # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
s.values # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
s.array # <PandasArray>
Solution 1:[1]
By default, all of these return a view:
import pandas as pd
s = pd.Series(range(10))
rav = s.ravel() # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
to_num = s.to_numpy() # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
values = s.values # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
array = s.array # <PandasArray>
rav[0] = 1
print('ravel', s[0])
to_num[0] = 2
print('to_numpy', s[0])
values[0] = 3
print('values', s[0])
array[0] = 4
print('array', s[0])
ravel 1
to_numpy 2
values 3
array 4
However, to_numpy()
has the copy
parameter (default False
), with which you can return a full copy of the underlying array:
to_num_copy = s.to_numpy(copy=True) # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
to_num_copy[0] = 5
print('to_numpy with copy', s[0])
to_numpy with copy 4
Finally, to_numpy()
may sometimes also return a copy when the dtype
of the column is not homogeneous and/or not a numpy dtype
. Instead, .array
should be used if we want to strictly avoid a copy.
Note that .values
is considered old an instead .array
or to_numpy()
should be used. More info in this answer.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |