From 59c82b81623f277e77c22d88f26651e0c3d825c8 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 9 Dec 2019 20:49:35 -0800 Subject: [PATCH 1/3] DEPR: to_timedelta box kwarg --- doc/source/whatsnew/v1.0.0.rst | 1 + pandas/core/dtypes/dtypes.py | 2 +- pandas/core/indexes/datetimes.py | 28 -------------- pandas/core/indexes/multi.py | 5 --- pandas/core/indexes/period.py | 23 ----------- pandas/core/indexes/timedeltas.py | 27 ------------- pandas/core/tools/timedeltas.py | 38 +++++-------------- pandas/io/excel/_base.py | 5 --- pandas/tests/indexes/timedeltas/test_tools.py | 10 ----- 9 files changed, 11 insertions(+), 128 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 19fb4bdcd9536..45fe35b06d7c5 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -540,6 +540,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more. - 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 the previously deprecated :attr:`Series.cat.categorical`, :attr:`Series.cat.index`, :attr:`Series.cat.name` (:issue:`24751`) - :func:`to_datetime` no longer accepts "box" argument, always returns :class:`DatetimeIndex` or :class:`Index`, :class:`Series`, or :class:`DataFrame` (:issue:`24486`) +- :func:`to_timedelta` no longer accepts "box" argument, always returns :class:`TimedeltaIndex` or :class:`Index`, :class:`Series`, or :class:`DataFrame` (:issue:`24486`) - :func:`to_timedelta`, :class:`Timedelta`, and :class:`TimedeltaIndex` no longer allow "M", "y", or "Y" for the "unit" argument (:issue:`23264`) - Removed the previously deprecated ``time_rule`` keyword from (non-public) :func:`offsets.generate_range`, which has been moved to :func:`core.arrays._ranges.generate_range` (:issue:`24157`) - :meth:`DataFrame.loc` or :meth:`Series.loc` with listlike indexers and missing labels will no longer reindex (:issue:`17295`) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 2bb27de320e7e..e45f4ce1b72b1 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -669,7 +669,7 @@ def __init__(self, unit="ns", tz=None): tz = result.tz msg = ( "Passing a dtype alias like 'datetime64[ns, {tz}]' " - "to DatetimeTZDtype is deprecated. Use " + "to DatetimeTZDtype is no longer supported. Use " "'DatetimeTZDtype.construct_from_string()' instead." ) raise ValueError(msg) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index b730fd0f876fa..cafa2e03f23d0 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -108,31 +108,6 @@ class DatetimeIndex(DatetimeIndexOpsMixin, Int64Index, DatetimeDelegateMixin): One of pandas date offset strings or corresponding objects. The string 'infer' can be passed in order to set the frequency of the index as the inferred frequency upon creation. - - start : starting value, datetime-like, optional - If data is None, start is used as the start point in generating regular - timestamp data. - - .. deprecated:: 0.24.0 - - periods : int, optional, > 0 - Number of periods to generate, if generating index. Takes precedence - over end argument. - - .. deprecated:: 0.24.0 - - end : end time, datetime-like, optional - If periods is none, generated index will extend to first conforming - time on or just past end argument. - - .. deprecated:: 0.24.0 - - closed : str or None, default None - Make the interval closed with respect to the given frequency to - the 'left', 'right', or both sides (None). - - .. deprecated:: 0.24. 0 - tz : pytz.timezone or dateutil.tz.tzfile ambiguous : 'infer', bool-ndarray, 'NaT', default 'raise' When clocks moved backward due to DST, ambiguous times may arise. @@ -217,9 +192,6 @@ class DatetimeIndex(DatetimeIndexOpsMixin, Int64Index, DatetimeDelegateMixin): ----- To learn more about the frequency strings, please see `this link `__. - - Creating a DatetimeIndex based on `start`, `periods`, and `end` has - been deprecated in favor of :func:`date_range`. """ _typ = "datetimeindex" diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index be436998e9f30..cffd1e99b37f3 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -157,11 +157,6 @@ class MultiIndex(Index): Integers for each level designating which label at each location. .. versionadded:: 0.24.0 - labels : sequence of arrays - Integers for each level designating which label at each location. - - .. deprecated:: 0.24.0 - Use ``codes`` instead sortorder : optional int Level of sortedness (must be lexicographically sorted by that level). diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index d63de10d92921..15639d4436d5f 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -91,24 +91,6 @@ class PeriodIndex(DatetimeIndexOpsMixin, Int64Index, PeriodDelegateMixin): Make a copy of input ndarray freq : str or period object, optional One of pandas period strings or corresponding objects - start : starting value, period-like, optional - If data is None, used as the start point in generating regular - period data. - - .. deprecated:: 0.24.0 - - periods : int, optional, > 0 - Number of periods to generate, if generating index. Takes precedence - over end argument - - .. deprecated:: 0.24.0 - - end : end value, period-like, optional - If periods is none, generated index will extend to first conforming - period on or just past end argument - - .. deprecated:: 0.24.0 - year : int, array, or Series, default None month : int, array, or Series, default None quarter : int, array, or Series, default None @@ -157,11 +139,6 @@ class PeriodIndex(DatetimeIndexOpsMixin, Int64Index, PeriodDelegateMixin): TimedeltaIndex : Index of timedelta64 data. period_range : Create a fixed-frequency PeriodIndex. - Notes - ----- - Creating a PeriodIndex based on `start`, `periods`, and `end` has - been deprecated in favor of :func:`period_range`. - Examples -------- >>> idx = pd.PeriodIndex(year=year_arr, quarter=q_arr) diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index dcbac3c2e19cf..16db4106552a9 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -82,30 +82,6 @@ class TimedeltaIndex( inferred frequency upon creation. copy : bool Make a copy of input ndarray. - start : starting value, timedelta-like, optional - If data is None, start is used as the start point in generating regular - timedelta data. - - .. deprecated:: 0.24.0 - - periods : int, optional, > 0 - Number of periods to generate, if generating index. Takes precedence - over end argument. - - .. deprecated:: 0.24.0 - - end : end time, timedelta-like, optional - If periods is none, generated index will extend to first conforming - time on or just past end argument. - - .. deprecated:: 0.24. 0 - - closed : str or None, default None - Make the interval closed with respect to the given frequency to - the 'left', 'right', or both sides (None). - - .. deprecated:: 0.24. 0 - name : object Name to be stored in the index. @@ -140,9 +116,6 @@ class TimedeltaIndex( ----- To learn more about the frequency strings, please see `this link `__. - - Creating a TimedeltaIndex based on `start`, `periods`, and `end` has - been deprecated in favor of :func:`timedelta_range`. """ _typ = "timedeltaindex" diff --git a/pandas/core/tools/timedeltas.py b/pandas/core/tools/timedeltas.py index c624ddb2eb4d1..3e185feaea38e 100644 --- a/pandas/core/tools/timedeltas.py +++ b/pandas/core/tools/timedeltas.py @@ -6,7 +6,6 @@ from pandas._libs.tslibs import NaT from pandas._libs.tslibs.timedeltas import Timedelta, parse_timedelta_unit -from pandas.util._decorators import deprecate_kwarg from pandas.core.dtypes.common import is_list_like from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries @@ -14,8 +13,7 @@ from pandas.core.arrays.timedeltas import sequence_to_td64ns -@deprecate_kwarg(old_arg_name="box", new_arg_name=None) -def to_timedelta(arg, unit="ns", box=True, errors="raise"): +def to_timedelta(arg, unit="ns", errors="raise"): """ Convert argument to timedelta. @@ -36,15 +34,6 @@ def to_timedelta(arg, unit="ns", box=True, errors="raise"): 'milli', 'millis', 'L', 'us', 'microseconds', 'microsecond', 'micro', 'micros', 'U', 'ns', 'nanoseconds', 'nano', 'nanos', 'nanosecond', 'N'). - box : bool, default True - - If True returns a Timedelta/TimedeltaIndex of the results. - - If False returns a numpy.timedelta64 or numpy.darray of - values of dtype timedelta64[ns]. - - .. deprecated:: 0.25.0 - Use :meth:`Series.to_numpy` or :meth:`Timedelta.to_timedelta64` - instead to get an ndarray of values or numpy.timedelta64, - respectively. errors : {'ignore', 'raise', 'coerce'}, default 'raise' - If 'raise', then invalid parsing will raise an exception. @@ -86,11 +75,6 @@ def to_timedelta(arg, unit="ns", box=True, errors="raise"): >>> pd.to_timedelta(np.arange(5), unit='d') TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'], dtype='timedelta64[ns]', freq=None) - - Returning an ndarray by using the 'box' keyword argument: - - >>> pd.to_timedelta(np.arange(5), box=False) - array([0, 1, 2, 3, 4], dtype='timedelta64[ns]') """ unit = parse_timedelta_unit(unit) @@ -106,32 +90,29 @@ def to_timedelta(arg, unit="ns", box=True, errors="raise"): if arg is None: return arg elif isinstance(arg, ABCSeries): - values = _convert_listlike(arg._values, unit=unit, box=False, errors=errors) + values = _convert_listlike(arg._values, unit=unit, errors=errors) return arg._constructor(values, index=arg.index, name=arg.name) elif isinstance(arg, ABCIndexClass): - return _convert_listlike(arg, unit=unit, box=box, errors=errors, name=arg.name) + return _convert_listlike(arg, unit=unit, errors=errors, name=arg.name) elif isinstance(arg, np.ndarray) and arg.ndim == 0: # extract array scalar and process below arg = arg.item() elif is_list_like(arg) and getattr(arg, "ndim", 1) == 1: - return _convert_listlike(arg, unit=unit, box=box, errors=errors) + return _convert_listlike(arg, unit=unit, errors=errors) elif getattr(arg, "ndim", 1) > 1: raise TypeError( "arg must be a string, timedelta, list, tuple, 1-d array, or Series" ) # ...so it must be a scalar value. Return scalar. - return _coerce_scalar_to_timedelta_type(arg, unit=unit, box=box, errors=errors) + return _coerce_scalar_to_timedelta_type(arg, unit=unit, errors=errors) -def _coerce_scalar_to_timedelta_type(r, unit="ns", box=True, errors="raise"): +def _coerce_scalar_to_timedelta_type(r, unit="ns", errors="raise"): """Convert string 'r' to a timedelta object.""" try: result = Timedelta(r, unit) - if not box: - # explicitly view as timedelta64 for case when result is pd.NaT - result = result.asm8.view("timedelta64[ns]") except ValueError: if errors == "raise": raise @@ -144,7 +125,7 @@ def _coerce_scalar_to_timedelta_type(r, unit="ns", box=True, errors="raise"): return result -def _convert_listlike(arg, unit="ns", box=True, errors="raise", name=None): +def _convert_listlike(arg, unit="ns", errors="raise", name=None): """Convert a list of objects to a timedelta index object.""" if isinstance(arg, (list, tuple)) or not hasattr(arg, "dtype"): @@ -169,8 +150,7 @@ def _convert_listlike(arg, unit="ns", box=True, errors="raise", name=None): # like to surface it. raise - if box: - from pandas import TimedeltaIndex + from pandas import TimedeltaIndex - value = TimedeltaIndex(value, unit="ns", name=name) + value = TimedeltaIndex(value, unit="ns", name=name) return value diff --git a/pandas/io/excel/_base.py b/pandas/io/excel/_base.py index d4570498e3fc6..7cb1309ae8d41 100644 --- a/pandas/io/excel/_base.py +++ b/pandas/io/excel/_base.py @@ -79,11 +79,6 @@ is based on the subset. usecols : int, str, list-like, or callable default None * If None, then parse all columns. - * If int, then indicates last column to be parsed. - - .. deprecated:: 0.24.0 - Pass in a list of int instead from 0 to `usecols` inclusive. - * If str, then indicates comma separated list of Excel column letters and column ranges (e.g. "A:E" or "A,C,E:F"). Ranges are inclusive of both sides. diff --git a/pandas/tests/indexes/timedeltas/test_tools.py b/pandas/tests/indexes/timedeltas/test_tools.py index 2b4a6722666bf..0498f853d6c94 100644 --- a/pandas/tests/indexes/timedeltas/test_tools.py +++ b/pandas/tests/indexes/timedeltas/test_tools.py @@ -208,13 +208,3 @@ def test_to_timedelta_float(self): result = pd.to_timedelta(arr, unit="s") expected_asi8 = np.arange(999990000, int(1e9), 1000, dtype="int64") tm.assert_numpy_array_equal(result.asi8, expected_asi8) - - def test_to_timedelta_box_deprecated(self): - result = np.timedelta64(0, "ns") - - # Deprecated - see GH24416 - with tm.assert_produces_warning(FutureWarning): - to_timedelta(0, box=False) - - expected = to_timedelta(0).to_timedelta64() - assert result == expected From 24ecb3d44083efa89e89cd63fd4270f1b3df4995 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 10 Dec 2019 08:43:28 -0800 Subject: [PATCH 2/3] missing test, merge whatsnew note --- doc/source/whatsnew/v1.0.0.rst | 3 +- pandas/tests/indexes/timedeltas/test_tools.py | 41 ------------------- 2 files changed, 1 insertion(+), 43 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index f19d6cb17877c..df7ad36c6af98 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -539,8 +539,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more. - Removed the previously deprecated :meth:`Series.compound` and :meth:`DataFrame.compound` (:issue:`26405`) - 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 the previously deprecated :attr:`Series.cat.categorical`, :attr:`Series.cat.index`, :attr:`Series.cat.name` (:issue:`24751`) -- :func:`to_datetime` no longer accepts "box" argument, always returns :class:`DatetimeIndex` or :class:`Index`, :class:`Series`, or :class:`DataFrame` (:issue:`24486`) -- :func:`to_timedelta` no longer accepts "box" argument, always returns :class:`TimedeltaIndex` or :class:`Index`, :class:`Series`, or :class:`DataFrame` (:issue:`24486`) +- :func:`to_datetime` and :func:`to_timedelta` no longer accept "box" argument, always returns :class:`DatetimeIndex`, :class:`TimedeltaIndex`, :class:`Index`, :class:`Series`, or :class:`DataFrame` (:issue:`24486`) - :func:`to_timedelta`, :class:`Timedelta`, and :class:`TimedeltaIndex` no longer allow "M", "y", or "Y" for the "unit" argument (:issue:`23264`) - Removed the previously deprecated ``time_rule`` keyword from (non-public) :func:`offsets.generate_range`, which has been moved to :func:`core.arrays._ranges.generate_range` (:issue:`24157`) - :meth:`DataFrame.loc` or :meth:`Series.loc` with listlike indexers and missing labels will no longer reindex (:issue:`17295`) diff --git a/pandas/tests/indexes/timedeltas/test_tools.py b/pandas/tests/indexes/timedeltas/test_tools.py index 0498f853d6c94..dca8eb0c5b75f 100644 --- a/pandas/tests/indexes/timedeltas/test_tools.py +++ b/pandas/tests/indexes/timedeltas/test_tools.py @@ -3,8 +3,6 @@ import numpy as np import pytest -from pandas._libs.tslib import iNaT - import pandas as pd from pandas import Series, TimedeltaIndex, isna, to_timedelta import pandas.util.testing as tm @@ -12,26 +10,6 @@ class TestTimedeltas: def test_to_timedelta(self): - def conv(v): - return v.astype("m8[ns]") - - d1 = np.timedelta64(1, "D") - - with tm.assert_produces_warning(FutureWarning): - assert to_timedelta("1 days 06:05:01.00003", box=False) == conv( - d1 - + np.timedelta64(6 * 3600 + 5 * 60 + 1, "s") - + np.timedelta64(30, "us") - ) - - with tm.assert_produces_warning(FutureWarning): - assert to_timedelta("15.5us", box=False) == conv( - np.timedelta64(15500, "ns") - ) - - # empty string - result = to_timedelta("", box=False) - assert result.astype("int64") == iNaT result = to_timedelta(["", ""]) assert isna(result).all() @@ -41,12 +19,6 @@ def conv(v): expected = pd.Index(np.array([np.timedelta64(1, "s")])) tm.assert_index_equal(result, expected) - with tm.assert_produces_warning(FutureWarning): - # ints - result = np.timedelta64(0, "ns") - expected = to_timedelta(0, box=False) - assert result == expected - # Series expected = Series([timedelta(days=1), timedelta(days=1, seconds=1)]) result = to_timedelta(Series(["1d", "1days 00:00:01"])) @@ -59,19 +31,6 @@ def conv(v): expected = to_timedelta([0, 10], unit="s") tm.assert_index_equal(result, expected) - with tm.assert_produces_warning(FutureWarning): - # single element conversion - v = timedelta(seconds=1) - result = to_timedelta(v, box=False) - expected = np.timedelta64(timedelta(seconds=1)) - assert result == expected - - with tm.assert_produces_warning(FutureWarning): - v = np.timedelta64(timedelta(seconds=1)) - result = to_timedelta(v, box=False) - expected = np.timedelta64(timedelta(seconds=1)) - assert result == expected - # arrays of various dtypes arr = np.array([1] * 5, dtype="int64") result = to_timedelta(arr, unit="s") From 5bd26f5c7f5d3cc5211f13506fddff8f09995302 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 10 Dec 2019 09:11:01 -0800 Subject: [PATCH 3/3] remove more box cases --- pandas/tests/indexes/timedeltas/test_tools.py | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/pandas/tests/indexes/timedeltas/test_tools.py b/pandas/tests/indexes/timedeltas/test_tools.py index dca8eb0c5b75f..5bd7a2a583b84 100644 --- a/pandas/tests/indexes/timedeltas/test_tools.py +++ b/pandas/tests/indexes/timedeltas/test_tools.py @@ -57,28 +57,6 @@ def test_to_timedelta(self): expected = TimedeltaIndex([np.timedelta64(1, "D")] * 5) tm.assert_index_equal(result, expected) - with tm.assert_produces_warning(FutureWarning): - # Test with lists as input when box=false - expected = np.array(np.arange(3) * 1000000000, dtype="timedelta64[ns]") - result = to_timedelta(range(3), unit="s", box=False) - tm.assert_numpy_array_equal(expected, result) - - with tm.assert_produces_warning(FutureWarning): - result = to_timedelta(np.arange(3), unit="s", box=False) - tm.assert_numpy_array_equal(expected, result) - - with tm.assert_produces_warning(FutureWarning): - result = to_timedelta([0, 1, 2], unit="s", box=False) - tm.assert_numpy_array_equal(expected, result) - - with tm.assert_produces_warning(FutureWarning): - # Tests with fractional seconds as input: - expected = np.array( - [0, 500000000, 800000000, 1200000000], dtype="timedelta64[ns]" - ) - result = to_timedelta([0.0, 0.5, 0.8, 1.2], unit="s", box=False) - tm.assert_numpy_array_equal(expected, result) - def test_to_timedelta_invalid(self): # bad value for errors parameter