ENH: .astype() with dictionary while preserving type(self) #45676
Labels
Astype
Duplicate Report
Duplicate issue or pull request
Enhancement
Subclassing
Subclassing pandas objects
Is your feature request related to a problem?
[this should provide a description of what the problem is, e.g. "I wish I could use pandas to do [...]"]
I have a custom DataFrame class with
which allows me to use normal pandas methods and preserves my custom DataFrame object.
When using df.astype({'col':'type'}) the type returned is always a DataFrame instead of preserving the self._constructor and returning a CustomFrame
When using df.assign(col = col.astype('type')) the type returned is CustomFrame
Describe the solution you'd like
If we could change
result = result.__finalize__(self, method="astype")
to
result = self._constructor(results).__finalize__(self, method="astype")
then the constructor call would ensure the correct type is returned which is what is used in the col.astype('type') case
self._constructor(new_data).__finalize__(self, method="astype")
API breaking implications
I don't think this would break anything, the resulting objects in both cases are the same except for the type.
Describe alternatives you've considered
I could just use the assign pattern as a workaround but it would be nice if we could also use the dictionary notation
Additional context
# Your code here, if applicable
The text was updated successfully, but these errors were encountered: