-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
API: Deprecate renamae_axis and reindex_axis #17842
Changes from 1 commit
8c54f26
9776ada
d4baabf
f40875b
1d1c269
9e915f9
50499e4
a92f492
c780537
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,7 @@ def _align_core(terms): | |
for axis, items in zip(range(ndim), axes): | ||
ti = terms[i].value | ||
|
||
# TODO: handle this for when reindex_axis is removed... | ||
if hasattr(ti, 'reindex_axis'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and using 'reindex' instead of 'reindex_axis' is not good enough? |
||
transpose = isinstance(ti, pd.Series) and naxes > 1 | ||
reindexer = axes[naxes - 1] if transpose else items | ||
|
@@ -104,11 +105,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()) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,7 +129,7 @@ def __init__(self, data, axes=None, copy=False, dtype=None, | |
|
||
if axes is not None: | ||
for i, ax in enumerate(axes): | ||
data = data.reindex_axis(ax, axis=i) | ||
data = data.reindex(ax, axis=i) | ||
|
||
object.__setattr__(self, 'is_copy', None) | ||
object.__setattr__(self, '_data', data) | ||
|
@@ -893,17 +893,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,6 +908,13 @@ 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sidenote: this is useless (does not make a link). Unless you want to start with shared docstring/substitution, I would just explicitly add both for series/frame: |
||
|
@@ -922,29 +924,29 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False): | |
-------- | ||
|
||
>>> 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 | ||
is_dict_like(mapper)) | ||
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 +2983,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 +3092,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]}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'NDFrame' is not something that exists in the documentation. You can just use
DataFrame
(it will not be shown because of the~
, and the docstrings are the same for series/dataframe)