|
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
|
@@ -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,30 @@ 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 | + try: |
| 994 | + if 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 | + except TypeError: |
| 1002 | + # extension dtype |
| 1003 | + pass |
| 1004 | + |
| 1005 | + try: |
| 1006 | + dtype_mismatch = dtype != _TD_DTYPE |
| 1007 | + except TypeError: |
| 1008 | + raise ValueError(_BAD_DTYPE.format(dtype=dtype)) |
| 1009 | + else: |
| 1010 | + if dtype_mismatch: |
| 1011 | + raise ValueError(_BAD_DTYPE.format(dtype=dtype)) |
| 1012 | + |
| 1013 | + return dtype |
| 1014 | + |
| 1015 | + |
1000 | 1016 | def _generate_regular_range(start, end, periods, offset):
|
1001 | 1017 | stride = offset.nanos
|
1002 | 1018 | if periods is None:
|
|
0 commit comments