Skip to content

BUG: empty groupby iloc equals empty dataframe but not empty groupby dataframe #48332

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

Open
3 tasks done
ntachukwu opened this issue Aug 31, 2022 · 1 comment
Open
3 tasks done
Labels
API - Consistency Internal Consistency of API/Behavior Bug Groupby

Comments

@ntachukwu
Copy link
Contributor

ntachukwu commented Aug 31, 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

import pandas as pd

df = pd.DataFrame([[1.2, 2]], columns=["a", "b"], dtype=float)
gb_df = df.groupby('a', group_keys=True).apply(lambda x: x)
    
df2 = pd.DataFrame(columns=["a", "b"], dtype=float)
gb_df.iloc[:0].equals(df2) # This returns True

gb_df2 = df2.groupby('a', group_keys=True).apply(lambda x: x)
gb_df.iloc[:0].equals(gb_df2) # This returns False

Issue Description

It seems inconsistent to have the elements of an empty groupby gb_df equal to an empty dataframe df2 but not equal to the groupby of df2 gb_df2.

This is part of an effort to find consistency between pandas operations with empty inputs. Issue #47959 is an attempt to start a discussion on this.

Expected Behavior

Should return True

Installed Versions

INSTALLED VERSIONS

commit : f0814bc
python : 3.9.10.final.0
python-bits : 64
OS : Darwin
OS-release : 21.3.0
Version : Darwin Kernel Version 21.3.0: Wed Jan 5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_ARM64_T8101
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.5.0.dev0+1364.g201cbf6bc1.dirty
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
setuptools : 60.9.3
pip : 22.1.1
Cython : 0.29.32
pytest : 7.1.2
hypothesis : 6.52.3
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.8.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.3.0
pandas_datareader: 0.10.0
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@ntachukwu ntachukwu added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 31, 2022
@jorisvandenbossche jorisvandenbossche added Groupby API - Consistency Internal Consistency of API/Behavior and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 5, 2022
@jorisvandenbossche
Copy link
Member

@Th3nn3ss I think part of the inconsistency here is also caused (and somewhat masked) by using equals(). The fact that it returns True vs False in both cases above might be a bug in equals.
Using the variables of your code above, but creating two other empty dataframes with a different index, and then calling equals():

# the result from groupby with non-emtpy dataframe has a MultiIndex
>>> empty_with_multiindex = gb_df.iloc[:0]
>>> empty_with_multiindex.index
MultiIndex([], names=['a', None])

# comparing that with other empty dataframes:
>>> empty_with_object_index = pd.DataFrame(columns=["a", "b"], dtype=float, index=pd.Index([], dtype=object))
>>> empty_with_float_index = pd.DataFrame(columns=["a", "b"], dtype=float, index=pd.Index([], dtype=float))
>>> empty_with_multiindex.equals(empty_with_object_index)
True
>>> empty_with_multiindex.equals(empty_with_float_index)
False

So the above returning True or False depending on the data type of the index seems a bug. I would actually expect False in both cases, since both don't have a MultiIndex.
Although also that is not 100% clear, since the docstring of equals says the following about the index:

The row/column index do not need to have the same type, as long
as the values are considered equal. Corresponding columns must be of
the same dtype.

But since they are empty, there are no values to compare (although I would still say that the number of levels of the index is something that should be checked).

(it might be worth to track this as a separate issue)


Making the actual groupby inconsistency you raise a bit more visible:

>>> df = pd.DataFrame([[1.2, 2]], columns=["a", "b"], dtype=float)
>>> df.groupby('a', group_keys=True).apply(lambda x: x)
         a    b
a              
1.2 0  1.2  2.0

>>> df.iloc[:0].groupby('a', group_keys=True).apply(lambda x: x)
Empty DataFrame
Columns: [a, b]
Index: []
>>> df.iloc[:0].groupby('a', group_keys=True).apply(lambda x: x).index
Float64Index([], dtype='float64', name='a')

So both the empty and non-empty cases still have a different index (MultiIndex vs FloatIndex), so I think this is still an inconsistency worth resolving.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Bug Groupby
Projects
None yet
Development

No branches or pull requests

2 participants