Skip to content

Commit c896c7b

Browse files
committed
properly inherit from geo data frames
1 parent 1e547ae commit c896c7b

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

src/cartogram/cartogram.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ def _geodataframe_constructor_with_fallback(cls, *args, **kwargs):
4444
4545
Checks whether or not arguments of the child class are used.
4646
"""
47-
if "cartogram_attribute" in kwargs:
47+
if (
48+
"cartogram_attribute" in kwargs
49+
or isinstance(args[0], (str, pandas.Series))
50+
):
4851
df = cls(*args, **kwargs)
4952
else:
5053
df = geopandas.GeoDataFrame(*args, **kwargs)
@@ -54,10 +57,23 @@ def _geodataframe_constructor_with_fallback(cls, *args, **kwargs):
5457

5558
return df
5659

60+
_cartogram_attributes = [
61+
"cartogram_attribute",
62+
"max_iterations",
63+
"max_average_error",
64+
"verbose",
65+
]
66+
67+
def __setattr__(self, attr, val):
68+
"""Catch our own attributes here so we don’t mess with (geo)pandas columns."""
69+
if attr in self._cartogram_attributes:
70+
object.__setattr__(self, attr, val)
71+
else:
72+
super().__setattr__(attr, val)
73+
5774
def __init__(
5875
self,
5976
input_polygon_geodataframe,
60-
/,
6177
cartogram_attribute,
6278
max_iterations=10,
6379
max_average_error=0.1,

0 commit comments

Comments
 (0)