Skip to content

BUG?: .sample() sometimes returning a view not a copy.  #10736

Closed
@nickeubank

Description

@nickeubank

I've realized the .sample() function is sometimes returning a view rather than a copy. This will cause the "setting on a view/copy" error:

df = pd.DataFrame({'col1':[5,6,7], 'col2':['a','b','c']})
a_sample = df.sample(1)
a_sample['col1'] = 'z'

I believe the behavior is the result of it using the .take() function, which is what the .sample() command uses to pull the sample the passed frame.

I'd like to suggest either:

  • replace .take() with .iloc()
  • change .take() to .take().copy() if that's faster (@shoyer suggested .take() would be a faster -- is .take().copy()?)

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions