python-geopandas/2298.patch
Benjamin A. Beasley 750a904925 Fix test failures with pandas 1.4
Backport upstream PR’s #2219, #2289, #2298, and #2320 in order to
resolve test failures with pandas 1.4.

It seems that PR#2320 may also be needed for pandas 1.3.5, which should
have been a compatible release.
2022-05-08 08:37:05 -04:00

116 lines
4 KiB
Diff

From 4121b08257d60cb1c9f504257ca2d8064461ea61 Mon Sep 17 00:00:00 2001
From: Joris Van den Bossche <jorisvandenbossche@gmail.com>
Date: Fri, 14 Jan 2022 10:58:10 +0100
Subject: [PATCH 1/3] TST: test groupby apply with function that requires
GeoDataFrame attributes
---
geopandas/tests/test_pandas_methods.py | 34 ++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/geopandas/tests/test_pandas_methods.py b/geopandas/tests/test_pandas_methods.py
index ed080af46..41db42809 100644
--- a/geopandas/tests/test_pandas_methods.py
+++ b/geopandas/tests/test_pandas_methods.py
@@ -558,6 +558,40 @@ def test_groupby_groups(df):
assert_frame_equal(res, exp)
+@pytest.mark.parametrize("crs", [None, "EPSG:4326"])
+def test_groupby_metadata(crs):
+ # https://github.com/geopandas/geopandas/issues/2294
+ df = GeoDataFrame(
+ {
+ "geometry": [Point(0, 0), Point(1, 1), Point(0, 0)],
+ "value1": np.arange(3, dtype="int64"),
+ "value2": np.array([1, 2, 1], dtype="int64"),
+ },
+ crs=crs,
+ )
+
+ # dummy test asserting we can access the crs
+ def func(group):
+ assert isinstance(group, GeoDataFrame)
+ assert group.crs == crs
+
+ df.groupby("value2").apply(func)
+
+ # actual test with functionality
+ res = df.groupby("value2").apply(
+ lambda x: geopandas.sjoin(x, x[["geometry", "value1"]], how="inner")
+ )
+
+ expected = (
+ df.take([0, 2, 0, 2, 1])
+ .set_index("value2", drop=False, append=True)
+ .swaplevel()
+ .rename(columns={"value1": "value1_left"})
+ .assign(value1_right=[0, 0, 2, 2, 1])
+ )
+ assert_geodataframe_equal(res.drop(columns=["index_right"]), expected)
+
+
def test_apply(s):
# function that returns geometry preserves GeoSeries class
def geom_func(geom):
From df3fc9ec5a241a01d76640404dcd0dab9b6ef434 Mon Sep 17 00:00:00 2001
From: Joris Van den Bossche <jorisvandenbossche@gmail.com>
Date: Mon, 17 Jan 2022 17:11:59 +0100
Subject: [PATCH 2/3] skip for pandas 1.3.5
---
geopandas/_compat.py | 1 +
geopandas/tests/test_pandas_methods.py | 4 ++++
2 files changed, 5 insertions(+)
diff --git a/geopandas/_compat.py b/geopandas/_compat.py
index 000db2a60..f233eee84 100644
--- a/geopandas/_compat.py
+++ b/geopandas/_compat.py
@@ -19,6 +19,7 @@
PANDAS_GE_11 = str(pd.__version__) >= LooseVersion("1.1.0")
PANDAS_GE_115 = str(pd.__version__) >= LooseVersion("1.1.5")
PANDAS_GE_12 = str(pd.__version__) >= LooseVersion("1.2.0")
+PANDAS_GE_13 = str(pd.__version__) >= LooseVersion("1.3.0")
PANDAS_GE_14 = str(pd.__version__) >= LooseVersion("1.4.0")
diff --git a/geopandas/tests/test_pandas_methods.py b/geopandas/tests/test_pandas_methods.py
index 41db42809..6c55e7ef6 100644
--- a/geopandas/tests/test_pandas_methods.py
+++ b/geopandas/tests/test_pandas_methods.py
@@ -558,6 +558,10 @@ def test_groupby_groups(df):
assert_frame_equal(res, exp)
+@pytest.mark.skipif(
+ compat.PANDAS_GE_13 and not compat.PANDAS_GE_14,
+ reason="this was broken in pandas 1.3.5",
+)
@pytest.mark.parametrize("crs", [None, "EPSG:4326"])
def test_groupby_metadata(crs):
# https://github.com/geopandas/geopandas/issues/2294
From d9a52c7945c254b313232739c7aaeed778a4f436 Mon Sep 17 00:00:00 2001
From: Joris Van den Bossche <jorisvandenbossche@gmail.com>
Date: Tue, 18 Jan 2022 00:47:59 +0100
Subject: [PATCH 3/3] add issue number in skip comment
---
geopandas/tests/test_pandas_methods.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/geopandas/tests/test_pandas_methods.py b/geopandas/tests/test_pandas_methods.py
index 6c55e7ef6..c6d04209b 100644
--- a/geopandas/tests/test_pandas_methods.py
+++ b/geopandas/tests/test_pandas_methods.py
@@ -560,7 +560,7 @@ def test_groupby_groups(df):
@pytest.mark.skipif(
compat.PANDAS_GE_13 and not compat.PANDAS_GE_14,
- reason="this was broken in pandas 1.3.5",
+ reason="this was broken in pandas 1.3.5 (GH-2294)",
)
@pytest.mark.parametrize("crs", [None, "EPSG:4326"])
def test_groupby_metadata(crs):