This is an automated email from the ASF dual-hosted git repository.

jiayu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sedona.git


The following commit(s) were added to refs/heads/master by this push:
     new 0a8dd86d9c [GH-1993] Geopandas __repr__() and to_geopandas() (#1990)
0a8dd86d9c is described below

commit 0a8dd86d9c907231bb40c00316f2b30e0c66f216
Author: Peter Nguyen <[email protected]>
AuthorDate: Fri Jun 20 09:44:16 2025 -0700

    [GH-1993] Geopandas __repr__() and to_geopandas() (#1990)
    
    * Implement to_geopandas and __repr__ for GeoSeries
    
    * Add check_geoseries_equal() to test_geoseries
    
    * Use wkb.loads instead of from_wkb and fix lint
    
    * Combine check_geoseries and check_geoseries_equal into one function
---
 python/sedona/geopandas/geoseries.py     | 32 ++++++++++++++++++++++++++------
 python/tests/geopandas/test_geoseries.py | 16 ++++++++++++----
 2 files changed, 38 insertions(+), 10 deletions(-)

diff --git a/python/sedona/geopandas/geoseries.py 
b/python/sedona/geopandas/geoseries.py
index db4962879c..e17e9817a9 100644
--- a/python/sedona/geopandas/geoseries.py
+++ b/python/sedona/geopandas/geoseries.py
@@ -28,6 +28,8 @@ from pyspark.pandas.series import first_series
 from pyspark.pandas.utils import scol_for
 from pyspark.sql.types import BinaryType
 
+import shapely
+
 from sedona.geopandas._typing import Label
 from sedona.geopandas.base import GeoFrame
 from sedona.geopandas.geodataframe import GeoDataFrame
@@ -43,6 +45,18 @@ class GeoSeries(GeoFrame, pspd.Series):
         # Implementation of the abstract method
         raise NotImplementedError("This method is not implemented yet.")
 
+    def __repr__(self) -> str:
+        """
+        Return a string representation of the GeoSeries in WKT format.
+        """
+        try:
+            pandas_series = self.to_geopandas()
+            return gpd.GeoSeries(pandas_series).__repr__()
+
+        except Exception as e:
+            # Fallback to parent's representation if conversion fails
+            return super().__repr__()
+
     def __init__(
         self,
         data=None,
@@ -206,13 +220,19 @@ class GeoSeries(GeoFrame, pspd.Series):
         # Implementation of the abstract method
         raise NotImplementedError("This method is not implemented yet.")
 
-    def to_geopandas(self) -> Union[gpd.GeoDataFrame, pd.Series]:
-        # Implementation of the abstract method
-        raise NotImplementedError("This method is not implemented yet.")
+    def to_geopandas(self) -> gpd.GeoSeries:
+        """
+        Convert the GeoSeries to a geopandas GeoSeries.
 
-    def _to_geopandas(self) -> Union[gpd.GeoDataFrame, pd.Series]:
-        # Implementation of the abstract method
-        raise NotImplementedError("This method is not implemented yet.")
+        Returns:
+        - geopandas.GeoSeries: A geopandas GeoSeries.
+        """
+        return self._to_geopandas()
+
+    def _to_geopandas(self) -> gpd.GeoSeries:
+        return gpd.GeoSeries(
+            self._to_internal_pandas().map(lambda wkb: 
shapely.wkb.loads(bytes(wkb)))
+        )
 
     @property
     def geometry(self) -> "GeoSeries":
diff --git a/python/tests/geopandas/test_geoseries.py 
b/python/tests/geopandas/test_geoseries.py
index 60776baa48..3f560262ea 100644
--- a/python/tests/geopandas/test_geoseries.py
+++ b/python/tests/geopandas/test_geoseries.py
@@ -17,6 +17,7 @@
 import os
 import shutil
 import tempfile
+from geopandas.testing import assert_geoseries_equal
 
 from shapely.geometry import (
     Point,
@@ -44,7 +45,7 @@ class TestSeries(TestBase):
 
     def test_constructor(self):
         s = GeoSeries([Point(x, x) for x in range(3)])
-        check_geoseries(s)
+        check_geoseries_equal(s, s)
 
     def test_psdf(self):
         # this is to make sure the spark session works with pandas on spark api
@@ -109,6 +110,13 @@ class TestSeries(TestBase):
 # -----------------------------------------------------------------------------
 
 
-def check_geoseries(s):
-    assert isinstance(s, GeoSeries)
-    assert isinstance(s.geometry, GeoSeries)
+def check_geoseries_equal(s1, s2):
+    assert isinstance(s1, GeoSeries)
+    assert isinstance(s1.geometry, GeoSeries)
+    assert isinstance(s2, GeoSeries)
+    assert isinstance(s2.geometry, GeoSeries)
+    if isinstance(s1, GeoSeries):
+        s1 = s1.to_geopandas()
+    if isinstance(s2, GeoSeries):
+        s2 = s2.to_geopandas()
+    assert_geoseries_equal(s1, s2)

Reply via email to