4

Consider the following example.

import pandas as pd

df = pd.DataFrame({
    "x": [1, 2, 3],
    "y": [4, 5, 6]
})

x = df["x"]
df.drop(index=[0], inplace=True)

Now we have x._is_view is True, so I would expect x to be a "view" into df. In other words, I would expect x to be identical to df["x"]. However, x still contains the values [1, 2, 3], while df["x"] only contains the values [2, 3].
In what sense is x a view?

P.S. In my head, I have been imagining that df.drop(..., inplace=True) is literally dropping rows of df from memory. Perhaps this is not the case...

  • If you change the second-to-last line to `x = df` it would have the behavior you are expecting. But if you want to see really weird behavior, leave that line as-is and change the last line to `df["x"].drop(index=[0], inplace=True)`. Then you would have `x = [2,3]` and `df["x"] = [1,2,3]`. – ekrall Jan 15 '22 at 02:16

2 Answers2

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.

bb1
  • 7,174
  • 2
  • 8
  • 23
0

As mentioned in the previous answer. inplace does not guarantee it won't be copied. You can inspect the unique id of each object to proof it out.

import pandas as pd

df = pd.DataFrame({
    "x": [1, 2, 3],
    "y": [4, 5, 6]
})

x = df

print(f'Original: {id(df)}')
print(f'View:     {id(x)}')
print(f'Copy:     {id(df.drop(index=[0], inplace=True))}')

Out[1]
Original: 2570551559120
View:     2570551559120
Copy:     140708949432448
Michael Gardner
  • 1,693
  • 1
  • 11
  • 13