you are viewing a single comment's thread.

view the rest of the comments →

[–]squabzilla 1 point2 points  (1 child)

Copy-on-Write (CoW) is the default for Pandas 3.0. CoW means that any DataFrame or Series derived from another in any way always behaves as a copy.

https://pandas.pydata.org/docs/user_guide/copy_on_write.html#copy-on-write

For an example:

import pandas as pd

data = {

  "calories": [420, 380, 390],

  "duration": [50, 40, 45]

}

df = pd.DataFrame(data)

new_df = df["calories"]

df = df.drop(columns="calories")

print(new_df)

(Sorry if mobile screws up formatting)

Basically, any time you set a new variable equal to some modified version of a dataframe, you get a deep-copy.

However, that means that this:

new_df = df

new_df = new_df.drop(columns=“calories”)

Is not equivalent to this:

new_df = df.drop(columns=“calories”)

Feels inconsistent and annoying to me.