BUG: override the .equals() method from pandas to correct isinstance (#1451)

This commit is contained in:
Joris Van den Bossche authored and GitHub committed 2020-05-31 17:01:39 +02:00
1 parent 8f4889affd
commit 07a8f4cc7e
2 files changed
+45 -1

No files matched your search

+28
View File
@@ -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
+17 -1
View File
@@ -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)