Skip to content

ENH: More permissable conversion to StringDtype #33412

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

Closed
topper-123 opened this issue Apr 9, 2020 · 2 comments
Closed

ENH: More permissable conversion to StringDtype #33412

topper-123 opened this issue Apr 9, 2020 · 2 comments

Comments

@topper-123
Copy link
Contributor

Currently, converting directly to StringDtype is not really safe unless you know you've got a str type:

>>> pd.Series([1,2, np.nan], dtype=str)[0]
'1'  # string, works
>>> pd.Series([1,2, np.nan], dtype="string")
ValueError  # does not work
>>> pd.Series([1,2, np.nan], dtype=str).astype("string")
# works
>>> pd.Series([1,2, np.nan]).astype("string")
ValueError  # does not work
>>> pd.Series([1,2, np.nan]).astype(str).astype("string")
# works

The net result is that we have to wrap conversions, so in general will have to do x.astype(str).astype("string"), which seems unnecessary.

This extends to other conversions to StringDtype, e.g. for read_excel we have to do:

>>> df = pd.read_excel(..., dtype={"col_x": str}).astype({"col_x": "string"})

where directly setting dtype={"col_x": "string"} would seem more natural.

I think we should be able to convert directly to StringDtype by internally converting first to str, if needed. I might be missing some nuance why this is not allowed, though.

Any comment on loosening up here?

@topper-123 topper-123 added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member API Design and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 9, 2020
@jorisvandenbossche
Copy link
Member

jorisvandenbossche commented Apr 9, 2020

This might be a duplicate of #31204, at least the astype("string") is discussed there.

And I suppose that if astype("string") works, doing pd.Series(.., dtype="string") should work similarly?

@topper-123
Copy link
Contributor Author

Yeah, it’s similar. I’ll close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants