From 8173827c7813381622a8928f3de0a0d6ec049945 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sat, 29 Jun 2019 09:28:54 -0500 Subject: [PATCH 1/4] Remove Series in rename_categories as list-like --- doc/source/whatsnew/v0.25.0.rst | 2 ++ pandas/core/arrays/categorical.py | 14 -------------- pandas/tests/arrays/categorical/test_api.py | 7 +------ 3 files changed, 3 insertions(+), 20 deletions(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 008f6f0b8643e..29682dddd3189 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -627,6 +627,8 @@ Removal of prior version deprecations/changes - Removed the previously deprecated ``pd.options.html.border`` (:issue:`16970`) - Removed the previously deprecated ``convert_objects`` (:issue:`11221`) - Removed the previously deprecated ``select`` method of ``DataFrame`` and ``Series`` (:issue:`17633`) +- Removed the previously deprecated behavior of :class:`Series` treated as list-like in :meth:`~Series.cat.rename_categories` (:issue:`17982`) + .. _whatsnew_0250.performance: diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index a1d591458fba3..3ef2f41f25338 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -887,11 +887,6 @@ def rename_categories(self, new_categories, inplace=False): .. versionadded:: 0.23.0 - .. warning:: - - Currently, Series are considered list like. In a future version - of pandas they'll be considered dict-like. - inplace : bool, default False Whether or not to rename the categories inplace or return a copy of this categorical with renamed categories. @@ -939,15 +934,6 @@ def rename_categories(self, new_categories, inplace=False): inplace = validate_bool_kwarg(inplace, 'inplace') cat = self if inplace else self.copy() - if isinstance(new_categories, ABCSeries): - msg = ("Treating Series 'new_categories' as a list-like and using " - "the values. In a future version, 'rename_categories' will " - "treat Series like a dictionary.\n" - "For dict-like, use 'new_categories.to_dict()'\n" - "For list-like, use 'new_categories.values'.") - warn(msg, FutureWarning, stacklevel=2) - new_categories = list(new_categories) - if is_dict_like(new_categories): cat.categories = [new_categories.get(item, item) for item in cat.categories] diff --git a/pandas/tests/arrays/categorical/test_api.py b/pandas/tests/arrays/categorical/test_api.py index 15e4bbab8f649..4be3919f173c4 100644 --- a/pandas/tests/arrays/categorical/test_api.py +++ b/pandas/tests/arrays/categorical/test_api.py @@ -91,12 +91,7 @@ def test_rename_categories(self): def test_rename_categories_series(self): # https://github.com/pandas-dev/pandas/issues/17981 c = Categorical(['a', 'b']) - xpr = "Treating Series 'new_categories' as a list-like " - with tm.assert_produces_warning(FutureWarning) as rec: - result = c.rename_categories(Series([0, 1])) - - assert len(rec) == 1 - assert xpr in str(rec[0].message) + result = c.rename_categories(Series([0, 1], index=['a', 'b'])) expected = Categorical([0, 1]) tm.assert_categorical_equal(result, expected) From 65d4d77daa34aa88723dad0ecb27b37d9e1c07a7 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sat, 29 Jun 2019 10:02:09 -0500 Subject: [PATCH 2/4] Remove reindex_axis and rename_axis behavior --- doc/source/reference/frame.rst | 1 - doc/source/whatsnew/v0.25.0.rst | 3 +- pandas/core/frame.py | 7 -- pandas/core/generic.py | 101 +----------------- pandas/core/panel.py | 7 -- pandas/core/series.py | 21 ---- pandas/tests/frame/test_alter_axes.py | 14 +-- .../tests/frame/test_axis_select_reindex.py | 38 ------- pandas/tests/sparse/series/test_series.py | 8 -- 9 files changed, 10 insertions(+), 190 deletions(-) diff --git a/doc/source/reference/frame.rst b/doc/source/reference/frame.rst index 6ae2ea6e392e6..1c0e6a3a25224 100644 --- a/doc/source/reference/frame.rst +++ b/doc/source/reference/frame.rst @@ -198,7 +198,6 @@ Reindexing / selection / label manipulation DataFrame.idxmin DataFrame.last DataFrame.reindex - DataFrame.reindex_axis DataFrame.reindex_like DataFrame.rename DataFrame.rename_axis diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 29682dddd3189..6b31e0089f230 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -628,7 +628,8 @@ Removal of prior version deprecations/changes - Removed the previously deprecated ``convert_objects`` (:issue:`11221`) - Removed the previously deprecated ``select`` method of ``DataFrame`` and ``Series`` (:issue:`17633`) - Removed the previously deprecated behavior of :class:`Series` treated as list-like in :meth:`~Series.cat.rename_categories` (:issue:`17982`) - +- Removed the previously deprecated :meth:`~DataFrame.reindex_axis`` (:issue:`17842`) +- Removed the previously deprecated behavior of altering column or index labels with :meth:`Series.rename_axis` or :meth:`DataFrame.rename_axis` (:issue:`17842`) .. _whatsnew_0250.performance: diff --git a/pandas/core/frame.py b/pandas/core/frame.py index df7003ecf000e..f5a5be5d20932 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3768,13 +3768,6 @@ def reindex(self, *args, **kwargs): kwargs.pop('labels', None) return super().reindex(**kwargs) - @Appender(_shared_docs['reindex_axis'] % _shared_doc_kwargs) - def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True, - limit=None, fill_value=np.nan): - return super().reindex_axis(labels=labels, axis=axis, method=method, - level=level, copy=copy, limit=limit, - fill_value=fill_value) - def drop(self, labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise'): """ diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 3bc7bbb633aed..46d990597355f 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1176,11 +1176,6 @@ def rename_axis(self, mapper=sentinel, **kwargs): Notes ----- - Prior to version 0.21.0, ``rename_axis`` could also be used to change - the axis *labels* by passing a mapping or scalar. This behavior is - deprecated and will be removed in a future version. Use ``rename`` - instead. - ``DataFrame.rename_axis`` supports two calling conventions * ``(index=index_mapper, columns=columns_mapper, ...)`` @@ -1280,22 +1275,15 @@ class name inplace = validate_bool_kwarg(inplace, 'inplace') - if (mapper is not sentinel): + if mapper is not sentinel: # Use v0.23 behavior if a scalar or list non_mapper = is_scalar(mapper) or (is_list_like(mapper) and not is_dict_like(mapper)) if non_mapper: return self._set_axis_name(mapper, axis=axis, inplace=inplace) else: - # Deprecated (v0.21) behavior is if mapper is specified, - # and not a list or scalar, then call rename - msg = ("Using 'rename_axis' to alter labels is deprecated. " - "Use '.rename' instead") - warnings.warn(msg, FutureWarning, stacklevel=3) - axis = self._get_axis_name(axis) - d = {'copy': copy, 'inplace': inplace} - d[axis] = mapper - return self.rename(**d) + raise ValueError("Use `.rename` to alter labels " + "with a mapper.") else: # Use new behavior. Means that index and/or columns # is specified @@ -4378,89 +4366,6 @@ def _needs_reindex_multi(self, axes, method, level): def _reindex_multi(self, axes, copy, fill_value): return NotImplemented - _shared_docs['reindex_axis'] = (""" - Conform input object to new index. - - .. deprecated:: 0.21.0 - Use `reindex` instead. - - By default, places NaN in locations having no value in the - previous index. A new object is produced unless the new index - is equivalent to the current one and copy=False. - - Parameters - ---------- - labels : array-like - New labels / index to conform to. Preferably an Index object to - avoid duplicating data. - axis : %(axes_single_arg)s - Indicate whether to use rows or columns. - method : {None, 'backfill'/'bfill', 'pad'/'ffill', 'nearest'}, optional - Method to use for filling holes in reindexed DataFrame: - - * default: don't fill gaps. - * pad / ffill: propagate last valid observation forward to next - valid. - * backfill / bfill: use next valid observation to fill gap. - * nearest: use nearest valid observations to fill gap. - - level : int or str - Broadcast across a level, matching Index values on the - passed MultiIndex level. - copy : bool, default True - Return a new object, even if the passed indexes are the same. - limit : int, optional - Maximum number of consecutive elements to forward or backward fill. - fill_value : float, default NaN - Value used to fill in locations having no value in the previous - index. - - .. versionadded:: 0.21.0 (list-like tolerance) - - Returns - ------- - %(klass)s - Returns a new DataFrame object with new indices, unless the new - index is equivalent to the current one and copy=False. - - See Also - -------- - DataFrame.set_index : Set row labels. - DataFrame.reset_index : Remove row labels or move them to new columns. - DataFrame.reindex : Change to new indices or expand indices. - DataFrame.reindex_like : Change to same indices as other DataFrame. - - Examples - -------- - >>> df = pd.DataFrame({'num_legs': [4, 2], 'num_wings': [0, 2]}, - ... index=['dog', 'hawk']) - >>> df - num_legs num_wings - dog 4 0 - hawk 2 2 - >>> df.reindex(['num_wings', 'num_legs', 'num_heads'], - ... axis='columns') - num_wings num_legs num_heads - dog 0 4 NaN - hawk 2 2 NaN - """) - - @Appender(_shared_docs['reindex_axis'] % _shared_doc_kwargs) - def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True, - limit=None, fill_value=None): - msg = ("'.reindex_axis' is deprecated and will be removed in a future " - "version. Use '.reindex' instead.") - self._consolidate_inplace() - - axis_name = self._get_axis_name(axis) - axis_values = self._get_axis(axis_name) - method = missing.clean_reindex_fill_method(method) - warnings.warn(msg, FutureWarning, stacklevel=3) - new_index, indexer = axis_values.reindex(labels, method, level, - limit=limit) - return self._reindex_with_indexers({axis: [new_index, indexer]}, - fill_value=fill_value, copy=copy) - def _reindex_with_indexers(self, reindexers, fill_value=None, copy=False, allow_dups=False): """allow_dups indicates an internal call here """ diff --git a/pandas/core/panel.py b/pandas/core/panel.py index c0340fc975a7e..c4623655dd812 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -1244,13 +1244,6 @@ def rename(self, items=None, major_axis=None, minor_axis=None, **kwargs): return super().rename(items=items, major_axis=major_axis, minor_axis=minor_axis, **kwargs) - @Appender(_shared_docs['reindex_axis'] % _shared_doc_kwargs) - def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True, - limit=None, fill_value=np.nan): - return super().reindex_axis(labels=labels, axis=axis, method=method, - level=level, copy=copy, limit=limit, - fill_value=fill_value) - @Substitution(**_shared_doc_kwargs) @Appender(NDFrame.transpose.__doc__) def transpose(self, *args, **kwargs): diff --git a/pandas/core/series.py b/pandas/core/series.py index 31cb7432b3ae1..7df01ffda0b8b 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -3998,27 +3998,6 @@ def shift(self, periods=1, freq=None, axis=0, fill_value=None): return super().shift(periods=periods, freq=freq, axis=axis, fill_value=fill_value) - def reindex_axis(self, labels, axis=0, **kwargs): - """ - Conform Series to new index with optional filling logic. - - .. deprecated:: 0.21.0 - Use ``Series.reindex`` instead. - - Returns - ------- - Series - Reindexed Series. - """ - # for compatibility with higher dims - if axis != 0: - raise ValueError("cannot reindex series on non-zero axis!") - msg = ("'.reindex_axis' is deprecated and will be removed in a future " - "version. Use '.reindex' instead.") - warnings.warn(msg, FutureWarning, stacklevel=2) - - return self.reindex(index=labels, **kwargs) - def memory_usage(self, index=True, deep=False): """ Return the memory usage of the Series. diff --git a/pandas/tests/frame/test_alter_axes.py b/pandas/tests/frame/test_alter_axes.py index 303604ba7d7ea..e7b4c2c65b842 100644 --- a/pandas/tests/frame/test_alter_axes.py +++ b/pandas/tests/frame/test_alter_axes.py @@ -668,24 +668,20 @@ def test_rename_axis_inplace(self, float_frame): assert no_return is None tm.assert_frame_equal(result, expected) - def test_rename_axis_warns(self): + def test_rename_axis_raises(self): # https://github.com/pandas-dev/pandas/issues/17833 df = DataFrame({"A": [1, 2], "B": [1, 2]}) - with tm.assert_produces_warning(FutureWarning) as w: + with pytest.raises(ValueError, match="Use `.rename`"): df.rename_axis(id, axis=0) - assert 'rename' in str(w[0].message) - with tm.assert_produces_warning(FutureWarning) as w: + with pytest.raises(ValueError, match="Use `.rename`"): df.rename_axis({0: 10, 1: 20}, axis=0) - assert 'rename' in str(w[0].message) - with tm.assert_produces_warning(FutureWarning) as w: + with pytest.raises(ValueError, match="Use `.rename`"): df.rename_axis(id, axis=1) - assert 'rename' in str(w[0].message) - with tm.assert_produces_warning(FutureWarning) as w: + with pytest.raises(ValueError, match="Use `.rename`"): df['A'].rename_axis(id) - assert 'rename' in str(w[0].message) def test_rename_axis_mapper(self): # GH 19978 diff --git a/pandas/tests/frame/test_axis_select_reindex.py b/pandas/tests/frame/test_axis_select_reindex.py index b4fde43ff3055..18c95beb62a13 100644 --- a/pandas/tests/frame/test_axis_select_reindex.py +++ b/pandas/tests/frame/test_axis_select_reindex.py @@ -416,17 +416,6 @@ def test_reindex_fill_value(self): expected[4] = 'foo' assert_frame_equal(result, expected) - # reindex_axis - with tm.assert_produces_warning(FutureWarning): - result = df.reindex_axis(range(15), fill_value=0., axis=0) - expected = df.reindex(range(15)).fillna(0) - assert_frame_equal(result, expected) - - with tm.assert_produces_warning(FutureWarning): - result = df.reindex_axis(range(5), fill_value=0., axis=1) - expected = df.reindex(columns=range(5)).fillna(0) - assert_frame_equal(result, expected) - # other dtypes df['foo'] = 'foo' result = df.reindex(range(15), fill_value=0) @@ -1026,33 +1015,6 @@ def test_reindex_corner(self, int_frame): smaller = int_frame.reindex(columns=['A', 'B', 'E']) assert smaller['E'].dtype == np.float64 - def test_reindex_axis(self, float_frame, int_frame): - cols = ['A', 'B', 'E'] - with tm.assert_produces_warning(FutureWarning) as m: - reindexed1 = int_frame.reindex_axis(cols, axis=1) - assert 'reindex' in str(m[0].message) - reindexed2 = int_frame.reindex(columns=cols) - assert_frame_equal(reindexed1, reindexed2) - - rows = int_frame.index[0:5] - with tm.assert_produces_warning(FutureWarning) as m: - reindexed1 = int_frame.reindex_axis(rows, axis=0) - assert 'reindex' in str(m[0].message) - reindexed2 = int_frame.reindex(index=rows) - assert_frame_equal(reindexed1, reindexed2) - - msg = ("No axis named 2 for object type" - " ") - with pytest.raises(ValueError, match=msg): - int_frame.reindex_axis(rows, axis=2) - - # no-op case - cols = float_frame.columns.copy() - with tm.assert_produces_warning(FutureWarning) as m: - newFrame = float_frame.reindex_axis(cols, axis=1) - assert 'reindex' in str(m[0].message) - assert_frame_equal(newFrame, float_frame) - def test_reindex_with_nans(self): df = DataFrame([[1, 2], [3, 4], [np.nan, np.nan], [7, 8], [9, 10]], columns=['a', 'b'], diff --git a/pandas/tests/sparse/series/test_series.py b/pandas/tests/sparse/series/test_series.py index 9ce1133cb39ca..290e0203567db 100644 --- a/pandas/tests/sparse/series/test_series.py +++ b/pandas/tests/sparse/series/test_series.py @@ -1516,14 +1516,6 @@ def test_deprecated_numpy_func_call(self): raise_on_extra_warnings=False): getattr(getattr(self, series), func)() - def test_deprecated_reindex_axis(self): - # https://github.com/pandas-dev/pandas/issues/17833 - # Multiple FutureWarnings, can't check stacklevel - with tm.assert_produces_warning(FutureWarning, - check_stacklevel=False) as m: - self.bseries.reindex_axis([0, 1, 2]) - assert 'reindex' in str(m[0].message) - @pytest.mark.parametrize( 'datetime_type', (np.datetime64, From 112014b62fe06cfbfc341fc17645f7c0b85476e6 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sat, 29 Jun 2019 10:08:35 -0500 Subject: [PATCH 3/4] Add Series.reindex_axis --- doc/source/whatsnew/v0.25.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 6b31e0089f230..b57c2d2b3ad51 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -628,7 +628,7 @@ Removal of prior version deprecations/changes - Removed the previously deprecated ``convert_objects`` (:issue:`11221`) - Removed the previously deprecated ``select`` method of ``DataFrame`` and ``Series`` (:issue:`17633`) - Removed the previously deprecated behavior of :class:`Series` treated as list-like in :meth:`~Series.cat.rename_categories` (:issue:`17982`) -- Removed the previously deprecated :meth:`~DataFrame.reindex_axis`` (:issue:`17842`) +- Removed the previously deprecated :meth:`~DataFrame.reindex_axis` and :meth:`~Series.reindex_axis`` (:issue:`17842`) - Removed the previously deprecated behavior of altering column or index labels with :meth:`Series.rename_axis` or :meth:`DataFrame.rename_axis` (:issue:`17842`) .. _whatsnew_0250.performance: From a89b600bfaea734af0f0ae32369a25dc674ef9d5 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sat, 29 Jun 2019 10:51:04 -0500 Subject: [PATCH 4/4] Fix whatsnew and import error --- doc/source/whatsnew/v0.25.0.rst | 2 +- pandas/core/panel.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index b57c2d2b3ad51..815e13733f7a2 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -628,7 +628,7 @@ Removal of prior version deprecations/changes - Removed the previously deprecated ``convert_objects`` (:issue:`11221`) - Removed the previously deprecated ``select`` method of ``DataFrame`` and ``Series`` (:issue:`17633`) - Removed the previously deprecated behavior of :class:`Series` treated as list-like in :meth:`~Series.cat.rename_categories` (:issue:`17982`) -- Removed the previously deprecated :meth:`~DataFrame.reindex_axis` and :meth:`~Series.reindex_axis`` (:issue:`17842`) +- Removed the previously deprecated ``DataFrame.reindex_axis`` and ``Series.reindex_axis``` (:issue:`17842`) - Removed the previously deprecated behavior of altering column or index labels with :meth:`Series.rename_axis` or :meth:`DataFrame.rename_axis` (:issue:`17842`) .. _whatsnew_0250.performance: diff --git a/pandas/core/panel.py b/pandas/core/panel.py index c4623655dd812..350c3083623eb 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -18,7 +18,7 @@ import pandas.core.common as com from pandas.core.frame import DataFrame -from pandas.core.generic import NDFrame, _shared_docs +from pandas.core.generic import NDFrame from pandas.core.index import ( Index, MultiIndex, _get_objs_combined_axis, ensure_index) import pandas.core.indexes.base as ibase