BUG: Merge with empty DataFrame raise IndexError (original) (raw)

@sinhrks

Description

import numpy as np
import pandas as pd

df1 = pd.DataFrame([], columns=['a', 'b', 'c'])
df2 = pd.DataFrame(np.random.randn(3, 3), columns=['x', 'y', 'z'])

# OK
pd.merge(df1, df2, how='right', left_index=True, right_index=True)
#      a    b    c         x         y         z
#0  NaN  NaN  NaN  0.665359  0.087728 -0.608138
#1  NaN  NaN  NaN -0.730847  0.882151  0.175648
#2  NaN  NaN  NaN  2.370834 -1.347337 -0.478547

pd.merge(df1, df2, how='outer', left_index=True, right_index=True)
#      a    b    c         x         y         z
#0  NaN  NaN  NaN  0.665359  0.087728 -0.608138
#1  NaN  NaN  NaN -0.730847  0.882151  0.175648
#2  NaN  NaN  NaN  2.370834 -1.347337 -0.478547

# NG, this should be the same as above
pd.merge(df1, df2, how='right', left_on='a', right_index=True)
# IndexError: cannot do a non-empty take from an empty axes.

pd.merge(df1, df2, how='outer', left_on='a', right_index=True)
# IndexError: cannot do a non-empty take from an empty axes.

This works if both DataFrame are empty.

df1 = pd.DataFrame([], columns=['a', 'b', 'c'])
df2 = pd.DataFrame([], columns=['x', 'y', 'z'])

pd.merge(df1, df2, how='right', left_on='a', right_index=True)
# Empty DataFrame
# Columns: [a, b, c, x, y, z]
# Index: []

pd.merge(df1, df2, how='outer', left_on='a', right_index=True)
# Empty DataFrame
# Columns: [a, b, c, x, y, z]
# Index: []