From fa48e48069db868a8ee91673d24b85e81375e94a Mon Sep 17 00:00:00 2001 From: Kelsey Jordahl Date: Wed, 30 Oct 2013 22:26:41 -0400 Subject: [PATCH] ENH: Define new _constructor property for GeoPandas objects Special fillna method is no longer needed for recent pandas versions. Need an extra check in __getitem__ to return a DataFrame if no geometry --- geopandas/geodataframe.py | 7 +++++++ geopandas/geoseries.py | 29 ++++++++++++----------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/geopandas/geodataframe.py b/geopandas/geodataframe.py index d956f01..1feb9f5 100644 --- a/geopandas/geodataframe.py +++ b/geopandas/geodataframe.py @@ -206,12 +206,19 @@ class GeoDataFrame(DataFrame): elif isinstance(result, DataFrame) and 'geometry' in result: result.__class__ = GeoDataFrame result.crs = self.crs + elif isinstance(result, DataFrame) and 'geometry' not in result: + result.__class__ = DataFrame + result.crs = self.crs return result # # Implement pandas methods # + @property + def _constructor(self): + return GeoDataFrame + def __finalize__(self, other, method=None, **kwargs): """ propagate metadata from other to self """ # NOTE: backported from pandas master (upcoming v0.13) diff --git a/geopandas/geoseries.py b/geopandas/geoseries.py index 843c680..b339300 100644 --- a/geopandas/geoseries.py +++ b/geopandas/geoseries.py @@ -492,6 +492,10 @@ class GeoSeries(Series): # Implement pandas methods # + @property + def _constructor(self): + return GeoSeries + def _wrapped_pandas_method(self, mtd, *args, **kwargs): """Wrap a generic pandas method to ensure it returns a GeoSeries""" val = getattr(super(GeoSeries, self), mtd)(*args, **kwargs) @@ -500,6 +504,14 @@ class GeoSeries(Series): val.crs = self.crs return val + def _wrapped_pandas_method_series(self, mtd, *args, **kwargs): + """Wrap a generic pandas method to ensure it returns a Series""" + val = getattr(super(GeoSeries, self), mtd)(*args, **kwargs) + return val.view(Series) + + def where(self, *args, **kwargs): + return self._wrapped_pandas_method_series('where', *args, **kwargs) + def __getitem__(self, key): return self._wrapped_pandas_method('__getitem__', key) @@ -552,23 +564,6 @@ class GeoSeries(Series): val = self.apply(_is_empty) return np.logical_or(non_geo_null, val) - def fillna(self, value=EMPTY_POLYGON, method=None, inplace=False, - **kwargs): - """Fill NA/NaN values with a geometry (empty polygon by default). - - "method" is currently not implemented for GeoSeries. - """ - if method is not None: - raise NotImplementedError('Fill method is currently not implemented for GeoSeries') - if isinstance(value, BaseGeometry): - result = self.copy() if not inplace else self - mask = self.isnull() - result[mask] = value - if not inplace: - return GeoSeries(result) - else: - raise ValueError('Non-geometric fill values not allowed for GeoSeries') - def align(self, other, join='outer', level=None, copy=True, fill_value=EMPTY_POLYGON, **kwargs): left, right = super(GeoSeries, self).align(other, join=join,