def test_value_counts(): # each object is considered unique s = GeoSeries([Point(0, 0), Point(1, 1), Point(0, 0)]) res = s.value_counts() with compat.ignore_shapely2_warnings(): exp = pd.Series([2, 1], index=[Point(0, 0), Point(1, 1)]) assert_series_equal(res, exp) # Check crs doesn't make a difference - note it is not kept in output index anyway s2 = GeoSeries([Point(0, 0), Point(1, 1), Point(0, 0)], crs="EPSG:4326") res2 = s2.value_counts() assert_series_equal(res2, exp) # check mixed geometry s3 = GeoSeries([Point(0, 0), LineString([[1, 1], [2, 2]]), Point(0, 0)]) res3 = s3.value_counts() with compat.ignore_shapely2_warnings(): exp3 = pd.Series([2, 1], index=[Point(0, 0), LineString([[1, 1], [2, 2]])]) assert_series_equal(res3, exp3) # check None is handled s4 = GeoSeries([Point(0, 0), None, Point(0, 0)]) res4 = s4.value_counts(dropna=True) with compat.ignore_shapely2_warnings(): exp4_dropna = pd.Series([2], index=[Point(0, 0)]) assert_series_equal(res4, exp4_dropna) with compat.ignore_shapely2_warnings(): exp4_keepna = pd.Series([2, 1], index=[Point(0, 0), None]) res4_keepna = s4.value_counts(dropna=False) assert_series_equal(res4_keepna, exp4_keepna)
def test_from_frame(self): data = { "A": range(3), "B": np.arange(3.0), "geometry": [Point(x, x) for x in range(3)], } gpdf = GeoDataFrame(data) with ignore_shapely2_warnings(): pddf = pd.DataFrame(data) check_geodataframe(gpdf) assert type(pddf) == pd.DataFrame for df in [gpdf, pddf]: res = GeoDataFrame(df) check_geodataframe(res) res = GeoDataFrame(df, index=pd.Index([0, 2])) check_geodataframe(res) assert_index_equal(res.index, pd.Index([0, 2])) assert res["A"].tolist() == [0, 2] res = GeoDataFrame(df, columns=["geometry", "B"]) check_geodataframe(res) assert_index_equal(res.columns, pd.Index(["geometry", "B"])) with pytest.raises(ValueError): GeoDataFrame(df, geometry="other_geom")
def test_overwrite_geometry(self): # GH602 data = pd.DataFrame({"geometry": [1, 2, 3], "col1": [4, 5, 6]}) with ignore_shapely2_warnings(): geoms = pd.Series([Point(i, i) for i in range(3)]) # passed geometry kwarg should overwrite geometry column in data res = GeoDataFrame(data, geometry=geoms) assert_geoseries_equal(res.geometry, GeoSeries(geoms))
def test_array_interface(self, data): # we are overriding this base test because the creation of `expected` # potentially doesn't work for shapely geometries # TODO can be removed with Shapely 2.0 result = np.array(data) assert result[0] == data[0] result = np.array(data, dtype=object) # expected = np.array(list(data), dtype=object) expected = np.empty(len(data), dtype=object) with ignore_shapely2_warnings(): expected[:] = list(data) assert_array_equal(result, expected)
def test_array(self): data = { "A": range(3), "B": np.arange(3.0), "geometry": [Point(x, x) for x in range(3)], } with ignore_shapely2_warnings(): a = np.array([data["A"], data["B"], data["geometry"]], dtype=object).T df = GeoDataFrame(a, columns=["A", "B", "geometry"]) check_geodataframe(df) df = GeoDataFrame(a, columns=["A", "B", "other_geom"], geometry="other_geom") check_geodataframe(df, "other_geom")
def test_from_series(self): shapes = [ Polygon([(random.random(), random.random()) for _ in range(3)]) for _ in range(10) ] with ignore_shapely2_warnings(): # the warning here is not suppressed by GeoPandas, as this is a pure # pandas construction call s = pd.Series(shapes, index=list("abcdefghij"), name="foo") g = GeoSeries(s) check_geoseries(g) assert [a.equals(b) for a, b in zip(s, g)] assert s.name == g.name assert s.index is g.index
def test_from_frame_specified_geometry(self): data = { "A": range(3), "B": np.arange(3.0), "other_geom": [Point(x, x) for x in range(3)], } gpdf = GeoDataFrame(data, geometry="other_geom") check_geodataframe(gpdf, "other_geom") with ignore_shapely2_warnings(): pddf = pd.DataFrame(data) for df in [gpdf, pddf]: res = GeoDataFrame(df, geometry="other_geom") check_geodataframe(res, "other_geom") # gdf from gdf should preserve active geometry column name df = GeoDataFrame(gpdf) check_geodataframe(df, "other_geom")
def test_from_frame_specified_geometry(self): data = { "A": range(3), "B": np.arange(3.0), "other_geom": [Point(x, x) for x in range(3)], } gpdf = GeoDataFrame(data, geometry="other_geom") check_geodataframe(gpdf, "other_geom") with ignore_shapely2_warnings(): pddf = pd.DataFrame(data) for df in [gpdf, pddf]: res = GeoDataFrame(df, geometry="other_geom") check_geodataframe(res, "other_geom") # when passing GeoDataFrame with custom geometry name to constructor # an invalid geodataframe is the result TODO is this desired ? df = GeoDataFrame(gpdf) with pytest.raises(AttributeError): df.geometry
def make_data(): a = np.empty(100, dtype=object) with ignore_shapely2_warnings(): a[:] = [shapely.geometry.Point(i, i) for i in range(100)] ga = from_shapely(a) return ga