Skip to content

Commit 6d19c26

Browse files
fix column drop
1 parent df4082b commit 6d19c26

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

climada/hazard/centroids/centr.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -909,31 +909,33 @@ def write_hdf5(self, file_name, mode="w"):
909909
xycols = []
910910
wkbcols = []
911911
store = pd.HDFStore(file_name, mode=mode, complevel=9)
912-
pandas_df = pd.DataFrame(self.gdf)
913-
for col in pandas_df.columns:
914-
if str(pandas_df[col].dtype) == "geometry":
915-
try:
916-
pandas_df[col + ".x"] = self.gdf[col].x
917-
pandas_df[col + ".y"] = self.gdf[col].y
918-
pandas_df.drop(col, inplace=True)
919-
xycols.append(col)
920-
except ValueError:
921-
pandas_df[col] = self.gdf[col].to_wkb()
922-
wkbcols.append(col)
923-
924-
# Avoid pandas PerformanceWarning when writing HDF5 data
925-
with warnings.catch_warnings():
926-
warnings.simplefilter("ignore", category=pd.errors.PerformanceWarning)
927-
# Write dataframe
928-
store.put("centroids", pandas_df)
929-
930-
centroids_metadata = {"crs": CRS.from_user_input(self.crs).to_wkt()}
931-
if xycols:
932-
centroids_metadata["xy_columns"] = xycols
933-
if wkbcols:
934-
centroids_metadata["wkb_columns"] = wkbcols
935-
store.get_storer("centroids").attrs.metadata = centroids_metadata
936-
store.close()
912+
try:
913+
pandas_df = pd.DataFrame(self.gdf)
914+
for col in pandas_df.columns:
915+
if str(pandas_df[col].dtype) == "geometry":
916+
try:
917+
pandas_df[col + ".x"] = self.gdf[col].x
918+
pandas_df[col + ".y"] = self.gdf[col].y
919+
pandas_df.drop(columns=[col], inplace=True)
920+
xycols.append(col)
921+
except ValueError:
922+
pandas_df[col] = self.gdf[col].to_wkb()
923+
wkbcols.append(col)
924+
925+
# Avoid pandas PerformanceWarning when writing HDF5 data
926+
with warnings.catch_warnings():
927+
warnings.simplefilter("ignore", category=pd.errors.PerformanceWarning)
928+
# Write dataframe
929+
store.put("centroids", pandas_df)
930+
931+
centroids_metadata = {"crs": CRS.from_user_input(self.crs).to_wkt()}
932+
if xycols:
933+
centroids_metadata["xy_columns"] = xycols
934+
if wkbcols:
935+
centroids_metadata["wkb_columns"] = wkbcols
936+
store.get_storer("centroids").attrs.metadata = centroids_metadata
937+
finally:
938+
store.close()
937939

938940
@classmethod
939941
def from_hdf5(cls, file_name):
@@ -961,12 +963,12 @@ def from_hdf5(cls, file_name):
961963
# in previous versions of CLIMADA and/or geopandas,
962964
# the CRS was stored in '_crs'/'crs'
963965
crs = metadata.get("crs")
964-
gdf = gpd.GeoDataFrame(store["centroids"], crs=crs)
966+
gdf = gpd.GeoDataFrame(store["centroids"])
965967
for xycol in metadata.get("xy_columns", []):
966968
gdf[xycol] = gpd.points_from_xy(
967969
x=gdf[xycol + ".x"], y=gdf[xycol + ".y"], crs=crs
968970
)
969-
gdf.drop([xycol + ".x", xycol + ".y"], inplace=True)
971+
gdf.drop(columns=[xycol + ".x", xycol + ".y"], inplace=True)
970972
for wkbcol in metadata.get("wkb_columns", []):
971973
gdf[wkbcol] = gpd.GeoSeries.from_wkb(gdf[wkbcol], crs=crs)
972974

0 commit comments

Comments
 (0)