Skip to content

Commit 21007f2

Browse files
committed
API: Datetime/TimedeltaArray from to_datetime
Closes #24656
1 parent c4994ca commit 21007f2

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

pandas/core/arrays/array_.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from pandas._libs import lib, tslibs
22

3-
from pandas.core.dtypes.common import is_extension_array_dtype
3+
from pandas.core.dtypes.common import (
4+
is_datetime64_ns_dtype, is_extension_array_dtype, is_timedelta64_ns_dtype)
45
from pandas.core.dtypes.dtypes import registry
56

67
from pandas import compat
@@ -75,9 +76,10 @@ def array(data, # type: Sequence[object]
7576
See Also
7677
--------
7778
numpy.array : Construct a NumPy array.
78-
arrays.PandasArray : ExtensionArray wrapping a NumPy array.
7979
Series : Construct a pandas Series.
8080
Index : Construct a pandas Index.
81+
arrays.PandasArray : ExtensionArray wrapping a NumPy array.
82+
Series.array : Extract the array stored within a Series.
8183
8284
Notes
8385
-----
@@ -120,6 +122,16 @@ def array(data, # type: Sequence[object]
120122
['a', 'b']
121123
Length: 2, dtype: str32
122124
125+
Finally, Pandas has arrays that mostly overlap with NumPy
126+
127+
* :class:`arrays.DatetimeArray`
128+
* :class:`arrays.TimedeltaArray`
129+
130+
When data with a ``datetime64[ns]`` or ``timedelta64[ns]`` dtype is
131+
passed, pandas will always return a ``DatetimeArray`` or ``TimedeltaArray``
132+
rather than a ``PandasArray``. This is for symmetry with the case of
133+
timezone-aware data, which NumPy does not natively support.
134+
123135
Examples
124136
--------
125137
If a dtype is not specified, `data` is passed through to
@@ -239,5 +251,14 @@ def array(data, # type: Sequence[object]
239251

240252
# TODO(BooleanArray): handle this type
241253

254+
# Pandas overrides NumPy for
255+
# 1. datetime64[ns]
256+
# 2. timedelta64[ns]
257+
# so that a DatetimeArray is returned.
258+
if is_datetime64_ns_dtype(dtype):
259+
return DatetimeArray._from_sequence(data, dtype=dtype, copy=copy)
260+
if is_timedelta64_ns_dtype(dtype):
261+
return TimedeltaArray._from_sequence(data, dtype=dtype, copy=copy)
262+
242263
result = PandasArray._from_sequence(data, dtype=dtype, copy=copy)
243264
return result

pandas/tests/arrays/test_array.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,30 @@
3636
3737
# Datetime (naive)
3838
([1, 2], np.dtype('datetime64[ns]'),
39-
PandasArray(np.array([1, 2], dtype='datetime64[ns]'))),
40-
# TODO(DatetimeArray): add here
39+
pd.arrays.DatetimeArray._from_sequence(
40+
np.array([1, 2], dtype='datetime64[ns]'))),
41+
42+
(np.array([1, 2], dtype='datetime64[ns]'), None,
43+
pd.arrays.DatetimeArray._from_sequence(
44+
np.array([1, 2], dtype='datetime64[ns]'))),
45+
46+
(pd.DatetimeIndex(['2000', '2001']), np.dtype('datetime64[ns]'),
47+
pd.arrays.DatetimeArray._from_sequence(['2000', '2001'])),
48+
49+
(['2000', '2001'], np.dtype('datetime64[ns]'),
50+
pd.arrays.DatetimeArray._from_sequence(['2000', '2001'])),
51+
52+
# Datetime (tz-aware)
53+
(['2000', '2001'], pd.DatetimeTZDtype(tz="CET"),
54+
pd.arrays.DatetimeArray._from_sequence(
55+
['2000', '2001'], dtype=pd.DatetimeTZDtype(tz="CET"))),
56+
57+
# Timedelta
58+
(['1H', '2H'], np.dtype('timedelta64[ns]'),
59+
pd.arrays.TimedeltaArray._from_sequence(['1H', '2H'])),
60+
61+
(pd.TimedeltaIndex(['1H', '2H']), np.dtype('timedelta64[ns]'),
62+
pd.arrays.TimedeltaArray._from_sequence(['1H', '2H'])),
4163
4264
# Category
4365
(['a', 'b'], 'category', pd.Categorical(['a', 'b'])),

0 commit comments

Comments
 (0)