Skip to content

Commit

Permalink
BUG: retain index names of input frames to sjoin (geopandas#545)
Browse files Browse the repository at this point in the history
* BUG: sjoin modifies original frame (geopandas#525)

Original pandas table's index names are no
longer changed by sjoin() call. Includes
some unit-tests. Fixes issue geopandas#525

* New code to fix issue that is compatible with pandas 0.16.2 - 0.20.3

* applied suggested changes to test_sjoin.py
  • Loading branch information
robochat authored and jdmcbr committed Oct 22, 2017
1 parent 87e7541 commit 18160ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 4 additions & 2 deletions geopandas/tools/sjoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ def sjoin(left_df, right_df, how='inner', op='intersects',
# index in geopandas may be any arbitrary dtype. so reset both indices now
# and store references to the original indices, to be reaffixed later.
# GH 352
left_df.index.name = index_left
right_df.index.name = index_right
left_df = left_df.copy(deep=True)
left_df.index = left_df.index.rename(index_left)
left_df = left_df.reset_index()
right_df = right_df.copy(deep=True)
right_df.index = right_df.index.rename(index_right)
right_df = right_df.reset_index()

if op == "within":
Expand Down
11 changes: 10 additions & 1 deletion geopandas/tools/tests/test_sjoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_inner(self, op, dfs):
index, df1, df2, expected = dfs

res = sjoin(df1, df2, how='inner', op=op)

exp = expected[op].dropna().copy()
exp = exp.drop('geometry_y', axis=1).rename(
columns={'geometry_x': 'geometry'})
Expand Down Expand Up @@ -208,6 +208,15 @@ def test_sjoin_duplicate_column_name(self):
assert 'Shape_Area_left' in df.columns
assert 'Shape_Area_right' in df.columns

@pytest.mark.parametrize('how', ['left', 'right', 'inner'])
def test_sjoin_named_index(self,how):
#original index names should be unchanged
pointdf2 = self.pointdf.copy()
pointdf2.index.name = 'pointid'
df = sjoin(pointdf2, self.polydf, how=how)
assert pointdf2.index.name == 'pointid'
assert self.polydf.index.name == None

def test_sjoin_values(self):
# GH190
self.polydf.index = [1, 3, 4, 5, 6]
Expand Down

0 comments on commit 18160ef

Please sign in to comment.