Skip to content

Commit

Permalink
DEPR: enforce deprecation of unitless td64/dt64 (pandas-dev#30199)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and proost committed Dec 19, 2019
1 parent 4156ec1 commit 2ff3a9f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,7 @@ class Timedelta(_Timedelta):
# also timedelta-like
return _broadcast_floordiv_td64(self.value, other, _rfloordiv)

# Includes integer array // Timedelta, deprecated in GH#19761
# Includes integer array // Timedelta, disallowed in GH#19761
raise TypeError(f'Invalid dtype {other.dtype} for __floordiv__')

elif is_float_object(other) and util.is_nan(other):
Expand Down
13 changes: 5 additions & 8 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from datetime import datetime, time, timedelta
import textwrap
from typing import Union
import warnings

Expand Down Expand Up @@ -2109,15 +2108,13 @@ def _validate_dt64_dtype(dtype):
if dtype is not None:
dtype = pandas_dtype(dtype)
if is_dtype_equal(dtype, np.dtype("M8")):
# no precision, warn
# no precision, disallowed GH#24806
dtype = _NS_DTYPE
msg = textwrap.dedent(
"""\
Passing in 'datetime64' dtype with no precision is deprecated
and will raise in a future version. Please pass in
'datetime64[ns]' instead."""
msg = (
"Passing in 'datetime64' dtype with no precision is not allowed. "
"Please pass in 'datetime64[ns]' instead."
)
warnings.warn(msg, FutureWarning, stacklevel=5)
raise ValueError(msg)

if (isinstance(dtype, np.dtype) and dtype != _NS_DTYPE) or not isinstance(
dtype, (np.dtype, DatetimeTZDtype)
Expand Down
13 changes: 5 additions & 8 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from datetime import timedelta
import textwrap
from typing import List
import warnings

import numpy as np

Expand Down Expand Up @@ -1123,14 +1121,13 @@ def objects_to_td64ns(data, unit="ns", errors="raise"):
def _validate_td64_dtype(dtype):
dtype = pandas_dtype(dtype)
if is_dtype_equal(dtype, np.dtype("timedelta64")):
# no precision disallowed GH#24806
dtype = _TD_DTYPE
msg = textwrap.dedent(
"""\
Passing in 'timedelta' dtype with no precision is deprecated
and will raise in a future version. Please pass in
'timedelta64[ns]' instead."""
msg = (
"Passing in 'timedelta' dtype with no precision is not allowed. "
"Please pass in 'timedelta64[ns]' instead."
)
warnings.warn(msg, FutureWarning, stacklevel=4)
raise ValueError(msg)

if not is_dtype_equal(dtype, _TD_DTYPE):
raise ValueError(_BAD_DTYPE.format(dtype=dtype))
Expand Down
9 changes: 0 additions & 9 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,6 @@ class DuplicateWarning(Warning):
# formats
_FORMAT_MAP = {"f": "fixed", "fixed": "fixed", "t": "table", "table": "table"}

format_deprecate_doc = """
the table keyword has been deprecated
use the format='fixed(f)|table(t)' keyword instead
fixed(f) : specifies the Fixed format
and is the default for put operations
table(t) : specifies the Table format
and is the default for append operations
"""

# storer class map
_STORER_MAP = {
"series": "SeriesFixed",
Expand Down
15 changes: 6 additions & 9 deletions pandas/tests/indexes/datetimes/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,18 +774,15 @@ def test_construction_with_nat_and_tzlocal(self):
expected = DatetimeIndex([Timestamp("2018", tz=tz), pd.NaT])
tm.assert_index_equal(result, expected)

def test_constructor_no_precision_warns(self):
def test_constructor_no_precision_raises(self):
# GH-24753, GH-24739
expected = pd.DatetimeIndex(["2000"], dtype="datetime64[ns]")

# we set the stacklevel for DatetimeIndex
with tm.assert_produces_warning(FutureWarning):
result = pd.DatetimeIndex(["2000"], dtype="datetime64")
tm.assert_index_equal(result, expected)
msg = "with no precision is not allowed"
with pytest.raises(ValueError, match=msg):
pd.DatetimeIndex(["2000"], dtype="datetime64")

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
result = pd.Index(["2000"], dtype="datetime64")
tm.assert_index_equal(result, expected)
with pytest.raises(ValueError, match=msg):
pd.Index(["2000"], dtype="datetime64")

def test_constructor_wrong_precision_raises(self):
with pytest.raises(ValueError):
Expand Down
15 changes: 6 additions & 9 deletions pandas/tests/indexes/timedeltas/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,15 @@ def test_constructor_name(self):
idx2 = TimedeltaIndex(idx, name="something else")
assert idx2.name == "something else"

def test_constructor_no_precision_warns(self):
def test_constructor_no_precision_raises(self):
# GH-24753, GH-24739
expected = pd.TimedeltaIndex(["2000"], dtype="timedelta64[ns]")

# we set the stacklevel for DatetimeIndex
with tm.assert_produces_warning(FutureWarning):
result = pd.TimedeltaIndex(["2000"], dtype="timedelta64")
tm.assert_index_equal(result, expected)
msg = "with no precision is not allowed"
with pytest.raises(ValueError, match=msg):
pd.TimedeltaIndex(["2000"], dtype="timedelta64")

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
result = pd.Index(["2000"], dtype="timedelta64")
tm.assert_index_equal(result, expected)
with pytest.raises(ValueError, match=msg):
pd.Index(["2000"], dtype="timedelta64")

def test_constructor_wrong_precision_raises(self):
with pytest.raises(ValueError):
Expand Down

0 comments on commit 2ff3a9f

Please sign in to comment.