Skip to content

BUG: join resets index even when doing left join on empty dataframes #52777

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
jeckst opened this issue Apr 19, 2023 · 6 comments · Fixed by #53893
Closed
3 tasks done

BUG: join resets index even when doing left join on empty dataframes #52777

jeckst opened this issue Apr 19, 2023 · 6 comments · Fixed by #53893
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@jeckst
Copy link

jeckst commented Apr 19, 2023

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

foo = pd.DataFrame([], index=pd.Index([], dtype="object"))
bar = foo.copy()

foo.join(bar).indexfoo.join(bar).index

Issue Description

On Pandas 1.5.3 above example produces Index([], dtype='object') while on 2.0.0 it produces RangeIndex(start=0, stop=0, step=1). I have not found this change in the release notes so I have to assume it is an unintended change and it causes unwanted consequences.

Expected Behavior

I expect the same behavior as in version 1.5.3

Installed Versions

INSTALLED VERSIONS

commit : 2e218d1
python : 3.10.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.19.0-38-generic
Version : #39~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Mar 17 21:16:15 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.3
numpy : 1.24.1
pytz : 2022.6
dateutil : 2.8.2
setuptools : 59.6.0
pip : 23.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.7.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.3
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : 2023.3

@jeckst jeckst added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 19, 2023
@asishm-wk
Copy link

asishm-wk commented Apr 19, 2023

@rhshadrach
Copy link
Member

rhshadrach commented Apr 21, 2023

Thanks @asishm-wk - agreed this is a consequence of the change to the default index. This now seems more consistent, e.g.

df1 = pd.DataFrame({'b': [3, 4, 5]})
df2 = pd.DataFrame({'c': [10, 11]})
df1.join(df2).index
# RangeIndex(start=0, stop=3, step=1)

so I have to assume it is an unintended change and it causes unwanted consequences.

@jeckst - I couldn't tell if you are assuming unwanted consequences or if you've experienced it.

cc @topper-123 @jbrockmendel @mroeschke

@rhshadrach rhshadrach added Indexing Related to indexing on series/frames, not to indexes themselves Reshaping Concat, Merge/Join, Stack/Unstack, Explode Closing Candidate May be closeable, needs more eyeballs labels Apr 21, 2023
@topper-123
Copy link
Contributor

topper-123 commented Apr 23, 2023

I would say that if the joined-on indexes are of dtype object, the joined index should also be an object dtype, also if empty. But that seems to be an issue with join, and not the issue with the default dtype IMO.

@rhshadrach
Copy link
Member

Thanks @topper-123 - I missed that the index is being specified as object in the OP. Agreed this should come out as object.

@rhshadrach rhshadrach removed Closing Candidate May be closeable, needs more eyeballs Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 23, 2023
@jeckst
Copy link
Author

jeckst commented Apr 24, 2023

I couldn't tell if you are assuming unwanted consequences or if you've experienced it.

@rhshadrach I experienced it, specifically I was doing something along the lines of df.index.to_series().apply(…).str.get(…) and ended up getting AttributeError: Can only use .str accessor with string values! when df was result of an empty join, which didn't use to happen in 1.5.3. It is obviously a pain when the code breaks based on whether the source DataFrames are empty or not (but now I can see that it's my fault).

I have obviously misdiagnosed the real reason for this and haven't noticed the connection to the changed default in release notes. What's worse, if my code relied on the index's dtype being something else, it wouldn't have worked even in 1.5.3, since the empty join used to reset the index to empty Index([], dtype="object"). So it behaves the same in 2.0.0 as in 1.5.3.

So at best, this is not a bug (apologies!), but an enhancement request — I would still argue that the empty join should probably keep the index type when reasonable.

@rhshadrach rhshadrach added the Regression Functionality that used to work in a prior pandas version label Apr 25, 2023
@rhshadrach rhshadrach added this to the 2.0.2 milestone Apr 25, 2023
@datapythonista datapythonista modified the milestones: 2.0.2, 2.0.3 May 26, 2023
@jbrockmendel
Copy link
Member

I would say that if the joined-on indexes are of dtype object, the joined index should also be an object dtype

this sounds right to me

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 Regression Functionality that used to work in a prior pandas version Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants