Skip to content

deepcopy failure on empty dataframes with non-empty column set (numpy 1.12 compatibility) #15370

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

Closed
bmcfee opened this issue Feb 11, 2017 · 3 comments
Labels
Compat pandas objects compatability with Numpy or Python functions Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@bmcfee
Copy link
Contributor

bmcfee commented Feb 11, 2017

Code Sample, a copy-pastable example if possible

# Your code here
import pandas as pd
import copy

# This one works
df = pd.DataFrame(data=[], index=[])
df_copy = copy.deepcopy(df)

# This one fails
df2 = pd.DataFrame(data=[], index=[], columns=['a'])
df2_copy = copy.deepcopy(df2)

Problem description

This only occurs with numpy 1.12 (and, presumably above): when deepcopying an empty dataframe with a non-empty column set, it fails with the following:

ValueError: Iteration of zero-sized operands is not enabled

If the column set is also empty, everything works as expected.

On older numpy versions (1.11), it also works as expected.

Expected Output

Not failing.

Output of pd.show_versions()

# Paste the output here pd.show_versions() here INSTALLED VERSIONS ------------------ commit: None python: 3.4.0.final.0 python-bits: 64 OS: Linux OS-release: 4.4.0-62-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

pandas: 0.19.2
nose: 1.3.7
pip: 9.0.1
setuptools: 32.3.1.post20170108
Cython: 0.25.2
numpy: 1.12.0
scipy: 0.18.1
statsmodels: None
xarray: None
IPython: 5.1.0
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 2.0.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
boto: None
pandas_datareader: None

@jreback
Copy link
Contributor

jreback commented Feb 11, 2017

not shocked this doesn't work, see #8571

we are not using an override (which we should be), to simply call our custom methods.

e.g. basically

def __copy__(self):
    return self.copy()

if you'd like to take a crack at this issue (and add this as a test) would be appreciated!

@jreback jreback added Compat pandas objects compatability with Numpy or Python functions Difficulty Novice Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Feb 11, 2017
@jreback jreback added this to the 0.20.0 milestone Feb 11, 2017
@bmcfee
Copy link
Contributor Author

bmcfee commented Feb 13, 2017

if you'd like to take a crack at this issue (and add this as a test) would be appreciated!

Not sure I know how best to fix it, as I haven't mucked around the internals of dataframes much. It's not obvious to me that this should be fixed in dataframe, ndframe, or somewhere else?

@jreback
Copy link
Contributor

jreback commented Feb 14, 2017

@bmcfee simply need to define __copy__ and __deepcopy__, can be done in pandas/core/generic/NDFrame. These are already defined for Index (in pandas/indexes/base.py (it may simply be possible to move those to pandas/core/base though as we have a mixin that goes thru the entire hierarchy PandasObject.

bmcfee added a commit to bmcfee/pandas that referenced this issue Feb 17, 2017
jreback pushed a commit to jreback/pandas that referenced this issue Feb 20, 2017
AnkurDedania pushed a commit to AnkurDedania/pandas that referenced this issue Mar 21, 2017
closes pandas-dev#15370

Author: Brian McFee <[email protected]>
Author: Jeff Reback <[email protected]>

Closes pandas-dev#15444 from bmcfee/deepcopy-ndframe and squashes the following commits:

bf36f35 [Jeff Reback] TST: skip the panel4d deepcopy tests
d58b1f6 [Brian McFee] added tests for copy and deepcopy
35f3e0f [Brian McFee] relocated Index.__deepcopy__ to live near __copy__
1aea940 [Brian McFee] switched deepcopy test to using generic comparator
7e67e7d [Brian McFee] ndframe and index __copy__ are now proper methods
820664c [Brian McFee] moved deepcopy test to generic.py
9721041 [Brian McFee] added copy/deepcopy to ndframe, fixes pandas-dev#15370
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compat pandas objects compatability with Numpy or Python functions Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants