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

Unexpected interaction in DataFrame.apply(f) when `f` returns a list #18919

Closed
gwerbin opened this Issue Dec 23, 2017 · 1 comment

Comments

Projects
None yet
3 participants
@gwerbin

gwerbin commented Dec 23, 2017

Code Sample, a copy-pastable example if possible

df = pd.DataFrame({'x': pd.Series([['a', 'b'], ['q']]),
                   'y': pd.Series([['z'], ['q', 't']])})
df.index = pd.MultiIndex.from_tuples([('i0', 'j0'), ('i1', 'j1')])

df.apply(lambda row: [el for el in row['x'] if el in row['y']], axis=1)

Problem description

When a DataFrame has a MultiIndex, and the function passed to DataFrame.apply returns all lists, weird stuff happens and an unintelligible error occurs.

What ends up happening is that the result somehow gets coerced to a list of arrays (not sure where or why the list->array conversion happens), and then submitted to DataFrame.__init__, which tries to massage it that into a DataFrame, and fails.

Resulting error: ValueError: Empty data passed with indices specified., emitted from deep within the bowels of pandas/core/internals.py, specifically create_block_manager_from_arrays.

This happens regardless of what the reduce= argument is set to.

Expected Output

Don't try to manipulate the output. Return a Series of lists.

In the example above, that'd be:

pd.Series([[], ['q']], index=df.index)

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.3.final.0
python-bits: 64
OS: Linux
OS-release: 3.10.0-514.26.2.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.21.1
pytest: None
pip: 9.0.1
setuptools: 36.6.0
Cython: None
numpy: 1.12.1
scipy: 0.19.1
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.9999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
@jreback

This comment has been minimized.

Contributor

jreback commented Dec 23, 2017

duplicate of many issues, se #18557 which will close.

@jreback jreback closed this Dec 23, 2017

@jreback jreback added this to the No action milestone Dec 23, 2017

@jreback jreback added the Duplicate label Dec 23, 2017

jreback added a commit to jreback/pandas that referenced this issue Jan 6, 2018

API/BUG: .apply will correctly infer output shape when axis=1
closes #16353
closes #17348
closes #17437
closes #18573
closes #17970
closes #17892
closes #17602
closes #18775
closes #18901
closes #18919

@jorisvandenbossche jorisvandenbossche modified the milestones: No action, 0.23.0 Jan 28, 2018

jreback added a commit to jreback/pandas that referenced this issue Feb 5, 2018

API/BUG: .apply will correctly infer output shape when axis=1
closes #16353
closes #17348
closes #17437
closes #18573
closes #17970
closes #17892
closes #17602
closes #18775
closes #18901
closes #18919

jorisvandenbossche added a commit that referenced this issue Feb 7, 2018

harisbal pushed a commit to harisbal/pandas that referenced this issue Feb 28, 2018

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