Skip to content

BUG: pandas merge suffixes accepting set can interchange right and left suffix order #33740

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
PuneethaPai opened this issue Apr 23, 2020 · 3 comments · Fixed by #34208
Closed
3 tasks done
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@PuneethaPai
Copy link
Contributor

  • 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.


df_b = pd.DataFrame(dict(p=[1,2,3], q=[2,3,4]))
df_a = pd.DataFrame(dict(p=[2,3,4], q=[1,2,3]))
pd.merge(left=df_b, right=df_a, on=['p'], how='outer', suffixes={'_b', '_a'})

Current output:

  p q_a q_b
0 1 2.0 NaN
1 2 3.0 1.0
2 3 4.0 2.0
3 4 NaN 3.0

Expected Output:

  p q_b q_a
0 1 2.0 NaN
1 2 3.0 1.0
2 3 4.0 2.0
3 4 NaN 3.0

Problem description

This line is causing the issue:

lsuf, rsuf = self.suffixes

When you unpack set it returns elements in sorted order:

l, r = (2, 1); print(l, r) # l=2, r=1
l, r = [2, 1]; print(l, r) # l=2, r=1
l, r = {2, 1}; print(l, r) # l=1, r=2

Output of pd.show_versions()

[INSTALLED VERSIONS ------------------ commit : None python : 3.8.2.final.0 python-bits : 64 OS : Linux OS-release : 4.14.165-133.209.amzn2.x86_64 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 1.0.3
numpy : 1.18.3
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.3.post20200330
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.13.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.2.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : 0.4.2
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None]

@PuneethaPai PuneethaPai added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 23, 2020
@jreback
Copy link
Contributor

jreback commented Apr 23, 2020

this should be an error
suffixes must be a tuple / list

a PR would be accepted for this patch

@PuneethaPai
Copy link
Contributor Author

I would like to contribute. @jreback we should restrict suffixes parameter type to be either tuple/list right?
Else raise InvalidInputException or ValueError something like this right?

@jreback
Copy link
Contributor

jreback commented Apr 23, 2020

yep ValueError

@dsaxton dsaxton added Reshaping Concat, Merge/Join, Stack/Unstack, Explode and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 23, 2020
@jreback jreback added this to the 1.1 milestone Jun 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
3 participants