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

Comments

Projects
None yet
2 participants
@njsmith

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 in 3ce416b Jul 11, 2012

@wesm

This comment has been minimized.

Show comment
Hide comment
@wesm

wesm Jul 11, 2012

Member

Thanks for the report. fixed this

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