Skip to content

BUG: The parameter “as_index=False” is not valid when the source DataFrame is empty #48135

@nanqiangyipo

Description

@nanqiangyipo

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

df = pd.DataFrame([[4, 9]] * 3, columns=['A', 'B'])
res1 = df.groupby('A', as_index=False).apply(lambda x: x)
print("result of index name attribute with data", res1.index.names)

df = pd.DataFrame([], columns=['A', 'B'])
res2 = df.groupby('A', as_index=False).apply(lambda x: x)
print("result of index name attribute with empty data", res2.index.names)
print(f"pandas version :{pd.__version__}")

Issue Description

The parameter “as_index=False” is not valid when the source DataFrame is empty.
examples:
result of index name attribute with data : [None]
result of index name attribute with empty data : ['A']
pandas version :1.3.4

result of index name attribute with data : [None]
result of index name attribute with empty data : ['A']
pandas version :1.4.3

Expected Behavior

when set “as_index=False” ,the names of index attribute of the result should be [None]

Installed Versions

Details

INSTALLED VERSIONS

commit : 945c9ed
python : 3.8.8.final.0
python-bits : 64
OS : Darwin
OS-release : 21.6.0
Version : Darwin Kernel Version 21.6.0: Sat Jun 18 17:07:25 PDT 2022; root:xnu-8020.140.41~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : zh_CN.UTF-8
LANG : zh_CN.UTF-8
LOCALE : zh_CN.UTF-8
pandas : 1.3.4
numpy : 1.20.1
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 52.0.0.post20210125
Cython : 0.29.23
pytest : 6.2.3
hypothesis : None
sphinx : 4.0.1
blosc : None
feather : None
xlsxwriter : 1.3.8
lxml.etree : 4.6.3
html5lib : 1.1
pymysql : 1.0.2
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.22.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 0.9.0
fastparquet : None
gcsfs : None
matplotlib : 3.3.4
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.2
sqlalchemy : 1.3.24
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : 1.3.0
numba : 0.53.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    ApplyApply, Aggregate, Transform, MapBugGroupby

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions