Skip to content

DOC: "Copy On Write: Patterns to avoid" confusion #56622

Closed
@avm19

Description

@avm19

Pandas version checks

  • I have checked that the issue still exists on the latest versions of the docs on main here

Location of the documentation

https://pandas.pydata.org/docs/dev/user_guide/copy_on_write.html#patterns-to-avoid

Documentation problem

This code snippet

In [59]: df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
In [60]: df2 = df.reset_index()
In [61]: df2.iloc[0, 0] = 100

is intended to illustrate that a copy (on write) will be created, because df and df2 reference the same data. In reality, no copy will be created because df2.iloc[0, 0] refers to the newly created integer column storing the reset index, which exists for df2, but not for df.

Suggested fix for documentation

Replace the last line of the snippet,

In [61]: df2.iloc[0, 0] = 100

with:

In [61]: df2.iloc[0, 1] = 100

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions