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: index.has_duplicates on a subset of a dataframe sometimes returns an incorrect outcome #60355

Open
3 tasks done
HungryZebra563 opened this issue Nov 18, 2024 · 1 comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Needs Info Clarification about behavior needed to assess issue

Comments

@HungryZebra563
Copy link

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([0, 0], index=['A','A'])

df.iloc[0:1, :].index.has_duplicates  # <-- this returns False (correctly)
df.index.has_duplicates               # <-- this somehow alters the outcome       
df.iloc[0:1, :].index.has_duplicates  # <-- exact same statement, but this returns True (incorrectly)

Issue Description

The value of index.has_duplicates of a subset of a dataframe depends on whether we run index.has_duplicates on the full dataframe first.

In the example, df.iloc[0:1, :] only has a single row, so it cannot have duplicates. The first call to df.iloc[0:1, :].index.has_duplicates correctly returns False. However, after we queried the same property on the full dataframe, df.index.has_duplicates, the exact same statement on the subset now returns True.

Expected Behavior

I would expect df.iloc[0:1, :] to always return False. It has only 1 row, so it cannot have duplicates.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.12.7
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.22631
machine : AMD64
processor : Intel64 Family 6 Model 154 Stepping 4, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.2.3
numpy : 2.1.3
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 24.2
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None

@HungryZebra563 HungryZebra563 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 18, 2024
@rhshadrach rhshadrach added the Indexing Related to indexing on series/frames, not to indexes themselves label Nov 18, 2024
@rhshadrach
Copy link
Member

Thanks for the report! I can reproduce this on the 2.2.x branch but not on main. You've checked the box that you've verified this bug exists on the main branch of pandas. Can you confirm if you've done that?

@rhshadrach rhshadrach added Needs Info Clarification about behavior needed to assess issue and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Needs Info Clarification about behavior needed to assess issue
Projects
None yet
Development

No branches or pull requests

2 participants