Skip to content

BUG: pser.mode() should not be set series name #46737

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
3 tasks done
Yikun opened this issue Apr 11, 2022 · 5 comments · Fixed by #46746
Closed
3 tasks done

BUG: pser.mode() should not be set series name #46737

Yikun opened this issue Apr 11, 2022 · 5 comments · Fixed by #46746
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff good first issue metadata _metadata, .attrs Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@Yikun
Copy link
Contributor

Yikun commented Apr 11, 2022

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

After 1.4.x:

import pandas as pd
pser = pd.Series([0, 0, 1, 1, 1])
pser.name = 'x'
pser.mode()

pser.mode()
0    1
Name: x, dtype: int64

Issue Description

pser.mode() should not be set series name.

Expected Behavior

Before 1.4:

pser.mode()
0    1
dtype: int64

Installed Versions

INSTALLED VERSIONS

commit : 4bfe3d0
python : 3.9.10.final.0
python-bits : 64
OS : Darwin
OS-release : 21.4.0
Version : Darwin Kernel Version 21.4.0: Mon Feb 21 20:35:58 PST 2022; root:xnu-8020.101.4~2/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : zh_CN.UTF-8
LOCALE : zh_CN.UTF-8

pandas : 1.4.2
numpy : 1.22.3
pytz : 2021.3
dateutil : 2.8.2
pip : 22.0.4
setuptools : 60.5.0
Cython : None
pytest : 7.1.0
hypothesis : None
sphinx : 3.0.4
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 8.1.1
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : 2.0.1
matplotlib : 3.2.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 7.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
snappy : None
sqlalchemy : 1.4.32
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
zstandard : None

@Yikun Yikun added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 11, 2022
@Yikun
Copy link
Contributor Author

Yikun commented Apr 11, 2022

first bad commit: 680e372

pandas/pandas/core/series.py

Lines 2061 to 2063 in cec066d

return self._constructor(
res_values, index=range(len(res_values)), name=self.name
)

 return self._constructor( 
     res_values, index=range(len(res_values))
 ) 

@samukweku
Copy link
Contributor

@Yikun Is this unique to mode or do other aggregations have the same output? E.g mean/median?

@Yikun
Copy link
Contributor Author

Yikun commented Apr 11, 2022

@samukweku I think it's only in mode after 1.4.x,so I think it's an unexpected change.

@mroeschke
Copy link
Member

Though it's an unexpected change, I think this is an accidental bug fix as Series.name should be preserved whenever possible. Doesn't appear to be a test for this so would be good to add one.

@mroeschke mroeschke added Testing pandas testing functions or related to the test suite good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member good first issue Testing pandas testing functions or related to the test suite labels Apr 12, 2022
@henryh9n
Copy link

@Yikun @mroeschke I think the issue here is not that adding name=self.name fixed the bug, but that it was missing metadata propagation. I think it should be something like

        return self._constructor(
            res_values, index=range(len(res_values))
        ).__finalize__(
            self, method="mode"
        )

similar to all of the other methods. This will not only preserve the name, but also the other attributes (if there are, or will be, any others)

@jreback jreback added this to the 1.5 milestone Apr 13, 2022
@jreback jreback added Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff metadata _metadata, .attrs labels Apr 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff good first issue metadata _metadata, .attrs Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants