Skip to content

ExtensionArray.astype docstring #24773

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

Open
andrewgsavage opened this issue Jan 14, 2019 · 1 comment
Open

ExtensionArray.astype docstring #24773

andrewgsavage opened this issue Jan 14, 2019 · 1 comment
Labels
Astype Docs ExtensionArray Extending pandas with custom dtypes or arrays.

Comments

@andrewgsavage
Copy link

def astype(self, dtype, copy=True):

Should the docstring say that this should return an ExtensionArray when an ExtensionType is passed into it?
I note that PeriodArray behaves like this.

import pandas as pd
a=pd.core.arrays.period_array([2000, 2001, 2002], freq='D')
type(a.dtype)

pandas.core.dtypes.dtypes.PeriodDtype

type(a.astype(a.dtype))

pandas.core.arrays.period.PeriodArray

@TomAugspurger
Copy link
Contributor

Yes, it should return an ExtensionArray when passed an ExtensionDtype.

This ends up making .astype quite complicated in practice. Pandas should provide some helpers here (though we had an issue for that, but couldn't quickly find one).

In the meantime, the EA docs / code should recommend a few things

  1. Use pandas_dtype(dtype) to convert strings like Period[D] to the ExtensionDtype or numba dtype
  2. For extension types, recommending dtype.construct_array_type()._from_sequence may make sense, but I'm not sure. That's supposed to take sequences of the scalar, so in theory it'll only work when you have to different ExtensionDtypes for the same scalar type. Not sure how common that'll be.

@TomAugspurger TomAugspurger added this to the Contributions Welcome milestone Jan 14, 2019
@mroeschke mroeschke added the ExtensionArray Extending pandas with custom dtypes or arrays. label Jun 25, 2021
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Astype Docs ExtensionArray Extending pandas with custom dtypes or arrays.
Projects
None yet
Development

No branches or pull requests

4 participants