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

Series[EA].astype always casts intermediate to NumPy array #21296

Closed
xhochy opened this issue Jun 2, 2018 · 2 comments · Fixed by #22343
Closed

Series[EA].astype always casts intermediate to NumPy array #21296

xhochy opened this issue Jun 2, 2018 · 2 comments · Fixed by #22343
Labels
ExtensionArray Extending pandas with custom dtypes or arrays.
Milestone

Comments

@xhochy
Copy link
Contributor

xhochy commented Jun 2, 2018

Calling pd.Series(EA).astype(object) will always generate an intermediate NumPy array and never delegate to the astype method of the ExtensionArray.

Callstack is as follows:

  1. pd.Series(EA).astype()
  2. (omitting some intermediates)
  3. pandas.core.internals.Block._astype calls self.get_values() in
    values = self.get_values(dtype=dtype)

    (introducted by https://github.com/pandas-dev/pandas/pull/20581/files)
  4. pandas.core.internals.ExtensionBlock.get_values then casts the ExtensionArray to a numpy.array:
    def get_values(self, dtype=None):

Expected behaviour would have been that astype is called on the ExtensionArray which then can do the casting on its own. Currently I have the problem that my underlying storage (ExtensionArray backed by Arrow) is not numpy-compatible and thus everything turns into np.array(…, dtype=object) before it is casted.

Happy to fix this on my own but I would need a pointer on what the correct approach is, i.e. where one should delegate to ExtensionArray.astype.

Output of pd.show_versions()

Ran into this with 0.23.0 but the code has not changed in master in this area.

INSTALLED VERSIONS                                                                                                                                    [6/1805]
------------------
commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-112-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: de_DE.UTF-8
LOCALE: de_DE.UTF-8

pandas: 0.23.0
pytest: 3.6.0
pip: 9.0.3
setuptools: 39.2.0
Cython: None
numpy: 1.14.2
scipy: None
pyarrow: 0.9.0
xarray: None
IPython: 6.4.0
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
@jreback
Copy link
Contributor

jreback commented Jun 3, 2018

this would be fixed by #21185

give a try there (will be merging to master somewhat soon)

@jreback jreback added the ExtensionArray Extending pandas with custom dtypes or arrays. label Jun 3, 2018
@jorisvandenbossche
Copy link
Member

I don't think this will fully fixed by #21185. #21185 will fix it for the case of pd.Series(..).astype(EAtype), but not for the case of pd.Series(EA).astype(numpy_type)

Happy to fix this on my own but I would need a pointer on what the correct approach is, i.e. where one should delegate to ExtensionArray.astype.

I think a fix in pandas.core.internals.Block._astype is probably appropriate (to ensure get_values is not called).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ExtensionArray Extending pandas with custom dtypes or arrays.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants