Skip to content
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

BUG: Change in index of empty dataframes in mode operation #43336

Closed
2 of 3 tasks
galipremsagar opened this issue Aug 31, 2021 · 3 comments
Closed
2 of 3 tasks

BUG: Change in index of empty dataframes in mode operation #43336

galipremsagar opened this issue Aug 31, 2021 · 3 comments
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug Closing Candidate May be closeable, needs more eyeballs

Comments

@galipremsagar
Copy link

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


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

Code Sample, a copy-pastable example

Previously pandas used to retain the index in an empty dataframe:

>>> df = pd.DataFrame({'a':['a', 'b', 'c']}, index=['a', 'b', 'c'])
>>> df.mode()
   a
0  a
1  b
2  c
>>> df.mode(numeric_only=True)
Empty DataFrame
Columns: []
Index: [a, b, c]
>>> pd.__version__
'1.2.5'

Currently the index is not being retained if the dataframe is empty:

>>> import pandas as pd
>>> df = pd.DataFrame({'a':['a', 'b', 'c']}, index=['a', 'b', 'c'])
>>> df.mode()
   a
0  a
1  b
2  c
>>> df.mode(numeric_only=True)
Empty DataFrame
Columns: []
Index: []
>>> pd.__version__

Is this a conscious change, if yes can someone point me to the docs where I can learn more about this change?

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 5f648bf
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-76-generic
Version : #86-Ubuntu SMP Fri Jan 17 17:24:28 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.2
numpy : 1.21.2
pytz : 2021.1
dateutil : 2.8.2
pip : 21.2.4
setuptools : 57.4.0
Cython : 0.29.24
pytest : 6.2.5
hypothesis : 6.17.4
sphinx : 4.1.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.27.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : 2021.08.1
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 5.0.0
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : 0.53.1

@galipremsagar galipremsagar added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 31, 2021
@simonjayhawkins
Copy link
Member

Thanks @galipremsagar for the report.

The new behavior looks potentially correct to me. (It maybe should raise instead @jbrockmendel?)

The number of rows in the result of a mode operation (with the default axis) corresponds to the highest number of modes in the results over all the columns.

For the first example, there are 3 modes in the only column present, hence 3 rows in the result.

In the second example there are no columns to evaluate (since numeric_only=True) and hence no modes so I would expect the result to have no rows.

Is this a conscious change, if yes can someone point me to the docs where I can learn more about this change?

I do a bisect shortly to get more context.

simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Sep 1, 2021
@simonjayhawkins
Copy link
Member

Is this a conscious change, if yes can someone point me to the docs where I can learn more about this change?

I do a bisect shortly to get more context.

change looks intentional to fix #33321

[508d0d2] BUG: Ensure series/frame mode() keeps int index (#38732)

@simonjayhawkins simonjayhawkins added Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Closing Candidate May be closeable, needs more eyeballs and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 1, 2021
@galipremsagar
Copy link
Author

@simonjayhawkins Thanks for clarifying, closing this issue.

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 Bug Closing Candidate May be closeable, needs more eyeballs
Projects
None yet
Development

No branches or pull requests

2 participants