Skip to content

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

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
pikeas opened this issue Nov 21, 2012 · 2 comments
Closed
Labels
Milestone

Comments

@pikeas
Copy link

pikeas 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
Copy link

ghost 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
Copy link
Author

pikeas 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
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants