|  | 
| 2 | 2 | from __future__ import division | 
| 3 | 3 | 
 | 
| 4 | 4 | from datetime import timedelta | 
|  | 5 | +import textwrap | 
| 5 | 6 | import warnings | 
| 6 | 7 | 
 | 
| 7 | 8 | import numpy as np | 
|  | 
| 15 | 16 | from pandas.util._decorators import Appender | 
| 16 | 17 | 
 | 
| 17 | 18 | from pandas.core.dtypes.common import ( | 
| 18 |  | - _NS_DTYPE, _TD_DTYPE, ensure_int64, is_datetime64_dtype, is_float_dtype, | 
| 19 |  | - is_integer_dtype, is_list_like, is_object_dtype, is_scalar, | 
|  | 19 | + _NS_DTYPE, _TD_DTYPE, ensure_int64, is_datetime64_dtype, is_dtype_equal, | 
|  | 20 | + is_float_dtype, is_integer_dtype, is_list_like, is_object_dtype, is_scalar, | 
| 20 | 21 |  is_string_dtype, is_timedelta64_dtype, is_timedelta64_ns_dtype, | 
| 21 | 22 |  pandas_dtype) | 
| 22 | 23 | from pandas.core.dtypes.dtypes import DatetimeTZDtype | 
| @@ -160,16 +161,8 @@ def __init__(self, values, dtype=_TD_DTYPE, freq=None, copy=False): | 
| 160 | 161 |  # nanosecond UTC (or tz-naive) unix timestamps | 
| 161 | 162 |  values = values.view(_TD_DTYPE) | 
| 162 | 163 | 
 | 
| 163 |  | - if values.dtype != _TD_DTYPE: | 
| 164 |  | - raise TypeError(_BAD_DTYPE.format(dtype=values.dtype)) | 
| 165 |  | - | 
| 166 |  | - try: | 
| 167 |  | - dtype_mismatch = dtype != _TD_DTYPE | 
| 168 |  | - except TypeError: | 
| 169 |  | - raise TypeError(_BAD_DTYPE.format(dtype=dtype)) | 
| 170 |  | - else: | 
| 171 |  | - if dtype_mismatch: | 
| 172 |  | - raise TypeError(_BAD_DTYPE.format(dtype=dtype)) | 
|  | 164 | + _validate_td64_dtype(values.dtype) | 
|  | 165 | + dtype = _validate_td64_dtype(dtype) | 
| 173 | 166 | 
 | 
| 174 | 167 |  if freq == "infer": | 
| 175 | 168 |  msg = ( | 
| @@ -204,9 +197,8 @@ def _simple_new(cls, values, freq=None, dtype=_TD_DTYPE): | 
| 204 | 197 |  @classmethod | 
| 205 | 198 |  def _from_sequence(cls, data, dtype=_TD_DTYPE, copy=False, | 
| 206 | 199 |  freq=None, unit=None): | 
| 207 |  | - if dtype != _TD_DTYPE: | 
| 208 |  | - raise ValueError("Only timedelta64[ns] dtype is valid.") | 
| 209 |  | - | 
|  | 200 | + if dtype: | 
|  | 201 | + _validate_td64_dtype(dtype) | 
| 210 | 202 |  freq, freq_infer = dtl.maybe_infer_freq(freq) | 
| 211 | 203 | 
 | 
| 212 | 204 |  data, inferred_freq = sequence_to_td64ns(data, copy=copy, unit=unit) | 
| @@ -997,6 +989,22 @@ def objects_to_td64ns(data, unit="ns", errors="raise"): | 
| 997 | 989 |  return result.view('timedelta64[ns]') | 
| 998 | 990 | 
 | 
| 999 | 991 | 
 | 
|  | 992 | +def _validate_td64_dtype(dtype): | 
|  | 993 | + dtype = pandas_dtype(dtype) | 
|  | 994 | + if is_dtype_equal(dtype, np.dtype("timedelta64")): | 
|  | 995 | + dtype = _TD_DTYPE | 
|  | 996 | + msg = textwrap.dedent("""\ | 
|  | 997 | + Passing in 'timedelta' dtype with no precision is deprecated | 
|  | 998 | + and will raise in a future version. Please pass in | 
|  | 999 | + 'timedelta64[ns]' instead.""") | 
|  | 1000 | + warnings.warn(msg, FutureWarning, stacklevel=4) | 
|  | 1001 | + | 
|  | 1002 | + if not is_dtype_equal(dtype, _TD_DTYPE): | 
|  | 1003 | + raise ValueError(_BAD_DTYPE.format(dtype=dtype)) | 
|  | 1004 | + | 
|  | 1005 | + return dtype | 
|  | 1006 | + | 
|  | 1007 | + | 
| 1000 | 1008 | def _generate_regular_range(start, end, periods, offset): | 
| 1001 | 1009 |  stride = offset.nanos | 
| 1002 | 1010 |  if periods is None: | 
|  | 
0 commit comments