|
16 | 16 |
|
17 | 17 | from pandas.core.dtypes.common import (
|
18 | 18 | _INT64_DTYPE, _NS_DTYPE, ensure_int64, is_datetime64_dtype,
|
19 |
| - is_datetime64_ns_dtype, is_datetimetz, is_dtype_equal, is_float, |
20 |
| - is_integer, is_integer_dtype, is_list_like, is_period_dtype, is_scalar, |
21 |
| - is_string_like, pandas_dtype) |
| 19 | + is_datetime64_ns_dtype, is_datetime64tz_dtype, is_datetimetz, |
| 20 | + is_dtype_equal, is_float, is_integer, is_integer_dtype, is_list_like, |
| 21 | + is_period_dtype, is_scalar, is_string_like, pandas_dtype) |
22 | 22 | import pandas.core.dtypes.concat as _concat
|
23 | 23 | from pandas.core.dtypes.generic import ABCSeries
|
24 | 24 | from pandas.core.dtypes.missing import isna
|
25 | 25 |
|
26 | 26 | from pandas.core.arrays import datetimelike as dtl
|
27 | 27 | from pandas.core.arrays.datetimes import (
|
28 |
| - DatetimeArrayMixin as DatetimeArray, _to_m8) |
| 28 | + DatetimeArrayMixin as DatetimeArray, _to_m8, maybe_convert_dtype, |
| 29 | + maybe_infer_tz) |
29 | 30 | from pandas.core.base import _shared_docs
|
30 | 31 | import pandas.core.common as com
|
31 | 32 | from pandas.core.indexes.base import Index, _index_shared_docs
|
@@ -246,50 +247,49 @@ def __new__(cls, data=None,
|
246 | 247 | name = data.name
|
247 | 248 |
|
248 | 249 | freq, freq_infer = dtl.maybe_infer_freq(freq)
|
| 250 | + if freq is None and hasattr(data, "freq"): |
| 251 | + # i.e. DatetimeArray/Index |
| 252 | + freq = data.freq |
| 253 | + verify_integrity = False |
249 | 254 |
|
250 | 255 | # if dtype has an embedded tz, capture it
|
251 | 256 | tz = dtl.validate_tz_from_dtype(dtype, tz)
|
252 | 257 |
|
253 |
| - if not isinstance(data, (np.ndarray, Index, ABCSeries, DatetimeArray)): |
254 |
| - # other iterable of some kind |
255 |
| - if not isinstance(data, (list, tuple)): |
| 258 | + if not hasattr(data, "dtype"): |
| 259 | + # e.g. list, tuple |
| 260 | + if np.ndim(data) == 0: |
| 261 | + # i.e. generator |
256 | 262 | data = list(data)
|
257 |
| - data = np.asarray(data, dtype='O') |
| 263 | + data = np.asarray(data) |
| 264 | + copy = False |
258 | 265 | elif isinstance(data, ABCSeries):
|
259 | 266 | data = data._values
|
260 | 267 |
|
261 |
| - # data must be Index or np.ndarray here |
| 268 | + # By this point we are assured to have either a numpy array or Index |
| 269 | + data, copy = maybe_convert_dtype(data, copy) |
| 270 | + |
262 | 271 | if not (is_datetime64_dtype(data) or is_datetimetz(data) or
|
263 | 272 | is_integer_dtype(data) or lib.infer_dtype(data) == 'integer'):
|
264 | 273 | data = tools.to_datetime(data, dayfirst=dayfirst,
|
265 | 274 | yearfirst=yearfirst)
|
266 | 275 |
|
267 |
| - if isinstance(data, DatetimeArray): |
268 |
| - if tz is None: |
269 |
| - tz = data.tz |
270 |
| - elif data.tz is None: |
271 |
| - data = data.tz_localize(tz, ambiguous=ambiguous) |
272 |
| - else: |
273 |
| - # the tz's must match |
274 |
| - if not timezones.tz_compare(tz, data.tz): |
275 |
| - msg = ('data is already tz-aware {0}, unable to ' |
276 |
| - 'set specified tz: {1}') |
277 |
| - raise TypeError(msg.format(data.tz, tz)) |
278 |
| - |
| 276 | + if is_datetime64tz_dtype(data): |
| 277 | + tz = maybe_infer_tz(tz, data.tz) |
279 | 278 | subarr = data._data
|
280 | 279 |
|
281 |
| - if freq is None: |
282 |
| - freq = data.freq |
283 |
| - verify_integrity = False |
284 |
| - elif issubclass(data.dtype.type, np.datetime64): |
| 280 | + elif is_datetime64_dtype(data): |
| 281 | + # tz-naive DatetimeArray/Index or ndarray[datetime64] |
| 282 | + data = getattr(data, "_data", data) |
285 | 283 | if data.dtype != _NS_DTYPE:
|
286 | 284 | data = conversion.ensure_datetime64ns(data)
|
| 285 | + |
287 | 286 | if tz is not None:
|
288 | 287 | # Convert tz-naive to UTC
|
289 | 288 | tz = timezones.maybe_get_tz(tz)
|
290 | 289 | data = conversion.tz_localize_to_utc(data.view('i8'), tz,
|
291 | 290 | ambiguous=ambiguous)
|
292 | 291 | subarr = data.view(_NS_DTYPE)
|
| 292 | + |
293 | 293 | else:
|
294 | 294 | # must be integer dtype otherwise
|
295 | 295 | # assume this data are epoch timestamps
|
|
0 commit comments