Skip to content

BUG: Merge with empty DataFrame raise IndexError #10824

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

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

BUG: Merge with empty DataFrame raise IndexError #10824

sinhrks opened this issue Aug 15, 2015 · 0 comments
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@sinhrks
Copy link
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 sinhrks added Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Aug 15, 2015
@sinhrks sinhrks added this to the 0.17.0 milestone Aug 15, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

1 participant