Skip to content

Commit 54ca57e

Browse files
jbrockmendelproost
authored andcommitted
DEPR: passing td64 data to DTA or dt64 data to TDA (pandas-dev#29794)
1 parent 854277f commit 54ca57e

File tree

6 files changed

+32
-48
lines changed

6 files changed

+32
-48
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more.
407407
- Removed the previously deprecated :meth:`Series.get_value`, :meth:`Series.set_value`, :meth:`DataFrame.get_value`, :meth:`DataFrame.set_value` (:issue:`17739`)
408408
- Changed the the default value of `inplace` in :meth:`DataFrame.set_index` and :meth:`Series.set_axis`. It now defaults to False (:issue:`27600`)
409409
- Removed support for nested renaming in :meth:`DataFrame.aggregate`, :meth:`Series.aggregate`, :meth:`DataFrameGroupBy.aggregate`, :meth:`SeriesGroupBy.aggregate`, :meth:`Rolling.aggregate` (:issue:`18529`)
410+
- Passing ``datetime64`` data to :class:`TimedeltaIndex` or ``timedelta64`` data to ``DatetimeIndex`` now raises ``TypeError`` (:issue:`23539`, :issue:`23937`)
410411
- A tuple passed to :meth:`DataFrame.groupby` is now exclusively treated as a single key (:issue:`18314`)
411412
- Removed :meth:`Series.from_array` (:issue:`18258`)
412413
- Removed :meth:`DataFrame.from_items` (:issue:`18458`)

pandas/core/arrays/datetimes.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -2102,14 +2102,8 @@ def maybe_convert_dtype(data, copy):
21022102
# with integer dtypes. See discussion in GH#23675
21032103

21042104
elif is_timedelta64_dtype(data):
2105-
warnings.warn(
2106-
"Passing timedelta64-dtype data is deprecated, will "
2107-
"raise a TypeError in a future version",
2108-
FutureWarning,
2109-
stacklevel=5,
2110-
)
2111-
data = data.view(_NS_DTYPE)
2112-
2105+
# GH#29794 enforcing deprecation introduced in GH#23539
2106+
raise TypeError(f"dtype {data.dtype} cannot be converted to datetime64[ns]")
21132107
elif is_period_dtype(data):
21142108
# Note: without explicitly raising here, PeriodIndex
21152109
# test_setops.test_join_does_not_recur fails

pandas/core/arrays/timedeltas.py

+1-13
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
from pandas.core.dtypes.common import (
2121
_NS_DTYPE,
2222
_TD_DTYPE,
23-
ensure_int64,
24-
is_datetime64_dtype,
2523
is_dtype_equal,
2624
is_float_dtype,
2725
is_integer_dtype,
@@ -1056,18 +1054,8 @@ def sequence_to_td64ns(data, copy=False, unit="ns", errors="raise"):
10561054
data = data.astype(_TD_DTYPE)
10571055
copy = False
10581056

1059-
elif is_datetime64_dtype(data):
1060-
# GH#23539
1061-
warnings.warn(
1062-
"Passing datetime64-dtype data to TimedeltaIndex is "
1063-
"deprecated, will raise a TypeError in a future "
1064-
"version",
1065-
FutureWarning,
1066-
stacklevel=4,
1067-
)
1068-
data = ensure_int64(data).view(_TD_DTYPE)
1069-
10701057
else:
1058+
# This includes datetime64-dtype, see GH#23539, GH#29794
10711059
raise TypeError(
10721060
"dtype {dtype} cannot be converted to timedelta64[ns]".format(
10731061
dtype=data.dtype

pandas/tests/arithmetic/test_timedelta64.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -577,15 +577,23 @@ def test_timedelta_ops_with_missing_values(self):
577577
# setup
578578
s1 = pd.to_timedelta(Series(["00:00:01"]))
579579
s2 = pd.to_timedelta(Series(["00:00:02"]))
580-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
581-
# Passing datetime64-dtype data to TimedeltaIndex is deprecated
582-
sn = pd.to_timedelta(Series([pd.NaT]))
580+
581+
msg = r"dtype datetime64\[ns\] cannot be converted to timedelta64\[ns\]"
582+
with pytest.raises(TypeError, match=msg):
583+
# Passing datetime64-dtype data to TimedeltaIndex is no longer
584+
# supported GH#29794
585+
pd.to_timedelta(Series([pd.NaT]))
586+
587+
sn = pd.to_timedelta(Series([pd.NaT], dtype="m8[ns]"))
583588

584589
df1 = pd.DataFrame(["00:00:01"]).apply(pd.to_timedelta)
585590
df2 = pd.DataFrame(["00:00:02"]).apply(pd.to_timedelta)
586-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
587-
# Passing datetime64-dtype data to TimedeltaIndex is deprecated
588-
dfn = pd.DataFrame([pd.NaT]).apply(pd.to_timedelta)
591+
with pytest.raises(TypeError, match=msg):
592+
# Passing datetime64-dtype data to TimedeltaIndex is no longer
593+
# supported GH#29794
594+
pd.DataFrame([pd.NaT]).apply(pd.to_timedelta)
595+
596+
dfn = pd.DataFrame([pd.NaT.value]).apply(pd.to_timedelta)
589597

590598
scalar1 = pd.to_timedelta("00:00:01")
591599
scalar2 = pd.to_timedelta("00:00:02")

pandas/tests/indexes/datetimes/test_construction.py

+11-18
Original file line numberDiff line numberDiff line change
@@ -70,28 +70,21 @@ def test_dti_with_period_data_raises(self):
7070
with pytest.raises(TypeError, match="PeriodDtype data is invalid"):
7171
to_datetime(period_array(data))
7272

73-
def test_dti_with_timedelta64_data_deprecation(self):
74-
# GH#23675
73+
def test_dti_with_timedelta64_data_raises(self):
74+
# GH#23675 deprecated, enforrced in GH#29794
7575
data = np.array([0], dtype="m8[ns]")
76-
with tm.assert_produces_warning(FutureWarning):
77-
result = DatetimeIndex(data)
78-
79-
assert result[0] == Timestamp("1970-01-01")
80-
81-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
82-
result = to_datetime(data)
83-
84-
assert result[0] == Timestamp("1970-01-01")
85-
86-
with tm.assert_produces_warning(FutureWarning):
87-
result = DatetimeIndex(pd.TimedeltaIndex(data))
76+
msg = r"timedelta64\[ns\] cannot be converted to datetime64"
77+
with pytest.raises(TypeError, match=msg):
78+
DatetimeIndex(data)
8879

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

91-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
92-
result = to_datetime(pd.TimedeltaIndex(data))
83+
with pytest.raises(TypeError, match=msg):
84+
DatetimeIndex(pd.TimedeltaIndex(data))
9385

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

9689
def test_construction_caching(self):
9790

pandas/tests/indexes/timedeltas/test_construction.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@ def test_infer_from_tdi_mismatch(self):
6060
def test_dt64_data_invalid(self):
6161
# GH#23539
6262
# passing tz-aware DatetimeIndex raises, naive or ndarray[datetime64]
63-
# does not yet, but will in the future
63+
# raise as of GH#29794
6464
dti = pd.date_range("2016-01-01", periods=3)
6565

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

70-
with tm.assert_produces_warning(FutureWarning):
70+
with pytest.raises(TypeError, match=msg):
7171
TimedeltaIndex(dti)
7272

73-
with tm.assert_produces_warning(FutureWarning):
73+
with pytest.raises(TypeError, match=msg):
7474
TimedeltaIndex(np.asarray(dti))
7575

7676
def test_float64_ns_rounded(self):

0 commit comments

Comments
 (0)