Skip to content

ENH: Use left._constructor on pd.merge #7737

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

Merged
merged 1 commit into from
Aug 11, 2014

Conversation

jwass
Copy link
Contributor

@jwass jwass commented Jul 12, 2014

Use the _constructor property when creating the merge result to
preserve the output type.

If a GeoPandas GeoDataFrame is merged with a DataFrame, the result is hard-coded to always be DataFrame GeoPandas Issue #118. We'd like it to return GeoDataFrame in these cases

>>> import geopandas as gpd
>>> import pandas as pd

>>> gdf = gpd.GeoDataFrame(...)
>>> df = pd.DataFrame(...)

>>> merged = pd.merge(gdf, df, on='column')
>>> type(merged)
GeoDataFrame

This PR uses left._constructor to generate the result type for merge operations.

@jreback
Copy link
Contributor

jreback commented Jul 12, 2014

sure

can u add a test pls (create an object that inherits from DataFrame and use it to confirm that this is correct)

@jwass
Copy link
Contributor Author

jwass commented Jul 14, 2014

@jreback Done.

@jreback jreback added this to the 0.15.0 milestone Jul 14, 2014
@jankatins
Copy link
Contributor

It would also be nice to mention this in the docs (i.e. that the output type is taken from left, so that pd.merge(df, gdf, on='column') still returns a DataFrame...)

@jreback
Copy link
Contributor

jreback commented Aug 8, 2014

@jwass this is fine, can you update the docs (and do-string). squash and looks ready.

@jwass
Copy link
Contributor Author

jwass commented Aug 9, 2014

@JanSchulz Missed your original comment from a few weeks ago - sorry for the delay here.

@jreback squashed and rebased. Let me know if the docs should change somewhere else or should be re-worded.

@@ -520,7 +521,8 @@ def get_result(self):
axes=[llabels.append(rlabels), join_index],
concat_axis=0, copy=self.copy)

result = DataFrame(result_data)
typ = self.left._constructor
result = typ(result_data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should prob also have __finalize__ called on it? (I know wasn't in the original and doesn't actually do anything in pandas atm..but)

@jreback
Copy link
Contributor

jreback commented Aug 10, 2014

can you add a note in v0.15.0.txt in API section (and reference this PR number)

Use the _constructor property when creating the merge/ordered_merge result to
preserve the output type. Update test and docs appropriately.
@jwass
Copy link
Contributor Author

jwass commented Aug 11, 2014

@jreback Yes - added the __finalize__ call in ordered_merge. While I was at it, added the test/doc for ordered_merge to reflect the new behavior here too.
Added the note for v0.15.0. Squashed, rebased.

@jreback jreback merged commit 8ca9c85 into pandas-dev:master Aug 11, 2014
@jreback
Copy link
Contributor

jreback commented Aug 11, 2014

merged via 123a555

thanks!

@jwass jwass deleted the merge_constructor branch August 11, 2014 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compat pandas objects compatability with Numpy or Python functions Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants