diff --git a/geopandas/base.py b/geopandas/base.py index ad31a6d..d426bfd 100644 --- a/geopandas/base.py +++ b/geopandas/base.py @@ -790,6 +790,34 @@ class GeoPandasBase(object): """ return _CoordinateIndexer(self) + def equals(self, other): + """ + Test whether two objects contain the same elements. + + This function allows two GeoSeries or GeoDataFrames to be compared + against each other to see if they have the same shape and elements. + Missing values in the same location are considered equal. The + row/column index do not need to have the same type (as long as the + values are still considered equal), but the dtypes of the respective + columns must be the same. + + Parameters + ---------- + other : GeoSeries or GeoDataFrame + The other GeoSeries or GeoDataFrame to be compared with the first. + + Returns + ------- + bool + True if all elements are the same in both objects, False + otherwise. + """ + # we override this because pandas is using `self._constructor` in the + # isinstance check (https://github.com/geopandas/geopandas/issues/1420) + if not isinstance(other, type(self)): + return False + return self._data.equals(other._data) + class _CoordinateIndexer(object): # see docstring GeoPandasBase.cx property above diff --git a/geopandas/tests/test_pandas_methods.py b/geopandas/tests/test_pandas_methods.py index 3318a19..7078331 100644 --- a/geopandas/tests/test_pandas_methods.py +++ b/geopandas/tests/test_pandas_methods.py @@ -314,6 +314,22 @@ def test_select_dtypes(df): assert_frame_equal(res, exp) +def test_equals(s, df): + # https://github.com/geopandas/geopandas/issues/1420 + s2 = s.copy() + assert s.equals(s2) is True + s2.iloc[0] = None + assert s.equals(s2) is False + + df2 = df.copy() + assert df.equals(df2) is True + df2.loc[0, "geometry"] = Point(10, 10) + assert df.equals(df2) is False + df2 = df.copy() + df2.loc[0, "value1"] = 10 + assert df.equals(df2) is False + + # Missing values @@ -460,7 +476,7 @@ def test_groupby(df): # apply on geometry column not resulting in new geometry res = df.groupby("value2")["geometry"].apply(lambda x: x.unary_union.area) - exp = pd.Series([0.0, 0.0], index=pd.Index([1, 2], name="value2"), name="geometry",) + exp = pd.Series([0.0, 0.0], index=pd.Index([1, 2], name="value2"), name="geometry") assert_series_equal(res, exp)