BUG: Merge with empty DataFrame raise IndexError #10824

Closed
sinhrks opened this Issue Aug 15, 2015 · 0 comments

Comments

Projects
None yet
1 participant
Member

sinhrks commented Aug 15, 2015

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: []

sinhrks added this to the 0.17.0 milestone Aug 15, 2015

jreback closed this in #10826 Aug 20, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment