Skip to content

CLN: Remove cdate_range, raise_on_error keyword, categories and ordered keywords in astype #27141

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

Merged
merged 5 commits into from
Jun 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions doc/source/user_guide/timeseries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -474,16 +474,6 @@ resulting ``DatetimeIndex``:
Custom frequency ranges
~~~~~~~~~~~~~~~~~~~~~~~

.. warning::

This functionality was originally exclusive to ``cdate_range``, which is
deprecated as of version 0.21.0 in favor of ``bdate_range``. Note that
``cdate_range`` only utilizes the ``weekmask`` and ``holidays`` parameters
when custom business day, 'C', is passed as the frequency string. Support has
been expanded with ``bdate_range`` to work with any custom frequency string.

.. versionadded:: 0.21.0

``bdate_range`` can also generate a range of custom frequency dates by using
the ``weekmask`` and ``holidays`` parameters. These parameters will only be
used if a custom frequency string is passed.
Expand Down
3 changes: 3 additions & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,9 @@ Removal of prior version deprecations/changes
- Removed the previously deprecated behavior of altering column or index labels with :meth:`Series.rename_axis` or :meth:`DataFrame.rename_axis` (:issue:`17842`)
- Removed the previously deprecated ``tupleize_cols`` keyword argument in :meth:`read_html`, :meth:`read_csv`, and :meth:`DataFrame.to_csv` (:issue:`17877`, :issue:`17820`)
- Removed the previously deprecated ``DataFrame.from.csv`` and ``Series.from_csv`` (:issue:`17812`)
- Removed the previously deprecated ``raise_on_error`` keyword argument in :meth:`DataFrame.where` and :meth:`DataFrame.mask` (:issue:`17744`)
- Removed the previously deprecated ``ordered`` and ``categories`` keyword arguments in ``astype`` (:issue:`17742`)
- Removed the previously deprecated ``cdate_range`` (:issue:`17691`)

.. _whatsnew_0250.performance:

Expand Down
33 changes: 2 additions & 31 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8644,13 +8644,6 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,

try_cast : bool, default False
Try to cast the result back to the input type (if possible).
raise_on_error : bool, default True
Whether to raise on invalid data types (e.g. trying to where on
strings).

.. deprecated:: 0.21.0

Use `errors`.

Returns
-------
Expand Down Expand Up @@ -8738,18 +8731,7 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
cond_rev="False", name='where',
name_other='mask'))
def where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
errors='raise', try_cast=False, raise_on_error=None):

if raise_on_error is not None:
warnings.warn(
"raise_on_error is deprecated in "
"favor of errors='raise|ignore'",
FutureWarning, stacklevel=2)

if raise_on_error:
errors = 'raise'
else:
errors = 'ignore'
errors='raise', try_cast=False):

other = com.apply_if_callable(other, self)
return self._where(cond, other, inplace, axis, level,
Expand All @@ -8759,18 +8741,7 @@ def where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
cond_rev="True", name='mask',
name_other='where'))
def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None,
errors='raise', try_cast=False, raise_on_error=None):

if raise_on_error is not None:
warnings.warn(
"raise_on_error is deprecated in "
"favor of errors='raise|ignore'",
FutureWarning, stacklevel=2)

if raise_on_error:
errors = 'raise'
else:
errors = 'ignore'
errors='raise', try_cast=False):

inplace = validate_bool_kwarg(inplace, 'inplace')
cond = com.apply_if_callable(cond, self)
Expand Down
62 changes: 1 addition & 61 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import pandas.core.tools.datetimes as tools

from pandas.tseries.frequencies import Resolution, to_offset
from pandas.tseries.offsets import CDay, Nano, prefix_mapping
from pandas.tseries.offsets import Nano, prefix_mapping


def _new_DatetimeIndex(cls, d):
Expand Down Expand Up @@ -1568,66 +1568,6 @@ def bdate_range(start=None, end=None, periods=None, freq='B', tz=None,
closed=closed, **kwargs)


def cdate_range(start=None, end=None, periods=None, freq='C', tz=None,
normalize=True, name=None, closed=None, **kwargs):
"""
Return a fixed frequency DatetimeIndex, with CustomBusinessDay as the
default frequency

.. deprecated:: 0.21.0

Parameters
----------
start : string or datetime-like, default None
Left bound for generating dates
end : string or datetime-like, default None
Right bound for generating dates
periods : integer, default None
Number of periods to generate
freq : string or DateOffset, default 'C' (CustomBusinessDay)
Frequency strings can have multiples, e.g. '5H'
tz : string, default None
Time zone name for returning localized DatetimeIndex, for example
Asia/Beijing
normalize : bool, default False
Normalize start/end dates to midnight before generating date range
name : string, default None
Name of the resulting DatetimeIndex
weekmask : string, Default 'Mon Tue Wed Thu Fri'
weekmask of valid business days, passed to ``numpy.busdaycalendar``
holidays : list
list/array of dates to exclude from the set of valid business days,
passed to ``numpy.busdaycalendar``
closed : string, default None
Make the interval closed with respect to the given frequency to
the 'left', 'right', or both sides (None)

Notes
-----
Of the three parameters: ``start``, ``end``, and ``periods``, exactly two
must be specified.

To learn more about the frequency strings, please see `this link
<http://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases>`__.

Returns
-------
rng : DatetimeIndex
"""
warnings.warn("cdate_range is deprecated and will be removed in a future "
"version, instead use pd.bdate_range(..., freq='{freq}')"
.format(freq=freq), FutureWarning, stacklevel=2)

if freq == 'C':
holidays = kwargs.pop('holidays', [])
weekmask = kwargs.pop('weekmask', 'Mon Tue Wed Thu Fri')
freq = CDay(holidays=holidays, weekmask=weekmask)

return date_range(start=start, end=end, periods=periods, freq=freq,
tz=tz, normalize=normalize, name=name,
closed=closed, **kwargs)


def _time_to_micros(time):
seconds = time.hour * 60 * 60 + 60 * time.minute + time.second
return 1000000 * seconds + time.microsecond
15 changes: 4 additions & 11 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,17 +542,10 @@ def _astype(self, dtype, copy=False, errors='raise', values=None,
if self.is_categorical_astype(dtype):

# deprecated 17636
if ('categories' in kwargs or 'ordered' in kwargs):
if isinstance(dtype, CategoricalDtype):
raise TypeError(
"Cannot specify a CategoricalDtype and also "
"`categories` or `ordered`. Use "
"`dtype=CategoricalDtype(categories, ordered)`"
" instead.")
warnings.warn("specifying 'categories' or 'ordered' in "
".astype() is deprecated; pass a "
"CategoricalDtype instead",
FutureWarning, stacklevel=7)
for deprecated_arg in ('categories', 'ordered'):
if deprecated_arg in kwargs:
raise ValueError('Got an unexpected argument: {}'.format(
deprecated_arg))

categories = kwargs.get('categories', None)
ordered = kwargs.get('ordered', None)
Expand Down
10 changes: 0 additions & 10 deletions pandas/tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,3 @@ def test_testing(self):

from pandas import testing
self.check(testing, self.funcs)


class TestCDateRange:

def test_deprecation_cdaterange(self):
# GH17596
from pandas.core.indexes.datetimes import cdate_range
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
cdate_range('2017-01-01', '2017-12-31')
11 changes: 0 additions & 11 deletions pandas/tests/series/indexing/test_boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,6 @@ def test_where_unsafe():
assert_series_equal(result, expected)


def test_where_raise_on_error_deprecation():
# gh-14968
# deprecation of raise_on_error
s = Series(np.random.randn(5))
cond = s > 0
with tm.assert_produces_warning(FutureWarning):
s.where(cond, raise_on_error=True)
with tm.assert_produces_warning(FutureWarning):
s.mask(cond, raise_on_error=True)


def test_where():
s = Series(np.random.randn(5))
cond = s > 0
Expand Down
32 changes: 3 additions & 29 deletions pandas/tests/series/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,12 @@ def test_astype_dict_like(self, dtype_class):
with pytest.raises(KeyError, match=msg):
s.astype(dt5)

def test_astype_categories_deprecation(self):
def test_astype_categories_deprecation_raises(self):

# deprecated 17636
s = Series(['a', 'b', 'a'])
expected = s.astype(CategoricalDtype(['a', 'b'], ordered=True))
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
result = s.astype('category', categories=['a', 'b'], ordered=True)
tm.assert_series_equal(result, expected)
with pytest.raises(ValueError, match="Got an unexpected"):
s.astype('category', categories=['a', 'b'], ordered=True)

def test_astype_from_categorical(self):
items = ["a", "b", "c", "a"]
Expand Down Expand Up @@ -349,21 +346,12 @@ def test_astype_categorical_to_categorical(self, name, dtype_ordered,
expected = Series(s_data, name=name, dtype=exp_dtype)
tm.assert_series_equal(result, expected)

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
result = s.astype('category', ordered=dtype_ordered)
tm.assert_series_equal(result, expected)

# different categories
dtype = CategoricalDtype(list('adc'), dtype_ordered)
result = s.astype(dtype)
expected = Series(s_data, name=name, dtype=dtype)
tm.assert_series_equal(result, expected)

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
result = s.astype(
'category', categories=list('adc'), ordered=dtype_ordered)
tm.assert_series_equal(result, expected)

if dtype_ordered is False:
# not specifying ordered, so only test once
expected = s
Expand All @@ -387,20 +375,6 @@ def test_astype_categoricaldtype(self):
tm.assert_series_equal(result, expected)
tm.assert_index_equal(result.cat.categories, Index(['a', 'b', 'c']))

def test_astype_categoricaldtype_with_args(self):
s = Series(['a', 'b'])
type_ = CategoricalDtype(['a', 'b'])

msg = (r"Cannot specify a CategoricalDtype and also `categories` or"
r" `ordered`\. Use `dtype=CategoricalDtype\(categories,"
r" ordered\)` instead\.")
with pytest.raises(TypeError, match=msg):
s.astype(type_, ordered=True)
with pytest.raises(TypeError, match=msg):
s.astype(type_, categories=['a', 'b'])
with pytest.raises(TypeError, match=msg):
s.astype(type_, categories=['a', 'b'], ordered=False)

@pytest.mark.parametrize("dtype", [
np.datetime64,
np.timedelta64,
Expand Down