|
1 | 1 | from pandas._libs import lib, tslibs
|
2 | 2 |
|
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) |
4 | 5 | from pandas.core.dtypes.dtypes import registry
|
5 | 6 |
|
6 | 7 | from pandas import compat
|
@@ -75,9 +76,10 @@ def array(data, # type: Sequence[object]
|
75 | 76 | See Also
|
76 | 77 | --------
|
77 | 78 | numpy.array : Construct a NumPy array.
|
78 |
| - arrays.PandasArray : ExtensionArray wrapping a NumPy array. |
79 | 79 | Series : Construct a pandas Series.
|
80 | 80 | Index : Construct a pandas Index.
|
| 81 | + arrays.PandasArray : ExtensionArray wrapping a NumPy array. |
| 82 | + Series.array : Extract the array stored within a Series. |
81 | 83 |
|
82 | 84 | Notes
|
83 | 85 | -----
|
@@ -120,6 +122,26 @@ def array(data, # type: Sequence[object]
|
120 | 122 | ['a', 'b']
|
121 | 123 | Length: 2, dtype: str32
|
122 | 124 |
|
| 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 | +
|
| 135 | + >>> pd.array(['2015', '2016'], dtype='datetime64[ns]') |
| 136 | + <DatetimeArray> |
| 137 | + ['2015-01-01 00:00:00', '2016-01-01 00:00:00'] |
| 138 | + Length: 2, dtype: datetime64[ns] |
| 139 | +
|
| 140 | + >>> pd.array(["1H", "2H"], dtype='timedelta64[ns]') |
| 141 | + <TimedeltaArray> |
| 142 | + ['01:00:00', '02:00:00'] |
| 143 | + Length: 2, dtype: timedelta64[ns] |
| 144 | +
|
123 | 145 | Examples
|
124 | 146 | --------
|
125 | 147 | If a dtype is not specified, `data` is passed through to
|
@@ -239,5 +261,14 @@ def array(data, # type: Sequence[object]
|
239 | 261 |
|
240 | 262 | # TODO(BooleanArray): handle this type
|
241 | 263 |
|
| 264 | + # Pandas overrides NumPy for |
| 265 | + # 1. datetime64[ns] |
| 266 | + # 2. timedelta64[ns] |
| 267 | + # so that a DatetimeArray is returned. |
| 268 | + if is_datetime64_ns_dtype(dtype): |
| 269 | + return DatetimeArray._from_sequence(data, dtype=dtype, copy=copy) |
| 270 | + elif is_timedelta64_ns_dtype(dtype): |
| 271 | + return TimedeltaArray._from_sequence(data, dtype=dtype, copy=copy) |
| 272 | + |
242 | 273 | result = PandasArray._from_sequence(data, dtype=dtype, copy=copy)
|
243 | 274 | return result
|
0 commit comments