ENH: Support dissolve(by=None) as unary_union of all geoms
See original GitHub issueIn order to have one unique geometry to deal with, I’m trying to dissolve all many edges that compose a route using the .dissolve()
method on a GeoDataFrame instance but it throws me an error (while by=None
is the default value);
gdf_unique_route = gdf_route_edges.dissolve() # or gdf_route_edges.dissolve(by=None)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-160-808abd5a70f0> in <module>
----> 1 gdf_unique_route = gdf_route_edges.dissolve(by=None)
/usr/local/lib/python3.6/dist-packages/geopandas/geodataframe.py in dissolve(self, by, aggfunc, as_index)
684 # Process non-spatial component
685 data = self.drop(labels=self.geometry.name, axis=1)
--> 686 aggregated_data = data.groupby(by=by).agg(aggfunc)
687
688 # Process spatial component
/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in groupby(self, by, axis, level, as_index, sort, group_keys, squeeze, observed)
5796
5797 if level is None and by is None:
-> 5798 raise TypeError("You have to supply one of 'by' and 'level'")
5799 axis = self._get_axis_number(axis)
5800
TypeError: You have to supply one of 'by' and 'level'
If I use by='attribute1
it works, but I end up with a few geometries instead of only one.
I really want to dissolve them all, regardless of their attributes, so I used the default argument values.
Help on the .dissolve()
method is as follows:
Help on method dissolve in module geopandas.geodataframe:
dissolve(by=None, aggfunc='first', as_index=True) method of geopandas.geodataframe.GeoDataFrame instance
Dissolve geometries within `groupby` into single observation.
This is accomplished by applying the `unary_union` method
to all geometries within a groupself.
Observations associated with each `groupby` group will be aggregated
using the `aggfunc`.
Parameters
----------
by : string, default None
Column whose values define groups to be dissolved
aggfunc : function or string, default "first"
Aggregation function for manipulation of data associated
with each group. Passed to pandas `groupby.agg` method.
as_index : boolean, default True
If true, groupby columns become index of result.
Returns
-------
GeoDataFrame
There is no such level
arg in there, it’s something used by pandas DataFrame .groupy()
method actually (which you probably rely on).
As I was facing this error using GeoPandas so I’m reporting it here, but let me know if it should be reported to Pandas developers.
Thanks a lot!
Environment
Ubuntu 18.04
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
Numpy 1.18.2
Pandas 1.0.3
GeoPandas 0.7.0
Anything useful I missed?
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
It will be part of 0.9.0 release in a few weeks.
Another solution on our side would be to do
unary_union
automatically under the hood of noby
is specified. That makes more sense.