Repository: geopandas/geopandas
Pull request #3000 (2023-09-03 07:53:44)

TST: update groupby metadata test for pandas 2.1

jorisvandenbossche: Thanks! 
I think `geopandas/tools/tests/test_sjoin.py::TestNearest::test_sjoin_nearest_exclusive[None-expected0]` will need a similar change?
m-richards: I think this now works, final dev failures are just astype and the EA null dtype tests

Patch:
diff --git a/geopandas/_compat.py b/geopandas/_compat.py
index b6264aa9..4f28d1f0 100644
--- a/geopandas/_compat.py
+++ b/geopandas/_compat.py
@@ -14,6 +14,7 @@ PANDAS_GE_14 = Version(pd.__version__) >= Version("1.4.0rc0")
 PANDAS_GE_15 = Version(pd.__version__) >= Version("1.5.0")
 PANDAS_GE_20 = Version(pd.__version__) >= Version("2.0.0")
 PANDAS_GE_21 = Version(pd.__version__) >= Version("2.1.0")
+PANDAS_GE_22 = Version(pd.__version__) >= Version("2.2.0.dev0")
 
 
 # -----------------------------------------------------------------------------


Test patch:
diff --git a/ci/envs/39-latest-conda-forge_no_fiona.yaml b/ci/envs/39-latest-conda-forge_no_fiona.yaml
index abbc60e2..a81986f1 100644
--- a/ci/envs/39-latest-conda-forge_no_fiona.yaml
+++ b/ci/envs/39-latest-conda-forge_no_fiona.yaml
@@ -16,7 +16,7 @@ dependencies:
   - pytest-xdist
   # - fsspec  # to have one non-minimal build without fsspec
   # optional
-  - matplotlib
+  - matplotlib>=3.6
   - mapclassify
   - folium
   - xyzservices
diff --git a/geopandas/tests/test_pandas_methods.py b/geopandas/tests/test_pandas_methods.py
index 1437944d..5a0a66ef 100644
--- a/geopandas/tests/test_pandas_methods.py
+++ b/geopandas/tests/test_pandas_methods.py
@@ -251,7 +251,10 @@ def test_astype(s, df):
     assert s.astype(str)[0] == "POINT (0 0)"
 
     res = s.astype(object)
-    if not Version(pd.__version__) == Version("2.1.0"):
+    if not (
+        (Version(pd.__version__) == Version("2.1.0"))
+        or (Version(pd.__version__) == Version("2.1.1"))
+    ):
         # https://github.com/geopandas/geopandas/issues/2948 - bug in pandas 2.1.0
         assert isinstance(res, pd.Series) and not isinstance(res, GeoSeries)
         assert res.dtype == object
@@ -677,12 +680,20 @@ def test_groupby_metadata(crs):
         lambda x: geopandas.sjoin(x, x[["geometry", "value1"]], how="inner")
     )
 
+    if compat.PANDAS_GE_22:
+        # merge sort behaviour changed in pandas #54611
+        take_indices = [0, 0, 2, 2, 1]
+        value_right = [0, 2, 0, 2, 1]
+    else:
+        take_indices = [0, 2, 0, 2, 1]
+        value_right = [0, 0, 2, 2, 1]
+
     expected = (
-        df.take([0, 2, 0, 2, 1])
+        df.take(take_indices)
         .set_index("value2", drop=False, append=True)
         .swaplevel()
         .rename(columns={"value1": "value1_left"})
-        .assign(value1_right=[0, 0, 2, 2, 1])
+        .assign(value1_right=value_right)
     )
     assert_geodataframe_equal(res.drop(columns=["index_right"]), expected)
 
diff --git a/geopandas/tools/tests/test_sjoin.py b/geopandas/tools/tests/test_sjoin.py
index 39614f54..9e7a1ccf 100644
--- a/geopandas/tools/tests/test_sjoin.py
+++ b/geopandas/tools/tests/test_sjoin.py
@@ -8,6 +8,7 @@ import shapely
 from shapely.geometry import Point, Polygon, GeometryCollection
 
 import geopandas
+import geopandas._compat as compat
 from geopandas import GeoDataFrame, GeoSeries, read_file, sjoin, sjoin_nearest
 from geopandas.testing import assert_geodataframe_equal, assert_geoseries_equal
 
@@ -898,8 +899,12 @@ class TestNearest:
         result5["index_right"] = result5["index_right"].astype("int64")
         assert_geodataframe_equal(result5, result4, check_like=True)
 
+    expected_index_uncapped = (
+        [1, 3, 3, 1, 2] if compat.PANDAS_GE_22 else [1, 1, 3, 3, 2]
+    )
+
     @pytest.mark.parametrize(
-        "max_distance,expected", [(None, [1, 1, 3, 3, 2]), (1.1, [3, 3, 1, 2])]
+        "max_distance,expected", [(None, expected_index_uncapped), (1.1, [3, 3, 1, 2])]
     )
     def test_sjoin_nearest_exclusive(self, max_distance, expected):
         geoms = shapely.points(np.arange(3), np.arange(3))