BUG: pd.apply returns a dataframe when empty dataframe instead of a series · Issue #39111 · pandas-dev/pandas (original) (raw)


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd pd.DataFrame({'a': [1,2], 'b': [3, 0]}) a b 0 1 3 1 2 0 df = pd.DataFrame({'a': [1,2], 'b': [3, 0]}) df a b 0 1 3 1 2 0 df.apply(lambda x: max(x['a'], x['b']), axis = 1) 0 3 1 2 dtype: int64 df.head(0).apply(lambda x: max(x['a'], x['b']), axis = 1) Empty DataFrame Columns: [a, b] Index: []

Problem description

When running .apply() to a dataframe, I'm expecting to get a pd.Series back to add as an additional column. If the dataframe is empty and I run .apply() on it, it will return a dataframe and I cannot add it as a column

Expected Output

Output of pd.show_versions()

pd.show_versions()

INSTALLED VERSIONS

commit : d9fff2792bf16178d4e450fe7384244e50635733 python : 3.8.5.final.0 python-bits : 64 OS : Darwin OS-release : 20.2.0 Version : Darwin Kernel Version 20.2.0: Wed Dec 2 20:39:59 PST 2020; root:xnu-7195.60.75~1/RELEASE_X86_64 machine : x86_64 processor : i386 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 1.1.0 numpy : 1.19.1 pytz : 2020.5 dateutil : 2.8.1 pip : 20.2.2 setuptools : 49.6.0.post20201009 Cython : None pytest : 6.2.1 hypothesis : None sphinx : None blosc : None feather : 0.4.1 xlsxwriter : 1.3.7 lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : 2.11.2 IPython : 7.19.0 pandas_datareader: None bs4 : None bottleneck : None fsspec : 0.8.5 fastparquet : None gcsfs : None matplotlib : 3.3.0 numexpr : None odfpy : None openpyxl : 3.0.5 pandas_gbq : None pyarrow : 2.0.0 pytables : None pyxlsb : None s3fs : 0.5.1 scipy : 1.5.2 sqlalchemy : 1.3.19 tables : None tabulate : 0.8.7 xarray : None xlrd : 1.2.0 xlwt : None numba : 0.50.1

[paste the output of pd.show_versions() here leaving a blank line after the details tag]