Skip to content

Constructing a DatetimeArray with pd.array? #24656

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

I wanted to create a naive DatetimeArray with pd.array, but that doesn't seem very easy or even impossible:

Specyfing dtype='datetime64[ns]' gives a numpy backed PandasArray:

In [7]: pd.array(['2012-01-01', '2012-01-02'], dtype='datetime64[ns]')
Out[7]: 
<PandasArray>
[numpy.datetime64('2012-01-01T00:00:00.000000000'), numpy.datetime64('2012-01-02T00:00:00.000000000')]
Length: 2, dtype: datetime64[ns]

We also can't specify a custom dtype, as it needs a tz:

In [8]: pd.array(['2012-01-01', '2012-01-02'], dtype=pd.DatetimeTZDtype(tz=None))  
...
TypeError: A 'tz' is required.

Passing a DatetimeIndex also gives a PandasArray:

In [9]: pd.array(pd.date_range('2012-01-01', periods=3)) 
Out[9]: 
<PandasArray>
[numpy.datetime64('2012-01-01T00:00:00.000000000'),
 numpy.datetime64('2012-01-02T00:00:00.000000000'),
 numpy.datetime64('2012-01-03T00:00:00.000000000')]
Length: 3, dtype: datetime64[ns]

Should one of those give a DatetimeArray instead of PandasArray?

Metadata

Metadata

Assignees

No one assigned

    Labels

    ExtensionArrayExtending pandas with custom dtypes or arrays.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions