-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DEPR/API: Non-ns precision in Index constructors #24806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
d856bb1
37d6779
ea31555
346f71c
a9a9588
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
from __future__ import division | ||
|
||
from datetime import timedelta | ||
import textwrap | ||
import warnings | ||
|
||
import numpy as np | ||
|
@@ -160,16 +161,8 @@ def __init__(self, values, dtype=_TD_DTYPE, freq=None, copy=False): | |
# nanosecond UTC (or tz-naive) unix timestamps | ||
values = values.view(_TD_DTYPE) | ||
|
||
if values.dtype != _TD_DTYPE: | ||
raise TypeError(_BAD_DTYPE.format(dtype=values.dtype)) | ||
|
||
try: | ||
dtype_mismatch = dtype != _TD_DTYPE | ||
except TypeError: | ||
raise TypeError(_BAD_DTYPE.format(dtype=dtype)) | ||
else: | ||
if dtype_mismatch: | ||
raise TypeError(_BAD_DTYPE.format(dtype=dtype)) | ||
_validate_td64_dtype(values.dtype) | ||
dtype = _validate_td64_dtype(dtype) | ||
|
||
if freq == "infer": | ||
msg = ( | ||
|
@@ -204,9 +197,8 @@ def _simple_new(cls, values, freq=None, dtype=_TD_DTYPE): | |
@classmethod | ||
def _from_sequence(cls, data, dtype=_TD_DTYPE, copy=False, | ||
freq=None, unit=None): | ||
if dtype != _TD_DTYPE: | ||
raise ValueError("Only timedelta64[ns] dtype is valid.") | ||
|
||
if dtype: | ||
_validate_td64_dtype(dtype) | ||
freq, freq_infer = dtl.maybe_infer_freq(freq) | ||
|
||
data, inferred_freq = sequence_to_td64ns(data, copy=copy, unit=unit) | ||
|
@@ -997,6 +989,30 @@ def objects_to_td64ns(data, unit="ns", errors="raise"): | |
return result.view('timedelta64[ns]') | ||
|
||
|
||
def _validate_td64_dtype(dtype): | ||
try: | ||
if dtype == np.dtype("timedelta64"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, use is_dtype_equal |
||
dtype = _TD_DTYPE | ||
msg = textwrap.dedent("""\ | ||
Passing in 'timedelta' dtype with no precision is deprecated | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
and will raise in a future version. Please pass in | ||
'timedelta64[ns]' instead.""") | ||
warnings.warn(msg, FutureWarning, stacklevel=4) | ||
except TypeError: | ||
# extension dtype | ||
pass | ||
|
||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can avoid all of this try/except/else if you use ``not is_dtype_equal(dtype, _TD_DTYPE) |
||
dtype_mismatch = dtype != _TD_DTYPE | ||
except TypeError: | ||
raise ValueError(_BAD_DTYPE.format(dtype=dtype)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note the change from TypeError to ValueError, when compared with https://github.com/pandas-dev/pandas/pull/24806/files#diff-57368e94ebd4c37c96935ab9862e3bc7L169. ValueError is more consistent with DatetimeArray. |
||
else: | ||
if dtype_mismatch: | ||
raise ValueError(_BAD_DTYPE.format(dtype=dtype)) | ||
|
||
return dtype | ||
|
||
|
||
def _generate_regular_range(start, end, periods, offset): | ||
stride = offset.nanos | ||
if periods is None: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do people think about this? In 0.23.4 we ignore the dtype precision
Should that raise now? Or should we deprecate first?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we actually can convert this (and I think we do?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
though I would not be averse to deprecating for consistency reasons
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do convert an array with non-ns precision dtype. That's similar to converting any sequence of datetime-like things
The issue is when the user says
DatetimeIndex(..., dtype='datetime64[us]')
. That's like saying "I want the dtype of the output DatetimeIndex to be datetime64[us]", which isn't supported.