-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: merge left and merge inner produce different index-order #33554
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
Comments
Here's a smaller example: import pandas as pd
left = pd.DataFrame({"a": [1, 2, 1]})
right = pd.DataFrame({"a": [1, 2]})
print(pd.merge(left, right, how="inner", on="a"))
# a
# 0 1
# 1 1
# 2 2 Does look like the documentation isn't entirely accurate here. |
Hey, thanks for the comment! That's great, as I tried to create a smaller example myself, but in all cases |
Btw - is it a problem in the documentation, or rather in the implementation? |
I suppose you could call it either, but personally I'd consider it more a documentation issue since it's promising behavior that's hard to define in general. In the example above, suppose the right DataFrame was instead right = pd.DataFrame({"a": [1, 1, 2]}) so now the join is many-to-many. What does it mean to preserve order on the left? Are both [1, 1, 2, 1, 1] and [1, 1, 1, 2, 1] the "same" order as [1, 2, 1]? It's not as obvious when data is changing and wasn't ordered to begin with. |
@dsaxton I think you could make a case for |
According to the
df.merge
docstring and documentation, concerning thehow
parameter:(emphasis mine.) I understand this to mean, that the order of the left index will be preserved when using
how='left'
andhow='inner'
(or omitted). Of course, some keys might not be present in the second case, but that's beside the point here.If my understanding is incorrect, my apologies and feel free to discard this report.
If my understanding is correct, however, I noticed today that this is not always true. Here is an example, where the order is not preserved.
Code Sample
The dataframes:
Output
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.
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.7.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en
LOCALE : None.None
pandas : 1.0.3
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 42.0.2.post20191203
Cython : 0.29.15
pytest : 5.4.1
hypothesis : 5.8.3
sphinx : 2.4.4
blosc : None
feather : None
xlsxwriter : 1.2.8
lxml.etree : 4.5.0
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.1
IPython : 7.13.0
pandas_datareader: None
bs4 : 4.8.2
bottleneck : 1.3.2
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : None
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.4.1
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.15
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : 1.3.0
xlsxwriter : 1.2.8
numba : 0.48.0
The text was updated successfully, but these errors were encountered: