BUG: Align index if set_geometry input is Series

If DataFrame's set_geometry() is called with a Series, align
the index.
This commit is contained in:
Jacob Wasserman
2014-03-24 00:46:10 -04:00
parent 1edddada8f
commit 04dfaf9a10
2 changed files with 22 additions and 3 deletions
+1 -3
View File
@@ -113,9 +113,7 @@ class GeoDataFrame(GeoPandasBase, DataFrame):
to_remove = None
geo_column_name = DEFAULT_GEO_COLUMN_NAME
if isinstance(col, Series):
level = col.values
elif isinstance(col, (list, np.ndarray)):
if isinstance(col, (Series, list, np.ndarray)):
level = col
elif hasattr(col, 'ndim') and col.ndim != 1:
raise ValueError("Must pass array with one dimension only.")
+21
View File
@@ -182,6 +182,27 @@ class TestDataFrame(unittest.TestCase):
geom = GeoSeries(geom, index=self.df.index, crs=self.df.crs)
assert_geoseries_equal(self.df.geometry, geom)
def test_set_geometry_series(self):
# Test when setting geometry with a Series that
# alignment will occur
#
# Reverse the index order
# Set the Series to be Point(i,i) where i is the index
self.df.index = range(len(self.df)-1, -1, -1)
d = {}
for i in range(len(self.df)):
d[i] = Point(i, i)
g = GeoSeries(d)
# At this point, the DataFrame index is [4,3,2,1,0] and the
# GeoSeries index is [0,1,2,3,4]. Make sure set_geometry aligns
# them to match indexes
df = self.df.set_geometry(g)
for i, r in df.iterrows():
self.assertAlmostEqual(i, r['geometry'].x)
self.assertAlmostEqual(i, r['geometry'].y)
def test_to_json(self):
text = self.df.to_json()
data = json.loads(text)