Skip to content
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

DataFrame.combine_first() breaks for DataFrame with indices but no columns #2307

Closed
pikeas opened this issue Nov 21, 2012 · 2 comments

Comments

@pikeas
Copy link

commented Nov 21, 2012

Title says it all. core/frame.py, combine() method at line 3448:

    if other.empty:
        return self.copy()

If other is a DataFrame which has indices but no columns, a combine_first() should add the indices with every column set to NaN.

Sorry for not submitting a patch myself - I'm very new to the pandas codebase, so I'm not sure whether this should be resolved in combine() for all combination attempts, or only in combine_first().

...Maybe empty should check whether the DataFrame has indices? Though that could silently break a lot of old code...

@ghost

This comment has been minimized.

Copy link

commented Nov 22, 2012

@pikeas , can you provide a testcase? I tried the following:

import pandas as pd
a=pd.DataFrame([1,2])
a.combine_first(pd.DataFrame())

which looks like it's working here.

and:

import pandas as pd
a=pd.DataFrame([1,2])
b=pd.DataFrame(index=[1,2])
a.combine_first(b)

which also works

@pikeas

This comment has been minimized.

Copy link
Author

commented Nov 22, 2012

Sure thing!

df = pd.DataFrame(index=[2,3,4])
df['A'] = 5
missing = pd.DataFrame(index=[0,1])
combined = df.combine_first(missing)

Actual output:

In [171]: combined
Out[171]: 
   A
2  5
3  5
4  5

Expected output:

In [171]: combined
Out[171]: 
   A
0  NaN
1  NaN
2  5
3  5
4  5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.