API: astype mechanism for extension arrays · Issue #22384 · pandas-dev/pandas (original) (raw)
Navigation Menu
- Explore
- Pricing
Provide feedback
Saved searches
Use saved searches to filter your results more quickly
Appearance settings
Description
In #22343, we're bumping into a few problems around casting to / from extension arrays.
- Our current default implementation for
ExtensionArray.astype
doesn't handle targetdtype
s that are extension types (soextension_array.astype('category')
fails). At the moment, each EA will have to implement their own astyping, which is error prone and difficult. - Some EAs may be more opinionated than others about how astyping should be done. There may exist fastpaths between certain EAs, but only one of the source and destination types may know about that fast path. This (I think) calls for a kind of dispatch mechanism, where each gets to say whether they know how to handle the astyping.
I'm not sure how far down this road we want to go. Should we instead encorage users to use explicit constructors like .assign(x=pd.Categorical(...)
rather than .astype('category')
?