BUG: Merge between partial index and index fails if result is empty · Issue #33814 · pandas-dev/pandas (original) (raw)
- 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 pandas pandas.merge( pandas.DataFrame({'a': [1], 'i': [2]}).set_index(['a', 'i']), pandas.DataFrame({'i': [1]}).set_index(['i']), left_on=['i'], right_index=True, )
Problem description
This merge fails with the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 88, in merge
return op.get_result()
File "/usr/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 668, in get_result
self._maybe_add_join_keys(result, left_indexer, right_indexer)
File "/usr/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 824, in _maybe_add_join_keys
key_col.name = name
AttributeError: 'numpy.ndarray' object has no attribute 'name'
Thing is, it works fine if I change i
in the left DataFrame from [2]
to [1]
. It only fails if there's no overlap between the join keys.
This behavior is problematic because the failure depends on the outcome of the merge, so it's difficult to avoid.
Expected Output
I expect this to not fail and just return an empty result.
Output of pd.show_versions()
>>> pandas.show_versions()
INSTALLED VERSIONS
------------------
commit : None
python : 3.8.2.final.0
python-bits : 64
OS : Linux
OS-release : 5.6.2-arch1-2
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_FYL.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.0.3
numpy : 1.18.2
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.3
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
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 : None
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : 3.2.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.15
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None