Skip to content

Cannot do agg if dataframe has column "name" #24915

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
ghost opened this issue Jan 24, 2019 · 6 comments
Closed

Cannot do agg if dataframe has column "name" #24915

ghost opened this issue Jan 24, 2019 · 6 comments
Labels
Apply Apply, Aggregate, Transform, Map Duplicate Report Duplicate issue or pull request Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@ghost
Copy link

ghost commented Jan 24, 2019

Code Sample, a copy-pastable example if possible

# Your code here
import pandas as pd

df = pd.DataFrame({"a": range(10)})
df.agg({"a": 'mean'}) # this line runs good

df["name"] = range(10)
df.agg({"a": 'mean'}) # now we have RuntimeError: maximum recursion depth exceeded

Problem description

If a dataframe has a column which is called "name", doing aggregation on the dataframe will cause RuntimeError.

But if the column is not called "name" but with some variant like "name0", "name_", it runs good.
import pandas as pd

df = pd.DataFrame({"a": range(10)})
df.agg({"a": 'mean'}) # this line runs good

df["name_"] = range(10)  # not "name"
df.agg({"a": 'mean'}) # this line runs good as well

Expected Output

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]

INSTALLED VERSIONS

commit: None
python: 2.7.15.final.0
python-bits: 64
OS: Darwin
OS-release: 18.0.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: None.None

pandas: 0.23.4
pytest: None
pip: 18.1
setuptools: 40.6.3
Cython: None
numpy: 1.16.0
scipy: 1.2.0
pyarrow: 0.11.1
xarray: None
IPython: 5.8.0
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.9
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: 1.1.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.9999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: 0.2.0
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@ghost
Copy link
Author

ghost commented Jan 24, 2019

@YaoquanYe

@ghost
Copy link
Author

ghost commented Jan 24, 2019

I think I found the issue.

Mainly because:

  1. "name" is a column in df
  2. When agg is executed with "func" parameter as a dictionary
  3. In pandas.core.base.py line 543, it is trying to get "name" attribute from self(df).
  4. The "name" attribute is supposed to be a quick reference to the dataframe that is assign by user df.name = "my dataframe". But now it is a pd.Series.
  5. df.name as pd.Series will trigger TypeError, and no result will be returned.
  6. So agg function will move on to execute self.apply() pandas.core.frame.py line 5828
  7. In apply function it uses df.agg() (go back to step 2) so an infinite loop is created.

My suggestion would be in step 3,4 (pandas.core.base.py line 543) apply additional rule to get "name" from self.

Any comment is welcome!

@mroeschke
Copy link
Member

Getting the same result on master. @youhealthy a pull request with that change is welcome!

@ghost
Copy link
Author

ghost commented Jan 25, 2019

@mroeschke thank you! PR submitted.

@mroeschke mroeschke added the Apply Apply, Aggregate, Transform, Map label Nov 2, 2019
@phofl
Copy link
Member

phofl commented Sep 14, 2020

Was fixed by #36224, maybe add a test? I am not sure if this is exactly the same case

@phofl phofl added Needs Tests Unit test(s) needed to prevent regressions and removed Bug labels Sep 14, 2020
@jreback
Copy link
Contributor

jreback commented Sep 14, 2020

this is the same case as #36212

@jreback jreback closed this as completed Sep 14, 2020
@jreback jreback added the Duplicate Report Duplicate issue or pull request label Sep 14, 2020
@jreback jreback added this to the 1.2 milestone Sep 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Duplicate Report Duplicate issue or pull request Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants