Skip to content
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

DEPR: passing td64 data to DTA or dt64 data to TDA #29794

Merged
merged 6 commits into from
Nov 27, 2019
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more.
- Removed the previously deprecated :meth:`Series.get_value`, :meth:`Series.set_value`, :meth:`DataFrame.get_value`, :meth:`DataFrame.set_value` (:issue:`17739`)
- Changed the the default value of `inplace` in :meth:`DataFrame.set_index` and :meth:`Series.set_axis`. It now defaults to False (:issue:`27600`)
- Removed support for nested renaming in :meth:`DataFrame.aggregate`, :meth:`Series.aggregate`, :meth:`DataFrameGroupBy.aggregate`, :meth:`SeriesGroupBy.aggregate`, :meth:`Rolling.aggregate` (:issue:`18529`)
- Passing ``datetime64`` data to :class:`TimedeltaIndex` or ``timedelta64`` data to ``DatetimeIndex`` now raises ``TypeError`` (:issue:`23539`, :issue:`23937`)
- A tuple passed to :meth:`DataFrame.groupby` is now exclusively treated as a single key (:issue:`18314`)
- Removed :meth:`Series.from_array` (:issue:`18258`)
- Removed :meth:`DataFrame.from_items` (:issue:`18458`)
Expand Down
10 changes: 2 additions & 8 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2102,14 +2102,8 @@ def maybe_convert_dtype(data, copy):
# with integer dtypes. See discussion in GH#23675

elif is_timedelta64_dtype(data):
warnings.warn(
"Passing timedelta64-dtype data is deprecated, will "
"raise a TypeError in a future version",
FutureWarning,
stacklevel=5,
)
data = data.view(_NS_DTYPE)

# GH#29794 enforcing deprecation introduced in GH#23539
raise TypeError(f"dtype {data.dtype} cannot be converted to datetime64[ns]")
elif is_period_dtype(data):
# Note: without explicitly raising here, PeriodIndex
# test_setops.test_join_does_not_recur fails
Expand Down
14 changes: 1 addition & 13 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
from pandas.core.dtypes.common import (
_NS_DTYPE,
_TD_DTYPE,
ensure_int64,
is_datetime64_dtype,
is_dtype_equal,
is_float_dtype,
is_integer_dtype,
Expand Down Expand Up @@ -1056,18 +1054,8 @@ def sequence_to_td64ns(data, copy=False, unit="ns", errors="raise"):
data = data.astype(_TD_DTYPE)
copy = False

elif is_datetime64_dtype(data):
# GH#23539
warnings.warn(
"Passing datetime64-dtype data to TimedeltaIndex is "
"deprecated, will raise a TypeError in a future "
"version",
FutureWarning,
stacklevel=4,
)
data = ensure_int64(data).view(_TD_DTYPE)

else:
# This includes datetime64-dtype, see GH#23539, GH#29794
raise TypeError(
"dtype {dtype} cannot be converted to timedelta64[ns]".format(
dtype=data.dtype
Expand Down
20 changes: 14 additions & 6 deletions pandas/tests/arithmetic/test_timedelta64.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,15 +577,23 @@ def test_timedelta_ops_with_missing_values(self):
# setup
s1 = pd.to_timedelta(Series(["00:00:01"]))
s2 = pd.to_timedelta(Series(["00:00:02"]))
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# Passing datetime64-dtype data to TimedeltaIndex is deprecated
sn = pd.to_timedelta(Series([pd.NaT]))

msg = r"dtype datetime64\[ns\] cannot be converted to timedelta64\[ns\]"
with pytest.raises(TypeError, match=msg):
# Passing datetime64-dtype data to TimedeltaIndex is no longer
# supported GH#29794
pd.to_timedelta(Series([pd.NaT]))

sn = pd.to_timedelta(Series([pd.NaT], dtype="m8[ns]"))

df1 = pd.DataFrame(["00:00:01"]).apply(pd.to_timedelta)
df2 = pd.DataFrame(["00:00:02"]).apply(pd.to_timedelta)
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# Passing datetime64-dtype data to TimedeltaIndex is deprecated
dfn = pd.DataFrame([pd.NaT]).apply(pd.to_timedelta)
with pytest.raises(TypeError, match=msg):
# Passing datetime64-dtype data to TimedeltaIndex is no longer
# supported GH#29794
pd.DataFrame([pd.NaT]).apply(pd.to_timedelta)

dfn = pd.DataFrame([pd.NaT.value]).apply(pd.to_timedelta)

scalar1 = pd.to_timedelta("00:00:01")
scalar2 = pd.to_timedelta("00:00:02")
Expand Down
29 changes: 11 additions & 18 deletions pandas/tests/indexes/datetimes/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,21 @@ def test_dti_with_period_data_raises(self):
with pytest.raises(TypeError, match="PeriodDtype data is invalid"):
to_datetime(period_array(data))

def test_dti_with_timedelta64_data_deprecation(self):
# GH#23675
def test_dti_with_timedelta64_data_raises(self):
# GH#23675 deprecated, enforrced in GH#29794
data = np.array([0], dtype="m8[ns]")
with tm.assert_produces_warning(FutureWarning):
result = DatetimeIndex(data)

assert result[0] == Timestamp("1970-01-01")

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
result = to_datetime(data)

assert result[0] == Timestamp("1970-01-01")

with tm.assert_produces_warning(FutureWarning):
result = DatetimeIndex(pd.TimedeltaIndex(data))
msg = r"timedelta64\[ns\] cannot be converted to datetime64"
with pytest.raises(TypeError, match=msg):
DatetimeIndex(data)

assert result[0] == Timestamp("1970-01-01")
with pytest.raises(TypeError, match=msg):
to_datetime(data)

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
result = to_datetime(pd.TimedeltaIndex(data))
with pytest.raises(TypeError, match=msg):
DatetimeIndex(pd.TimedeltaIndex(data))

assert result[0] == Timestamp("1970-01-01")
with pytest.raises(TypeError, match=msg):
to_datetime(pd.TimedeltaIndex(data))

def test_construction_caching(self):

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/indexes/timedeltas/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ def test_infer_from_tdi_mismatch(self):
def test_dt64_data_invalid(self):
# GH#23539
# passing tz-aware DatetimeIndex raises, naive or ndarray[datetime64]
# does not yet, but will in the future
# raise as of GH#29794
dti = pd.date_range("2016-01-01", periods=3)

msg = "cannot be converted to timedelta64"
with pytest.raises(TypeError, match=msg):
TimedeltaIndex(dti.tz_localize("Europe/Brussels"))

with tm.assert_produces_warning(FutureWarning):
with pytest.raises(TypeError, match=msg):
TimedeltaIndex(dti)

with tm.assert_produces_warning(FutureWarning):
with pytest.raises(TypeError, match=msg):
TimedeltaIndex(np.asarray(dti))

def test_float64_ns_rounded(self):
Expand Down