Repository: geopandas/geopandas

Issue:
GeoDataFrame explode with non-default active geometry name
I'm running into an issue when calling explode on a geodataframe with an active geometry named something other than "geometry". After looking through the source code, this seems to be because the active geometry is renamed to "geometry"; however, `_geometry_column_name` does not get updated.

Example:
```py
gdf = GeoDataFrame({"geom": [MultiPoint([(1, 2), (3, 4)])]}, geometry="geom", crs=4326)

# All good
print(gdf.geometry.name)
print(gdf._geometry_column_name)
print(gdf.columns)

gdf = gdf.explode(ignore_index=True)

# gdf.geometry.name != gdf._geometry_column_name
print(gdf.geometry.name)
print(gdf._geometry_column_name)
print(gdf.columns)

gdf.to_crs(crs=gdf.estimate_utm_crs(), inplace=True)

# gdf now has an unwanted geom column
print(gdf.geometry.name)
print(gdf._geometry_column_name)
print(gdf.columns)
```
A workaround is to call `gdf.set_geometry(gdf.geometry.name, inplace=True)` after the call to explode.
GeoDataFrame explode with non-default active geometry name
I'm running into an issue when calling explode on a geodataframe with an active geometry named something other than "geometry". After looking through the source code, this seems to be because the active geometry is renamed to "geometry"; however, `_geometry_column_name` does not get updated.

Example:
```py
gdf = GeoDataFrame({"geom": [MultiPoint([(1, 2), (3, 4)])]}, geometry="geom", crs=4326)

# All good
print(gdf.geometry.name)
print(gdf._geometry_column_name)
print(gdf.columns)

gdf = gdf.explode(ignore_index=True)

# gdf.geometry.name != gdf._geometry_column_name
print(gdf.geometry.name)
print(gdf._geometry_column_name)
print(gdf.columns)

gdf.to_crs(crs=gdf.estimate_utm_crs(), inplace=True)

# gdf now has an unwanted geom column
print(gdf.geometry.name)
print(gdf._geometry_column_name)
print(gdf.columns)
```
A workaround is to call `gdf.set_geometry(gdf.geometry.name, inplace=True)` after the call to explode.


Discussion:
Which version of geopandas do you have? Can you show the output of `geopandas.show_versions()`? I don't see the behaviour you describe on the main branch.
```
>>> geopandas.show_versions()

SYSTEM INFO
-----------
python     : 3.10.5 | packaged by conda-forge | (main, Jun 14 2022, 06:57:19) [MSC v.1929 64 bit (AMD64)]
executable : C:\Users\jpm5375\Miniconda3\envs\test-geopandas\python.exe
machine    : Windows-10-10.0.19044-SP0

GEOS, GDAL, PROJ INFO
---------------------
GEOS       : None
GEOS lib   : None
GDAL       : 3.5.1
GDAL data dir: None
PROJ       : 9.0.1
PROJ data dir: C:\Users\jpm5375\Miniconda3\envs\test-geopandas\Library\share\proj

PYTHON DEPENDENCIES
-------------------
geopandas  : 0.11.0
pandas     : 1.4.3
fiona      : 1.8.21
numpy      : 1.23.1
shapely    : 1.8.2
rtree      : 1.0.0
pyproj     : 3.3.1
matplotlib : 3.5.2
mapclassify: 2.4.3
geopy      : None
psycopg2   : None
geoalchemy2: None
pyarrow    : None
pygeos     : None
```
Installed as `conda create -n test-geopandas -c conda-forge geopandas`
Thanks! I actually wasn't on main (my bad). This is a regression introduced in 0.11.0, the older 0.10.2 works correctly.
Which version of geopandas do you have? Can you show the output of `geopandas.show_versions()`? I don't see the behaviour you describe on the main branch.
```
>>> geopandas.show_versions()

SYSTEM INFO
-----------
python     : 3.10.5 | packaged by conda-forge | (main, Jun 14 2022, 06:57:19) [MSC v.1929 64 bit (AMD64)]
executable : C:\Users\jpm5375\Miniconda3\envs\test-geopandas\python.exe
machine    : Windows-10-10.0.19044-SP0

GEOS, GDAL, PROJ INFO
---------------------
GEOS       : None
GEOS lib   : None
GDAL       : 3.5.1
GDAL data dir: None
PROJ       : 9.0.1
PROJ data dir: C:\Users\jpm5375\Miniconda3\envs\test-geopandas\Library\share\proj

PYTHON DEPENDENCIES
-------------------
geopandas  : 0.11.0
pandas     : 1.4.3
fiona      : 1.8.21
numpy      : 1.23.1
shapely    : 1.8.2
rtree      : 1.0.0
pyproj     : 3.3.1
matplotlib : 3.5.2
mapclassify: 2.4.3
geopy      : None
psycopg2   : None
geoalchemy2: None
pyarrow    : None
pygeos     : None
```
Installed as `conda create -n test-geopandas -c conda-forge geopandas`
Thanks! I actually wasn't on main (my bad). This is a regression introduced in 0.11.0, the older 0.10.2 works correctly.

Resolving patch:
diff --git a/geopandas/geodataframe.py b/geopandas/geodataframe.py
--- a/geopandas/geodataframe.py
+++ b/geopandas/geodataframe.py
@@ -1825,12 +1825,11 @@ def explode(self, column=None, ignore_index=False, index_parts=None, **kwargs):
 
         exploded_geom = self.geometry.reset_index(drop=True).explode(index_parts=True)
 
-        df = GeoDataFrame(
-            self.drop(self._geometry_column_name, axis=1).take(
-                exploded_geom.index.droplevel(-1)
-            ),
-            geometry=exploded_geom.values,
-        ).__finalize__(self)
+        df = self.drop(self._geometry_column_name, axis=1).take(
+            exploded_geom.index.droplevel(-1)
+        )
+        df[exploded_geom.name] = exploded_geom.values
+        df = df.set_geometry(self._geometry_column_name).__finalize__(self)
 
         if ignore_index:
             df.reset_index(inplace=True, drop=True)


Test patch:
diff --git a/geopandas/tests/test_geom_methods.py b/geopandas/tests/test_geom_methods.py
--- a/geopandas/tests/test_geom_methods.py
+++ b/geopandas/tests/test_geom_methods.py
@@ -1220,6 +1220,24 @@ def test_explode_duplicated_index(self):
         )
         assert_geodataframe_equal(test_df, expected_df)
 
+    @pytest.mark.parametrize("geom_col", ["geom", "geometry"])
+    def test_explode_geometry_name(self, geom_col):
+        s = GeoSeries([MultiPoint([Point(1, 2), Point(2, 3)]), Point(5, 5)])
+        df = GeoDataFrame({"col": [1, 2], geom_col: s}, geometry=geom_col)
+        test_df = df.explode(index_parts=True)
+
+        assert test_df.geometry.name == geom_col
+        assert test_df.geometry.name == test_df._geometry_column_name
+
+    def test_explode_geometry_name_two_geoms(self):
+        s = GeoSeries([MultiPoint([Point(1, 2), Point(2, 3)]), Point(5, 5)])
+        df = GeoDataFrame({"col": [1, 2], "geom": s, "geometry": s}, geometry="geom")
+        test_df = df.explode(index_parts=True)
+
+        assert test_df.geometry.name == "geom"
+        assert test_df.geometry.name == test_df._geometry_column_name
+        assert "geometry" in test_df.columns
+
     #
     # Test '&', '|', '^', and '-'
     #