This pandas series is a view... into what? [duplicate]

Solution 1:

inplace does not guarantee that the dataframe will be modified in place. In this case, as in many cases, it creates a copy and reassigns it to df. See the discussion of inplace here.

As for x being a view, if you execute x.values.base you will get:

array([[1, 2, 3],
       [4, 5, 6]])

Thus, x is a view of the original dataframe, which is not assigned to df anymore.

Here is another way to verify it:

import pandas as pd
import numpy as np

df = pd.DataFrame({
    "x": [1, 2, 3],
    "y": [4, 5, 6]
})
df_arr = df.values # numpy array underlying df
x = df["x"]
df.drop(index=[0], inplace=True)

Now if you run

np.shares_memory(df_arr, x.values)

the result is True, since x occupies the same space in the memory as df originally did. On the other hand

np.shares_memory(df_arr, df.values)

returns False, because values of df now reside somewhere else.