From a4a11a1dc1dfa9f61cdb5e0384d6fc34c81a7ecb Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Wed, 11 Oct 2017 10:18:05 -0500 Subject: [PATCH] API: Deprecate renamae_axis and reindex_axis (#17842) * API: Deprecate renamae_axis and reindex_axis Closes https://github.com/pandas-dev/pandas/issues/17833 * REF: Refactor axis style validator to generic This sets us up to re-use it for Panel reindex * fixup! API: Deprecate renamae_axis and reindex_axis * fixup! API: Deprecate renamae_axis and reindex_axis * fixup! API: Deprecate renamae_axis and reindex_axis * fixup! API: Deprecate renamae_axis and reindex_axis * Ugh * fixup! API: Deprecate renamae_axis and reindex_axis * Fixup --- doc/source/basics.rst | 7 +- doc/source/whatsnew/v0.21.0.txt | 3 + pandas/core/computation/align.py | 8 +- pandas/core/frame.py | 64 +++---------- pandas/core/generic.py | 92 +++++++++++++++---- pandas/core/groupby.py | 2 +- pandas/core/indexing.py | 4 +- pandas/core/internals.py | 4 +- pandas/core/panel.py | 14 ++- pandas/core/panel4d.py | 14 +++ pandas/core/reshape/pivot.py | 4 +- pandas/core/series.py | 4 + pandas/core/sparse/scipy_sparse.py | 2 +- pandas/io/pytables.py | 6 +- pandas/plotting/_core.py | 2 +- pandas/tests/frame/test_alter_axes.py | 19 ++++ .../tests/frame/test_axis_select_reindex.py | 18 +++- pandas/tests/reshape/test_pivot.py | 4 +- pandas/tests/reshape/test_reshape.py | 6 +- pandas/tests/sparse/test_series.py | 6 ++ pandas/tests/test_multilevel.py | 2 +- pandas/tests/test_panel.py | 16 ++++ pandas/tests/test_resample.py | 4 +- 23 files changed, 199 insertions(+), 106 deletions(-) diff --git a/doc/source/basics.rst b/doc/source/basics.rst index be9d1a5d83b85..3044a8886b9ae 100644 --- a/doc/source/basics.rst +++ b/doc/source/basics.rst @@ -1201,8 +1201,11 @@ With a DataFrame, you can simultaneously reindex the index and columns: df df.reindex(index=['c', 'f', 'b'], columns=['three', 'two', 'one']) -For convenience, you may utilize the :meth:`~Series.reindex_axis` method, which -takes the labels and a keyword ``axis`` parameter. +You may also use ``reindex`` with an ``axis`` keyword: + +.. ipython:: python + + df.reindex(index=['c', 'f', 'b'], axis='index') Note that the ``Index`` objects containing the actual axis labels can be **shared** between objects. So if we have a Series and a DataFrame, the diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index f04410ef63531..2bee7cf5cdddc 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -810,6 +810,8 @@ Deprecations - ``.get_value`` and ``.set_value`` on ``Series``, ``DataFrame``, ``Panel``, ``SparseSeries``, and ``SparseDataFrame`` are deprecated in favor of using ``.iat[]`` or ``.at[]`` accessors (:issue:`15269`) - Passing a non-existent column in ``.to_excel(..., columns=)`` is deprecated and will raise a ``KeyError`` in the future (:issue:`17295`) - ``raise_on_error`` parameter to :func:`Series.where`, :func:`Series.mask`, :func:`DataFrame.where`, :func:`DataFrame.mask` is deprecated, in favor of ``errors=`` (:issue:`14968`) +- Using :meth:`DataFrame.rename_axis` and :meth:`Series.rename_axis` to alter index or column *labels* is now deprecated in favor of using ``.rename``. ``rename_axis`` may still be used to alter the name of the index or columns (:issue:`17833`). +- :meth:`~DataFrame.reindex_axis` has been deprecated in favor of :meth:`~DataFrame.reindex`. See :ref`here` for more (:issue:`17833`). .. _whatsnew_0210.deprecations.select: @@ -998,6 +1000,7 @@ Reshaping - Bug in :func:`unique` where checking a tuple of strings raised a ``TypeError`` (:issue:`17108`) - Bug in :func:`concat` where order of result index was unpredictable if it contained non-comparable elements (:issue:`17344`) - Fixes regression when sorting by multiple columns on a ``datetime64`` dtype ``Series`` with ``NaT`` values (:issue:`16836`) +- Bug in :fun:`pivot_table` where the result's columns did not preserve the categorical dtype of ``columns`` when ``dropna`` was ``False`` (:issue:`17842`) Numeric ^^^^^^^ diff --git a/pandas/core/computation/align.py b/pandas/core/computation/align.py index 691eaebfd5fc1..0e7ae0cbe7c87 100644 --- a/pandas/core/computation/align.py +++ b/pandas/core/computation/align.py @@ -89,7 +89,7 @@ def _align_core(terms): for axis, items in zip(range(ndim), axes): ti = terms[i].value - if hasattr(ti, 'reindex_axis'): + if hasattr(ti, 'reindex'): transpose = isinstance(ti, pd.Series) and naxes > 1 reindexer = axes[naxes - 1] if transpose else items @@ -104,11 +104,7 @@ def _align_core(terms): ).format(axis=axis, term=terms[i].name, ordm=ordm) warnings.warn(w, category=PerformanceWarning, stacklevel=6) - if transpose: - f = partial(ti.reindex, index=reindexer, copy=False) - else: - f = partial(ti.reindex_axis, reindexer, axis=axis, - copy=False) + f = partial(ti.reindex, reindexer, axis=axis, copy=False) terms[i].update(f()) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 94ff70f287fbe..c7e8c0da75e2c 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -65,7 +65,6 @@ _values_from_object, _maybe_box_datetimelike, _dict_compat, - _all_not_none, standardize_mapping) from pandas.core.generic import NDFrame, _shared_docs from pandas.core.index import (Index, MultiIndex, _ensure_index, @@ -2736,7 +2735,7 @@ def reindexer(value): if isinstance(loc, (slice, Series, np.ndarray, Index)): cols = maybe_droplevels(self.columns[loc], key) if len(cols) and not cols.equals(value.columns): - value = value.reindex_axis(cols, axis=1) + value = value.reindex(cols, axis=1) # now align rows value = reindexer(value).T @@ -2783,47 +2782,6 @@ def reindexer(value): return np.atleast_2d(np.asarray(value)) - def _validate_axis_style_args(self, arg, arg_name, index, columns, - axis, method_name): - if axis is not None: - # Using "axis" style, along with a positional arg - # Both index and columns should be None then - axis = self._get_axis_name(axis) - if index is not None or columns is not None: - msg = ( - "Can't specify both 'axis' and 'index' or 'columns'. " - "Specify either\n" - "\t.{method_name}.rename({arg_name}, axis=axis), or\n" - "\t.{method_name}.rename(index=index, columns=columns)" - ).format(arg_name=arg_name, method_name=method_name) - raise TypeError(msg) - if axis == 'index': - index = arg - elif axis == 'columns': - columns = arg - - elif _all_not_none(arg, index, columns): - msg = ( - "Cannot specify all of '{arg_name}', 'index', and 'columns'. " - "Specify either {arg_name} and 'axis', or 'index' and " - "'columns'." - ).format(arg_name=arg_name) - raise TypeError(msg) - - elif _all_not_none(arg, index): - # This is the "ambiguous" case, so emit a warning - msg = ( - "Interpreting call to '.{method_name}(a, b)' as " - "'.{method_name}(index=a, columns=b)'. " - "Use keyword arguments to remove any ambiguity." - ).format(method_name=method_name) - warnings.warn(msg, stacklevel=3) - index, columns = arg, index - elif index is None: - # This is for the default axis, like reindex([0, 1]) - index = arg - return index, columns - @property def _series(self): result = {} @@ -2952,11 +2910,11 @@ def align(self, other, join='outer', axis=None, level=None, copy=True, @Appender(_shared_docs['reindex'] % _shared_doc_kwargs) def reindex(self, labels=None, index=None, columns=None, axis=None, **kwargs): - index, columns = self._validate_axis_style_args(labels, 'labels', - index, columns, - axis, 'reindex') - return super(DataFrame, self).reindex(index=index, columns=columns, - **kwargs) + axes = self._validate_axis_style_args(labels, 'labels', + axes=[index, columns], + axis=axis, method_name='reindex') + kwargs.update(axes) + return super(DataFrame, self).reindex(**kwargs) @Appender(_shared_docs['reindex_axis'] % _shared_doc_kwargs) def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True, @@ -3041,11 +2999,11 @@ def rename(self, mapper=None, index=None, columns=None, axis=None, 2 2 5 4 3 6 """ - index, columns = self._validate_axis_style_args(mapper, 'mapper', - index, columns, - axis, 'rename') - return super(DataFrame, self).rename(index=index, columns=columns, - **kwargs) + axes = self._validate_axis_style_args(mapper, 'mapper', + axes=[index, columns], + axis=axis, method_name='rename') + kwargs.update(axes) + return super(DataFrame, self).rename(**kwargs) @Appender(_shared_docs['fillna'] % _shared_doc_kwargs) def fillna(self, value=None, method=None, axis=None, inplace=False, diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 9d9d8334fcaf4..5fe5718d46bcb 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -29,7 +29,8 @@ from pandas.core.dtypes.missing import isna, notna from pandas.core.dtypes.generic import ABCSeries, ABCPanel, ABCDataFrame -from pandas.core.common import (_values_from_object, +from pandas.core.common import (_all_not_none, + _values_from_object, _maybe_box_datetimelike, SettingWithCopyError, SettingWithCopyWarning, AbstractMethodError) @@ -729,6 +730,51 @@ def swaplevel(self, i=-2, j=-1, axis=0): result._data.set_axis(axis, labels.swaplevel(i, j)) return result + def _validate_axis_style_args(self, arg, arg_name, axes, + axis, method_name): + out = {} + for i, value in enumerate(axes): + if value is not None: + out[self._AXIS_NAMES[i]] = value + + aliases = ', '.join(self._AXIS_NAMES.values()) + if axis is not None: + # Using "axis" style, along with a positional arg + # Both index and columns should be None then + axis = self._get_axis_name(axis) + if any(x is not None for x in axes): + msg = ( + "Can't specify both 'axis' and {aliases}. " + "Specify either\n" + "\t.{method_name}({arg_name}, axis=axis), or\n" + "\t.{method_name}(index=index, columns=columns)" + ).format(arg_name=arg_name, method_name=method_name, + aliases=aliases) + raise TypeError(msg) + out[axis] = arg + + elif _all_not_none(arg, *axes): + msg = ( + "Cannot specify all of '{arg_name}', {aliases}. " + "Specify either {arg_name} and 'axis', or {aliases}." + ).format(arg_name=arg_name, aliases=aliases) + raise TypeError(msg) + + elif _all_not_none(arg, axes[0]): + # This is the "ambiguous" case, so emit a warning + msg = ( + "Interpreting call to '.{method_name}(a, b)' as " + "'.{method_name}(index=a, columns=b)'. " # TODO + "Use keyword arguments to remove any ambiguity." + ).format(method_name=method_name) + warnings.warn(msg, stacklevel=3) + out[self._AXIS_ORDERS[0]] = arg + out[self._AXIS_ORDERS[1]] = axes[0] + elif axes[0] is None: + # This is for the default axis, like reindex([0, 1]) + out[self._AXIS_ORDERS[0]] = arg + return out + # ---------------------------------------------------------------------- # Rename @@ -893,17 +939,12 @@ def f(x): rename.__doc__ = _shared_docs['rename'] def rename_axis(self, mapper, axis=0, copy=True, inplace=False): - """ - Alter index and / or columns using input function or functions. - A scalar or list-like for ``mapper`` will alter the ``Index.name`` - or ``MultiIndex.names`` attribute. - A function or dict for ``mapper`` will alter the labels. - Function / dict values must be unique (1-to-1). Labels not contained in - a dict / Series will be left as-is. + """Alter the name of the index or columns. Parameters ---------- - mapper : scalar, list-like, dict-like or function, optional + mapper : scalar, list-like, optional + Value to set the axis name attribute. axis : int or string, default 0 copy : boolean, default True Also copy underlying data @@ -913,31 +954,35 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False): ------- renamed : type of caller or None if inplace=True + 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. + See Also -------- - pandas.NDFrame.rename + pandas.Series.rename, pandas.DataFrame.rename pandas.Index.rename Examples -------- >>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) - >>> df.rename_axis("foo") # scalar, alters df.index.name + >>> df.rename_axis("foo") A B foo 0 1 4 1 2 5 2 3 6 - >>> df.rename_axis(lambda x: 2 * x) # function: alters labels - A B - 0 1 4 - 2 2 5 - 4 3 6 - >>> df.rename_axis({"A": "ehh", "C": "see"}, axis="columns") # mapping - ehh B + + >>> df.rename_axis("bar", axis="columns") + bar A B 0 1 4 1 2 5 2 3 6 + """ inplace = validate_bool_kwarg(inplace, 'inplace') non_mapper = is_scalar(mapper) or (is_list_like(mapper) and not @@ -945,6 +990,9 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False): if non_mapper: return self._set_axis_name(mapper, axis=axis, inplace=inplace) else: + msg = ("Using 'rename_axis' to alter labels is deprecated. " + "Use '.rename' instead") + warnings.warn(msg, FutureWarning, stacklevel=2) axis = self._get_axis_name(axis) d = {'copy': copy, 'inplace': inplace} d[axis] = mapper @@ -2981,6 +3029,11 @@ def reindex(self, *args, **kwargs): tolerance = kwargs.pop('tolerance', None) fill_value = kwargs.pop('fill_value', np.nan) + # Series.reindex doesn't use / need the axis kwarg + # We pop and ignore it here, to make writing Series/Frame generic code + # easier + kwargs.pop("axis", None) + if kwargs: raise TypeError('reindex() got an unexpected keyword ' 'argument "{0}"'.format(list(kwargs.keys())[0])) @@ -3085,11 +3138,14 @@ def _reindex_multi(self, axes, copy, fill_value): @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): + 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]}, diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index 9518f17e5f4f1..ccaf90b4482a7 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -901,7 +901,7 @@ def reset_identity(values): result.index.get_indexer_for(ax.values)) result = result.take(indexer, axis=self.axis) else: - result = result.reindex_axis(ax, axis=self.axis) + result = result.reindex(ax, axis=self.axis) elif self.group_keys: diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index f1a3fe81a4540..654c3510b7cf7 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -368,7 +368,7 @@ def _setitem_with_indexer(self, indexer, value): # so the object is the same index = self.obj._get_axis(i) labels = index.insert(len(index), key) - self.obj._data = self.obj.reindex_axis(labels, i)._data + self.obj._data = self.obj.reindex(labels, axis=i)._data self.obj._maybe_update_cacher(clear=True) self.obj.is_copy = None @@ -1132,7 +1132,7 @@ def _getitem_iterable(self, key, axis=None): if labels.is_unique and Index(keyarr).is_unique: try: - return self.obj.reindex_axis(keyarr, axis=axis) + return self.obj.reindex(keyarr, axis=axis) except AttributeError: # Series diff --git a/pandas/core/internals.py b/pandas/core/internals.py index 689f5521e1ccb..879859309c4f9 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -3283,8 +3283,8 @@ def apply(self, f, axes=None, filter=None, do_integrity_check=False, for k, obj in aligned_args.items(): axis = getattr(obj, '_info_axis_number', 0) - kwargs[k] = obj.reindex_axis(b_items, axis=axis, - copy=align_copy) + kwargs[k] = obj.reindex(b_items, axis=axis, + copy=align_copy) kwargs['mgr'] = self applied = getattr(b, f)(**kwargs) diff --git a/pandas/core/panel.py b/pandas/core/panel.py index b2f50eaf733d8..1f22cb49d0196 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -1197,13 +1197,21 @@ def _wrap_result(self, result, axis): return self._construct_return_type(result, axes) @Appender(_shared_docs['reindex'] % _shared_doc_kwargs) - def reindex(self, items=None, major_axis=None, minor_axis=None, **kwargs): + def reindex(self, labels=None, + items=None, major_axis=None, minor_axis=None, + axis=None, **kwargs): major_axis = (major_axis if major_axis is not None else kwargs.pop('major', None)) minor_axis = (minor_axis if minor_axis is not None else kwargs.pop('minor', None)) - return super(Panel, self).reindex(items=items, major_axis=major_axis, - minor_axis=minor_axis, **kwargs) + axes = self._validate_axis_style_args( + labels, 'labels', axes=[items, major_axis, minor_axis], + axis=axis, method_name='reindex') + if self.ndim >= 4: + # Hack for PanelND + axes = {} + kwargs.update(axes) + return super(Panel, self).reindex(**kwargs) @Appender(_shared_docs['rename'] % _shared_doc_kwargs) def rename(self, items=None, major_axis=None, minor_axis=None, **kwargs): diff --git a/pandas/core/panel4d.py b/pandas/core/panel4d.py index 16e7d0dfcc336..e6914fb268359 100644 --- a/pandas/core/panel4d.py +++ b/pandas/core/panel4d.py @@ -57,4 +57,18 @@ def panel4d_init(self, data=None, labels=None, items=None, major_axis=None, dtype=dtype) +def panel4d_reindex(self, labs=None, labels=None, items=None, major_axis=None, + minor_axis=None, axis=None, **kwargs): + # Hack for reindex_axis deprecation + # Ha, we used labels for two different things + # I think this will work still. + axes = self._validate_axis_style_args( + labs, 'labels', + axes=[labels, items, major_axis, minor_axis], + axis=axis, method_name='reindex') + kwargs.update(axes) + return super(Panel, self).reindex(**kwargs) + + Panel4D.__init__ = panel4d_init +Panel4D.reindex = panel4d_reindex diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index 38c28af4d6ecb..7ee021e5c6246 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -101,14 +101,14 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', try: m = MultiIndex.from_arrays(cartesian_product(table.index.levels), names=table.index.names) - table = table.reindex_axis(m, axis=0) + table = table.reindex(m, axis=0) except AttributeError: pass # it's a single level try: m = MultiIndex.from_arrays(cartesian_product(table.columns.levels), names=table.columns.names) - table = table.reindex_axis(m, axis=1) + table = table.reindex(m, axis=1) except AttributeError: pass # it's a single level or a series diff --git a/pandas/core/series.py b/pandas/core/series.py index 93afdc5151b35..8499f8b55d2d0 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2615,6 +2615,10 @@ def reindex_axis(self, labels, axis=0, **kwargs): """ 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): diff --git a/pandas/core/sparse/scipy_sparse.py b/pandas/core/sparse/scipy_sparse.py index d2b9583d8efe5..748a52f484893 100644 --- a/pandas/core/sparse/scipy_sparse.py +++ b/pandas/core/sparse/scipy_sparse.py @@ -134,5 +134,5 @@ def _coo_to_sparse_series(A, dense_index=False): i = range(A.shape[0]) j = range(A.shape[1]) ind = MultiIndex.from_product([i, j]) - s = s.reindex_axis(ind) + s = s.reindex(ind) return s diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index ca1b4d031d3ce..39d088e00b219 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -1040,7 +1040,7 @@ def append_to_multiple(self, d, value, selector, data_columns=None, dc = data_columns if k == selector else None # compute the val - val = value.reindex_axis(v, axis=axis) + val = value.reindex(v, axis=axis) self.append(k, val, data_columns=dc, **kwargs) @@ -3493,7 +3493,7 @@ def get_blk_items(mgr, blocks): data_columns = self.validate_data_columns( data_columns, min_itemsize) if len(data_columns): - mgr = block_obj.reindex_axis( + mgr = block_obj.reindex( Index(axis_labels).difference(Index(data_columns)), axis=axis )._data @@ -3501,7 +3501,7 @@ def get_blk_items(mgr, blocks): blocks = list(mgr.blocks) blk_items = get_blk_items(mgr, blocks) for c in data_columns: - mgr = block_obj.reindex_axis([c], axis=axis)._data + mgr = block_obj.reindex([c], axis=axis)._data blocks.extend(mgr.blocks) blk_items.extend(get_blk_items(mgr, mgr.blocks)) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index c4cd562df7eb3..0d77b5f41a08e 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -697,7 +697,7 @@ def _parse_errorbars(self, label, err): from pandas import DataFrame, Series def match_labels(data, e): - e = e.reindex_axis(data.index) + e = e.reindex(data.index) return e # key-matched DataFrame diff --git a/pandas/tests/frame/test_alter_axes.py b/pandas/tests/frame/test_alter_axes.py index feb32324ff1b1..84f7dd108f2cb 100644 --- a/pandas/tests/frame/test_alter_axes.py +++ b/pandas/tests/frame/test_alter_axes.py @@ -436,6 +436,25 @@ def test_rename_axis_inplace(self): assert no_return is None assert_frame_equal(result, expected) + def test_rename_axis_warns(self): + # https://github.com/pandas-dev/pandas/issues/17833 + df = pd.DataFrame({"A": [1, 2], "B": [1, 2]}) + with tm.assert_produces_warning(FutureWarning) as w: + df.rename_axis(id, axis=0) + assert 'rename' in str(w[0].message) + + with tm.assert_produces_warning(FutureWarning) as w: + df.rename_axis({0: 10, 1: 20}, axis=0) + assert 'rename' in str(w[0].message) + + with tm.assert_produces_warning(FutureWarning) as w: + df.rename_axis(id, axis=1) + assert 'rename' in str(w[0].message) + + with tm.assert_produces_warning(FutureWarning) as w: + df['A'].rename_axis(id) + assert 'rename' in str(w[0].message) + def test_rename_multiindex(self): tuples_index = [('foo1', 'bar1'), ('foo2', 'bar2')] diff --git a/pandas/tests/frame/test_axis_select_reindex.py b/pandas/tests/frame/test_axis_select_reindex.py index 38ed8ee20bc50..fee0c8b213bd9 100644 --- a/pandas/tests/frame/test_axis_select_reindex.py +++ b/pandas/tests/frame/test_axis_select_reindex.py @@ -418,11 +418,13 @@ def test_reindex_fill_value(self): assert_frame_equal(result, expected) # reindex_axis - result = df.reindex_axis(lrange(15), fill_value=0., axis=0) + with tm.assert_produces_warning(FutureWarning): + result = df.reindex_axis(lrange(15), fill_value=0., axis=0) expected = df.reindex(lrange(15)).fillna(0) assert_frame_equal(result, expected) - result = df.reindex_axis(lrange(5), fill_value=0., axis=1) + with tm.assert_produces_warning(FutureWarning): + result = df.reindex_axis(lrange(5), fill_value=0., axis=1) expected = df.reindex(columns=lrange(5)).fillna(0) assert_frame_equal(result, expected) @@ -1030,12 +1032,16 @@ def test_reindex_corner(self): def test_reindex_axis(self): cols = ['A', 'B', 'E'] - reindexed1 = self.intframe.reindex_axis(cols, axis=1) + with tm.assert_produces_warning(FutureWarning) as m: + reindexed1 = self.intframe.reindex_axis(cols, axis=1) + assert 'reindex' in str(m[0].message) reindexed2 = self.intframe.reindex(columns=cols) assert_frame_equal(reindexed1, reindexed2) rows = self.intframe.index[0:5] - reindexed1 = self.intframe.reindex_axis(rows, axis=0) + with tm.assert_produces_warning(FutureWarning) as m: + reindexed1 = self.intframe.reindex_axis(rows, axis=0) + assert 'reindex' in str(m[0].message) reindexed2 = self.intframe.reindex(index=rows) assert_frame_equal(reindexed1, reindexed2) @@ -1043,7 +1049,9 @@ def test_reindex_axis(self): # no-op case cols = self.frame.columns.copy() - newFrame = self.frame.reindex_axis(cols, axis=1) + with tm.assert_produces_warning(FutureWarning) as m: + newFrame = self.frame.reindex_axis(cols, axis=1) + assert 'reindex' in str(m[0].message) assert_frame_equal(newFrame, self.frame) def test_reindex_with_nans(self): diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 4126bb1de84d7..90afd2e216045 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -116,7 +116,9 @@ def test_pivot_table_dropna_categoricals(self): result_false = df.pivot_table(index='B', columns='A', values='C', dropna=False) - expected_columns = Series(['a', 'b', 'c', 'd'], name='A') + expected_columns = ( + Series(['a', 'b', 'c', 'd'], name='A').astype('category') + ) expected_false = DataFrame([[0.0, 3.0, 6.0, np.NaN], [1.0, 4.0, 7.0, np.NaN], [2.0, 5.0, 8.0, np.NaN]], diff --git a/pandas/tests/reshape/test_reshape.py b/pandas/tests/reshape/test_reshape.py index 632d3b4ad2e7a..fc9f89934b4ea 100644 --- a/pandas/tests/reshape/test_reshape.py +++ b/pandas/tests/reshape/test_reshape.py @@ -311,7 +311,7 @@ def test_include_na(self): 'a': {0: 1, 1: 0, 2: 0}, 'b': {0: 0, 1: 1, 2: 0}}, dtype=np.uint8) - exp_na = exp_na.reindex_axis(['a', 'b', nan], 1) + exp_na = exp_na.reindex(['a', 'b', nan], axis=1) # hack (NaN handling in assert_index_equal) exp_na.columns = res_na.columns assert_frame_equal(res_na, exp_na) @@ -542,8 +542,8 @@ def test_basic_drop_first_NA(self): 2: 0}, nan: {0: 0, 1: 0, - 2: 1}}, dtype=np.uint8).reindex_axis( - ['b', nan], 1) + 2: 1}}, dtype=np.uint8).reindex( + ['b', nan], axis=1) assert_frame_equal(res_na, exp_na) res_just_na = get_dummies([nan], dummy_na=True, sparse=self.sparse, diff --git a/pandas/tests/sparse/test_series.py b/pandas/tests/sparse/test_series.py index 7c7399317809f..c218eee921bb1 100644 --- a/pandas/tests/sparse/test_series.py +++ b/pandas/tests/sparse/test_series.py @@ -1414,6 +1414,12 @@ def test_deprecated_numpy_func_call(self): check_stacklevel=False): getattr(getattr(self, series), func)() + def test_deprecated_reindex_axis(self): + # https://github.com/pandas-dev/pandas/issues/17833 + with tm.assert_produces_warning(FutureWarning) as m: + self.bseries.reindex_axis([0, 1, 2]) + assert 'reindex' in str(m[0].message) + @pytest.mark.parametrize( 'datetime_type', (np.datetime64, diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py index 94577db15f01a..785be71e236d7 100644 --- a/pandas/tests/test_multilevel.py +++ b/pandas/tests/test_multilevel.py @@ -1805,7 +1805,7 @@ def test_reindex_level_partial_selection(self): expected = self.frame.iloc[[0, 1, 2, 7, 8, 9]] tm.assert_frame_equal(result, expected) - result = self.frame.T.reindex_axis(['foo', 'qux'], axis=1, level=0) + result = self.frame.T.reindex(['foo', 'qux'], axis=1, level=0) tm.assert_frame_equal(result, expected.T) result = self.frame.loc[['foo', 'qux']] diff --git a/pandas/tests/test_panel.py b/pandas/tests/test_panel.py index 2769ec0d2dbed..da30c8c403d41 100644 --- a/pandas/tests/test_panel.py +++ b/pandas/tests/test_panel.py @@ -1444,6 +1444,22 @@ def test_reindex(self): assert_panel_equal(result, self.panel) assert result is self.panel + def test_reindex_axis_style(self): + with catch_warnings(record=True): + panel = Panel(np.random.rand(5, 5, 5)) + expected0 = Panel(panel.values).iloc[[0, 1]] + expected1 = Panel(panel.values).iloc[:, [0, 1]] + expected2 = Panel(panel.values).iloc[:, :, [0, 1]] + + result = panel.reindex([0, 1], axis=0) + assert_panel_equal(result, expected0) + + result = panel.reindex([0, 1], axis=1) + assert_panel_equal(result, expected1) + + result = panel.reindex([0, 1], axis=2) + assert_panel_equal(result, expected2) + def test_reindex_multi(self): with catch_warnings(record=True): diff --git a/pandas/tests/test_resample.py b/pandas/tests/test_resample.py index cd15203eccd82..4e26689badb3c 100644 --- a/pandas/tests/test_resample.py +++ b/pandas/tests/test_resample.py @@ -1422,7 +1422,7 @@ def test_resample_ohlc_dataframe(self): Timestamp('2011-01-06 10:59:05', tz=None): 1500000000, Timestamp('2011-01-06 12:43:33', tz=None): 5000000000, Timestamp('2011-01-06 12:54:09', tz=None): 100000000}}) - ).reindex_axis(['VOLUME', 'PRICE'], axis=1) + ).reindex(['VOLUME', 'PRICE'], axis=1) res = df.resample('H').ohlc() exp = pd.concat([df['VOLUME'].resample('H').ohlc(), df['PRICE'].resample('H').ohlc()], @@ -1652,7 +1652,7 @@ def test_resample_categorical_data_with_timedeltaindex(self): expected = DataFrame({'Group_obj': ['A', 'A'], 'Group': ['A', 'A']}, index=pd.to_timedelta([0, 10], unit='s')) - expected = expected.reindex_axis(['Group_obj', 'Group'], 1) + expected = expected.reindex(['Group_obj', 'Group'], axis=1) tm.assert_frame_equal(result, expected) def test_resample_daily_anchored(self):