Skip to content

BUG: Misleading error message when aggregating duplicate columns #55964

Open
@NickiForte

Description

@NickiForte

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(
    {
        "date": ["2021-01-01", "2021-01-02", "2021-01-03"],
        "a": [4, 5.0, 6],
        "b": [7.0, 8, 9],
    }
)

df.rename(columns={"b": "a"}, inplace=True)
df.groupby("date").agg({"a": "sum"})

Issue Description

The following produces the error AttributeError: 'DataFrame' object has no attribute 'name'.

Expected Behavior

First of all, it is not obvious that this should produce an error, and if it does, the error could say that there are duplicate columns present.

One solution could also be to print a warning if you do something like df.rename(columns={"b": "a"}) in the example above to let the user know there are duplicate columns.

Installed Versions

INSTALLED VERSIONS

commit : 965ceca
python : 3.11.6.final.0
python-bits : 64
OS : Darwin
OS-release : 22.6.0
Version : Darwin Kernel Version 22.6.0: Thu Nov 2 07:43:57 PDT 2023; root:xnu-8796.141.3.701.17~6/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 2.0.2
numpy : 1.25.0
pytz : 2023.3
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.3.1
Cython : None
pytest : 7.1.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.1.2
lxml.etree : 4.9.3
html5lib : None
pymysql : None
psycopg2 : 2.9.4
jinja2 : 3.1.2
IPython : 8.17.2
pandas_datareader: None
bs4 : 4.12.2
bottleneck : None
brotli : None
fastparquet : 2023.4.0
fsspec : 2023.6.0
gcsfs : None
matplotlib : 3.8.1
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 12.0.1
pyreadstat : None
pyxlsb : None
s3fs : 0.4.2
scipy : 1.10.1
snappy : None
sqlalchemy : 2.0.16
tables : None
tabulate : None
xarray : 2023.10.1
xlrd : 2.0.1
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    ApplyApply, Aggregate, Transform, MapBugNeeds DiscussionRequires discussion from core team before further action

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions