Skip to content

BUG: merge_ordered fails when left_by is set to more than one column #35269

@Rufflewind

Description

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


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import io, pandas
l = pandas.read_csv(io.StringIO('''
G H T 
g h 1   
g h 3  
'''), delim_whitespace=True)
r = pandas.read_csv(io.StringIO('''
T
2
'''), delim_whitespace=True)
pandas.merge_ordered(l, r, on=['T'], left_by=['G', 'H'])

Problem description

This fails:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 290, in merge_ordered
    result, _ = _groupby_and_merge(
  File "/usr/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 162, in _groupby_and_merge
    merged[k] = key
  File "/usr/lib/python3.8/site-packages/pandas/core/frame.py", line 2938, in __setitem__
    self._set_item(key, value)
  File "/usr/lib/python3.8/site-packages/pandas/core/frame.py", line 3000, in _set_item
    value = self._sanitize_column(key, value)
  File "/usr/lib/python3.8/site-packages/pandas/core/frame.py", line 3636, in _sanitize_column
    value = sanitize_index(value, self.index, copy=False)
  File "/usr/lib/python3.8/site-packages/pandas/core/internals/construction.py", line 611, in sanitize_index
    raise ValueError("Length of values does not match length of index")
ValueError: Length of values does not match length of index

Expected Output

Not failing. Should return:

   G  H  T
0  g  h  1
1  g  h  3

For comparison, the above works fine if we use left_by=['G'] and omit the H column entirely.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.3.final.0
python-bits : 64
OS : Linux
OS-release : 5.7.7-arch1-1
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_FYL.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.5
numpy : 1.19.0
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 49.1.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.1
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.16.1
pandas_datareader: None
bs4 : 4.9.1
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.5.1
matplotlib : 3.2.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.5.0
sqlalchemy : 1.3.18
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions