Skip to content

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

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
jorisvandenbossche opened this issue Jan 4, 2019 · 2 comments · Fixed by #40210
Closed

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

jorisvandenbossche opened this issue Jan 4, 2019 · 2 comments · Fixed by #40210
Labels
Bug Constructors Series/DataFrame/Index/pd.array Constructors Datetime Datetime data dtype ExtensionArray Extending pandas with custom dtypes or arrays.
Milestone

Comments

@jorisvandenbossche
Copy link
Member

jorisvandenbossche commented Jan 4, 2019

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.

@gfyoung gfyoung added the Datetime Datetime data dtype label Jan 9, 2019
@jbrockmendel jbrockmendel added the Constructors Series/DataFrame/Index/pd.array Constructors label Jul 23, 2019
@mroeschke
Copy link
Member

This looks supported on master now. Could use a test?

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

In [129]: In [44]: pd.arrays.DatetimeArray(pd.array([1, 2], dtype='datetime64[ns]'))
     ...:
Out[129]:
<DatetimeArray>
['1970-01-01 00:00:00.000000001', '1970-01-01 00:00:00.000000002']
Length: 2, dtype: datetime64[ns]

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Constructors Series/DataFrame/Index/pd.array Constructors Enhancement Datetime Datetime data dtype labels Apr 1, 2020
@jorisvandenbossche
Copy link
Member Author

The initial example isn't really valid anymore, as pd.array(.., dtype="datetime64[ns]") is apparently not creating a PandasArray anymore, but a DatetimeArray.

So the original issue still fails:

In [16]: a = pd.arrays.PandasArray(np.array([1, 2, 3], dtype="datetime64[ns]"))                                                                                                                                    

In [17]: a                                                                                                                                                                                                         
Out[17]: 
<PandasArray>
[numpy.datetime64('1970-01-01T00:00:00.000000001'),
 numpy.datetime64('1970-01-01T00:00:00.000000002'),
 numpy.datetime64('1970-01-01T00:00:00.000000003')]
Length: 3, dtype: datetime64[ns]

In [18]: pd.arrays.DatetimeArray(a)                                                                                                                                                                                
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-98567534d92b> in <module>
----> 1 pd.arrays.DatetimeArray(a)

~/scipy/pandas/pandas/core/arrays/datetimes.py in __init__(self, values, dtype, freq, copy)
    237         if not isinstance(values, np.ndarray):
    238             raise ValueError(
--> 239                 f"Unexpected type '{type(values).__name__}'. 'values' must be "
    240                 "a DatetimeArray ndarray, or Series or Index containing one of those."
    241             )

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

(I am not sure it is the most important issue to support though)

@mroeschke mroeschke added Bug ExtensionArray Extending pandas with custom dtypes or arrays. Datetime Datetime data dtype and removed Needs Tests Unit test(s) needed to prevent regressions good first issue labels Apr 1, 2020
@jbrockmendel jbrockmendel added the Constructors Series/DataFrame/Index/pd.array Constructors label Dec 16, 2020
@jreback jreback added this to the 1.3 milestone Mar 16, 2021
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 Datetime Datetime data dtype ExtensionArray Extending pandas with custom dtypes or arrays.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants