Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REGR: using loc to add rows doesn't preserve dtypes #3146

Closed
wants to merge 3 commits into from

Conversation

m-richards
Copy link
Member

Closes #3119. Alternative to #3125 which I think will not go anywhere.

This is also not an ideal solution.

@m-richards
Copy link
Member Author

Also not ideal, the "upcast by default" assumption is not great for astype(object), and potentially other things.

Comment on lines +1600 to +1610
df = pd.DataFrame._from_mgr(mgr, axes)
# operations like loc to append an object row will cast GeometryDtype
# blocks to object dtype. Try and cast back
# TODO relying on self here is not ideal pandas#56681
for c in self.columns[self.dtypes == "geometry"].intersection(df.columns):
df[c] = _attempt_ensure_geometry(df[c], crs=self[c].crs)
if self._geometry_column_name in df.columns:
try:
df = df.set_geometry(self._geometry_column_name, crs=self.crs)
except TypeError:
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a shorter term fix (to just fix the regression for the common case of a "geometry" named columns, and not the longer standing bug), could we just check if the result has a column called "geometry" and in that case try to convert it?

As a way to have something smaller to backport for 0.14.x

@m-richards m-richards closed this Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

REGR: adding a row to a GeoDataFrame downcasts GeoSeries to Series
2 participants