-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
REF/DEPR: DatetimeIndex constructor #23675
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 10 commits
f8efbef
9d20bc9
aef3f4c
66ae42b
d0e8ee3
e1f4e17
d18e0df
a4c8c77
5dc5980
7e5587e
e94e826
7464d15
80b5dbe
3c822f1
ba7e5e8
3ba9da7
49c11e1
f1d3fd8
d44055e
1471a2b
11b5f6c
9f56d23
1c3a5aa
be4d472
145772d
7c99105
6b60da2
a7038bb
14d923b
c9dbf24
ce9914d
b3d5bb7
09c88fc
0367d6f
7cc8577
b3a096b
fd5af18
2cdd215
782ca81
03d5b35
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 |
---|---|---|
|
@@ -16,10 +16,11 @@ | |
from pandas.util._decorators import Appender, Substitution, cache_readonly | ||
|
||
from pandas.core.dtypes.common import ( | ||
_INT64_DTYPE, _NS_DTYPE, ensure_int64, is_datetime64_dtype, | ||
is_datetime64_ns_dtype, is_datetimetz, is_dtype_equal, is_float, | ||
is_integer, is_integer_dtype, is_list_like, is_period_dtype, is_scalar, | ||
is_string_like, pandas_dtype) | ||
_INT64_DTYPE, _NS_DTYPE, ensure_int64, is_datetime64_ns_dtype, | ||
is_datetime64tz_dtype, is_dtype_equal, is_extension_type, is_float, | ||
is_float_dtype, is_integer, is_list_like, is_object_dtype, is_period_dtype, | ||
is_scalar, is_string_dtype, is_string_like, is_timedelta64_dtype, | ||
pandas_dtype) | ||
import pandas.core.dtypes.concat as _concat | ||
from pandas.core.dtypes.generic import ABCSeries | ||
from pandas.core.dtypes.missing import isna | ||
|
@@ -252,19 +253,54 @@ def __new__(cls, data=None, | |
# if dtype has an embedded tz, capture it | ||
tz = dtl.validate_tz_from_dtype(dtype, tz) | ||
|
||
if not isinstance(data, (np.ndarray, Index, ABCSeries, DatetimeArray)): | ||
# other iterable of some kind | ||
if not isinstance(data, (list, tuple)): | ||
if not hasattr(data, "dtype"): | ||
# e.g. list, tuple | ||
if np.ndim(data) == 0: | ||
# i.e. generator | ||
data = list(data) | ||
data = np.asarray(data, dtype='O') | ||
data = np.asarray(data) | ||
copy = False | ||
elif isinstance(data, ABCSeries): | ||
data = data._values | ||
|
||
# data must be Index or np.ndarray here | ||
if not (is_datetime64_dtype(data) or is_datetimetz(data) or | ||
is_integer_dtype(data) or lib.infer_dtype(data) == 'integer'): | ||
data = tools.to_datetime(data, dayfirst=dayfirst, | ||
yearfirst=yearfirst) | ||
# By this point we are assured to have either a numpy array or Index | ||
|
||
if is_float_dtype(data): | ||
# Note: we must cast to datetime64[ns] here in order to treat these | ||
# as wall-times instead of UTC timestamps. | ||
data = data.astype(_NS_DTYPE) | ||
copy = False | ||
# TODO: Why do we treat this differently from integer dtypes? | ||
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. @jreback any idea why we treat floats differently from ints here? 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. no idea, i would try to remove this special handling. only thing i can think of maybe this could have some odd rounding in the astype if its out of range of an int64 . 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.
Yah, the immediate goal is to pass fewer cases to If If we just cast floats to int64 (and mask with iNaT) then exactly one test fails in
I don't see any especially good reason why it should work this way, save for keeping this test working and not introducing breaking changes. |
||
|
||
elif is_timedelta64_dtype(data): | ||
warnings.warn("Passing timedelta64-dtype data to {cls} is " | ||
"deprecated, will raise a TypeError in a future " | ||
"version".format(cls=cls.__name__), | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
FutureWarning, stacklevel=2) | ||
data = data.view(_NS_DTYPE) | ||
|
||
elif is_period_dtype(data): | ||
# Note: without explicitly raising here, PeriondIndex | ||
# test_setops.test_join_does_not_recur fails | ||
raise TypeError("Passing PeriodDtype data to {cls} is invalid. " | ||
"Use `data.to_timestamp()` instead" | ||
.format(cls=cls.__name__)) | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
elif is_extension_type(data) and not is_datetime64tz_dtype(data): | ||
# Includes categorical | ||
# TODO: We have no tests for these | ||
data = np.array(data, dtype=np.object_) | ||
copy = False | ||
|
||
if is_object_dtype(data) or is_string_dtype(data): | ||
# TODO: We do not have tests specific to string-dtypes, | ||
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 could just write this as
might be more clear 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. The issue with that is np.array(['20160405']) becomes np.array([20160405]) instead of 2016-04-05. 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. ok sure |
||
# also complex or categorical or other extension | ||
copy = False | ||
if lib.infer_dtype(data) == 'integer': | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data = data.astype(np.int64) | ||
else: | ||
data = tools.to_datetime(data, dayfirst=dayfirst, | ||
yearfirst=yearfirst) | ||
|
||
if isinstance(data, DatetimeArray): | ||
if tz is None: | ||
|
@@ -281,6 +317,7 @@ def __new__(cls, data=None, | |
subarr = data._data | ||
|
||
if freq is None: | ||
# TODO: Should this be the stronger condition of `freq_infer`? | ||
freq = data.freq | ||
verify_integrity = False | ||
elif issubclass(data.dtype.type, np.datetime64): | ||
|
@@ -319,17 +356,15 @@ def __new__(cls, data=None, | |
return subarr._deepcopy_if_needed(ref_to_data, copy) | ||
|
||
@classmethod | ||
def _simple_new(cls, values, name=None, freq=None, tz=None, | ||
dtype=None, **kwargs): | ||
def _simple_new(cls, values, name=None, freq=None, tz=None, dtype=None): | ||
""" | ||
we require the we have a dtype compat for the values | ||
if we are passed a non-dtype compat, then coerce using the constructor | ||
""" | ||
# DatetimeArray._simple_new will accept either i8 or M8[ns] dtypes | ||
assert isinstance(values, np.ndarray), type(values) | ||
|
||
result = super(DatetimeIndex, cls)._simple_new(values, freq, tz, | ||
**kwargs) | ||
result = super(DatetimeIndex, cls)._simple_new(values, freq, tz) | ||
result.name = name | ||
result._reset_identity() | ||
return result | ||
|
Uh oh!
There was an error while loading. Please reload this page.