Skip to content

Accept PandasArray (with correct dtype) in DatetimeArray constructor #24615

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

I think it make sense that eg in the following case, we extract the numpy array out of a PandasArray, and thus accept it as an argument:

In [43]: pd.array([1, 2], dtype='datetime64[ns]')                       
Out[43]: 
<PandasArray>
[numpy.datetime64('1970-01-01T00:00:00.000000001'), numpy.datetime64('1970-01-01T00:00:00.000000002')]
Length: 2, dtype: datetime64[ns]

In [44]: pd.arrays.DatetimeArray(pd.array([1, 2], dtype='datetime64[ns]'))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-44-4cc26981aff2> in <module>
----> 1 pd.arrays.DatetimeArray(pd.array([1, 2], dtype='datetime64[ns]'))

~/scipy/pandas/pandas/core/arrays/datetimes.py in __init__(self, values, dtype, freq, copy)
    265                 "ndarray, or Series or Index containing one of those."
    266             )
--> 267             raise ValueError(msg.format(type(values).__name__))
    268 
    269         if values.dtype == 'i8':

ValueError: Unexpected type 'PandasArray'. 'values' must be a DatetimeArray ndarray, or Series or Index containing one of those.

There might be more similar cases like this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugConstructorsSeries/DataFrame/Index/pd.array ConstructorsDatetimeDatetime data dtypeExtensionArrayExtending pandas with custom dtypes or arrays.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions