question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: 'GeometryArray' object has no attribute 'value_counts'

See original GitHub issue
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of geopandas.

  • (optional) I have confirmed this bug exists on the master branch of geopandas.


Code Sample, a copy-pastable example

gdf["geometry"].value_counts()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-25-fb6329993e01> in <module>
----> 1 gdf["geometry"].value_counts()

C:\tools\miniconda3\envs\project-dev\lib\site-packages\pandas\core\base.py in value_counts(self, normalize, sort, ascending, bins, dropna)
   1243         dtype: int64
   1244         """
-> 1245         result = value_counts(
   1246             self,
   1247             sort=sort,

C:\tools\miniconda3\envs\project-dev\lib\site-packages\pandas\core\algorithms.py in value_counts(values, sort, ascending, normalize, bins, dropna)
    790 
    791             # handle Categorical and sparse,
--> 792             result = Series(values)._values.value_counts(dropna=dropna)
    793             result.name = name
    794             counts = result._values

AttributeError: 'GeometryArray' object has no attribute 'value_counts'

Problem description

The “geometry” columns of a geopandas data frame should act like a regular pandas Series. This includes supporting “value_counts”.

I can’t even monkey-patch it with something that uses collections.Counter since the geometry types are unhashable, i.e. the following also fails:

import collections
pd.Series(collections.Counter(gdf["geometry"]), name="geometry")

Expected Output

Same as pd.Series.value_counts(*args, **kwargs)

Output of geopandas.show_versions()

SYSTEM INFO

python : 3.8.6 | packaged by conda-forge | (default, Nov 27 2020, 18:58:29) [MSC v.1916 64 bit (AMD64)] executable : C:\tools\miniconda3\envs\project-dev\python.exe machine : Windows-10-10.0.18362-SP0

GEOS, GDAL, PROJ INFO

GEOS : None GEOS lib : None GDAL : 3.1.4 GDAL data dir: None PROJ : 7.1.1 PROJ data dir: C:\tools\miniconda3\envs\project-dev\Library\share\proj

PYTHON DEPENDENCIES

geopandas : 0.8.1 pandas : 1.1.5 fiona : 1.8.18 numpy : 1.19.4 shapely : 1.7.1 rtree : 0.9.4 pyproj : 2.6.1.post1 matplotlib : 3.3.3 mapclassify: 2.3.0 geopy : 2.0.0 psycopg2 : None geoalchemy2: None pyarrow : 2.0.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
m-richardscommented, Aug 2, 2021

I think I’ll work on this as well, seems pretty straightforward and nice to have.

1reaction
jorisvandenbosschecommented, Jan 8, 2021

Would it be simple to do something like convert the geometry series to into a regular pandas binary series using WKB, then use regular value_counts and then convert the index back to a geometry type?

Yes, that would be the idea. Something like this (untested code), method on GeometryArray:

def value_counts(self, ...):
    values = to_wkb(self)
    from pandas import Series, Index
    result = Series(values).value_counts(...)
    result.index = Index(from_wkb(result.index))
Read more comments on GitHub >

github_iconTop Results From Across the Web

'DataFrame' object has no attribute 'value_counts' Why is it ...
Why is the error occurring? Why does it change from Series to DataFrame? Answer: As per the docs, extract returns DataFrame by default...
Read more >
DataFrame' object has no attribute 'value_counts' in pandas ...
Coding example for the question 'DataFrame' object has no attribute 'value_counts' in pandas 0.25-Pandas,Python.
Read more >
geopandas.GeoDataFrame
Series object designed to store shapely geometry objects. Examples ... Constructing GeoDataFrame from a pandas DataFrame with a column of WKT geometries:.
Read more >
pandas.Series.value_counts — pandas 1.5.2 documentation
Return a Series containing counts of unique values. The resulting object will be in descending order so that the first element is the...
Read more >
geopandas Changelog - pyup.io
Small bug-fix release removing the shapely<2 pin in the installation ... Combining GeoSeries/GeoDataFrames with ``pandas.concat`` will no longer silently
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found