Skip to content

Should I be able to initialise a Series with a pandas array and pandas dtype? #32028

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
MarcoGorelli opened this issue Feb 15, 2020 · 3 comments
Labels
Bug Constructors Series/DataFrame/Index/pd.array Constructors ExtensionArray Extending pandas with custom dtypes or arrays.

Comments

@MarcoGorelli
Copy link
Member

Code Sample, a copy-pastable example if possible

>>> import pandas as pd
>>> from pandas.core.arrays import PandasDtype
>>> pd.Series(pd.array([1,2,3]), dtype=PandasDtype('int64'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/marco/pandas/pandas/core/series.py", line 323, in __init__
    data = sanitize_array(data, index, dtype, copy, raise_cast_failure=True)
  File "/home/marco/pandas/pandas/core/construction.py", line 431, in sanitize_array
    subarr = subarr.astype(dtype, copy=copy)
  File "/home/marco/pandas/pandas/core/arrays/integer.py", line 475, in astype
    data = self.to_numpy(dtype=dtype, **kwargs)
  File "/home/marco/pandas/pandas/core/arrays/masked.py", line 144, in to_numpy
    data = self._data.astype(dtype, copy=copy)
TypeError: data type not understood

Output of pd.show_versions()

(something's wrong here)

INSTALLED VERSIONS

{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}

{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}
{k:<17}: {stat}

@jorisvandenbossche
Copy link
Member

PandasDtype is not really meant for direct public use (they are also never actually stored in a Series, only created when we need to return an ExtensionArray).

But in general, yes, you should be able to use such arrays and dtypes. Eg this works:

In [1]: pd.Series(pd.array([1,2,3]), dtype=pd.Int16Dtype()) 
Out[1]: 
0    1
1    2
2    3
dtype: Int16

@jorisvandenbossche
Copy link
Member

But so there are still several missing things in astype support for extension arrays, eg #22384, #31204, ..

@jbrockmendel jbrockmendel added the Constructors Series/DataFrame/Index/pd.array Constructors label Feb 25, 2020
@mroeschke mroeschke added Bug ExtensionArray Extending pandas with custom dtypes or arrays. labels Jun 28, 2020
@jorisvandenbossche
Copy link
Member

This has been fixed in the meantime, and we now "unpack" a PandasArray/PandasDtype it its plain numpy array/dtype when passed to the Series constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Constructors Series/DataFrame/Index/pd.array Constructors ExtensionArray Extending pandas with custom dtypes or arrays.
Projects
None yet
Development

No branches or pull requests

4 participants