Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataFrame.__init__(..., dtype=dt) makes unnecessary copies #1572

Closed
njsmith opened this issue Jul 6, 2012 · 1 comment
Closed

DataFrame.__init__(..., dtype=dt) makes unnecessary copies #1572

njsmith opened this issue Jul 6, 2012 · 1 comment

Comments

@njsmith
Copy link

njsmith commented Jul 6, 2012

If I have a DataFrame df with homogenous type, then DataFrame(df) returns a view on the original dataframe. DataFrame(df, dtype=current_type) should be identical; but, instead, it makes an unnecessary copy.

>>> import pandas
>>> pandas.__version__
'0.8.0'
>>> df = pandas.DataFrame([[1, 2]])
>>> df
   0  1
0  1  2
>>> df[0].dtype
dtype('int64')
>>> view = pandas.DataFrame(df)
>>> view
   0  1
0  1  2
>>> view[0][0] = 100
>>> view
     0  1
0  100  2
>>> df
     0  1
0  100  2
>>> should_be_view = pandas.DataFrame(df, dtype=df[0].dtype)
>>> should_be_view
     0  1
0  100  2
>>> should_be_view[0][0] = 99
>>> should_be_view
    0  1
0  99  2
>>> df
     0  1
0  100  2

The same thing seems to happen in the input is an ndarray -- DataFrame(arr) returns a view, DataFrame(arr, dtype=arr.dtype) returns a copy.

@wesm wesm closed this as completed in 3ce416b Jul 11, 2012
@wesm
Copy link
Member

wesm commented Jul 11, 2012

Thanks for the report. fixed this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants