def astype(self, dtype, copy=True, errors="raise", **kwargs): """ Cast a pandas object to a specified dtype ``dtype``. Returns a GeoDataFrame when the geometry column is kept as geometries, otherwise returns a pandas DataFrame. See the pandas.DataFrame.astype docstring for more details. Returns ------- GeoDataFrame or DataFrame """ df = super(GeoDataFrame, self).astype(dtype, copy=copy, errors=errors, **kwargs) try: geoms = df[self._geometry_column_name] if is_geometry_type(geoms): return geopandas.GeoDataFrame( df, geometry=self._geometry_column_name) except KeyError: pass # if the geometry column is converted to non-geometries or did not exist # do not return a GeoDataFrame return pd.DataFrame(df)
def _ensure_geometry(data): """ Ensure the data is of geometry dtype or converted to it. If input is a (Geo)Series, output is a GeoSeries, otherwise output is GeometryArray. """ if is_geometry_type(data): if isinstance(data, Series): return GeoSeries(data) return data else: if isinstance(data, Series): out = from_shapely(np.asarray(data)) return GeoSeries(out, index=data.index, name=data.name) else: out = from_shapely(data) return out
def _ensure_geometry(data, crs=None): """ Ensure the data is of geometry dtype or converted to it. If input is a (Geo)Series, output is a GeoSeries, otherwise output is GeometryArray. If the input is a GeometryDtype with a set CRS, `crs` is ignored. """ if is_geometry_type(data): if isinstance(data, Series): return GeoSeries(data) return data else: if isinstance(data, Series): out = from_shapely(np.asarray(data), crs=crs) return GeoSeries(out, index=data.index, name=data.name) else: out = from_shapely(data, crs=crs) return out