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
This commit is contained in:
Kelsey Jordahl
2013-10-30 22:29:58 -04:00
parent 1b77106b0d
commit fa48e48069
2 changed files with 19 additions and 17 deletions
+7
View File
@@ -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)
+12 -17
View File
@@ -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,