-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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 6 commits
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 |
---|---|---|
|
@@ -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) | ||
|
@@ -129,7 +130,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(ax, axis=i) | ||
data = data.reindex_axis(ax, axis=i) | ||
|
||
object.__setattr__(self, 'is_copy', None) | ||
object.__setattr__(self, '_data', data) | ||
|
@@ -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)" # TODO | ||
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. does your 'todo' point to the fact that "index=index, columns=columns" is not generic for all NDFrame types ? (like Panels) If that is the case, I personally wouldn't care. 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. Yep, I'll happily ignore :) |
||
).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}" | ||
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. also missing ". " |
||
"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 | ||
|
||
|
@@ -917,7 +963,7 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False): | |
|
||
See Also | ||
-------- | ||
pandas.NDFrame.rename | ||
pandas.Series.rename, pandas.DataFrame.rename | ||
pandas.Index.rename | ||
|
||
Examples | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4580,7 +4580,7 @@ def _get_sorted_data(self): | |
|
||
# this is sort of wasteful but... | ||
sorted_axis = data.axes[self.axis].take(self.sort_idx) | ||
sorted_data = data.reindex(sorted_axis, axis=self.axis) | ||
sorted_data = data.reindex_axis(sorted_axis, axis=self.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. This won't cause a deprecation warning in our own code? 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.
|
||
|
||
return sorted_data | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -722,7 +722,7 @@ def dropna(self, axis=0, how='any', inplace=False): | |
cond = mask == per_slice | ||
|
||
new_ax = self._get_axis(axis)[cond] | ||
result = self.reindex(new_ax, axis=axis) | ||
result = self.reindex_axis(new_ax, axis=axis) | ||
if inplace: | ||
self._update_inplace(result) | ||
else: | ||
|
@@ -1197,13 +1197,20 @@ 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: | ||
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. I got a bit annoyed with PanelND :) I think this is acceptable until we remove it. |
||
# Hack for PanelND | ||
axes = {} | ||
return super(Panel, self).reindex(**axes, **kwargs) | ||
|
||
@Appender(_shared_docs['rename'] % _shared_doc_kwargs) | ||
def rename(self, items=None, major_axis=None, minor_axis=None, **kwargs): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = ( | ||
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. I wanted to flag this change in the tests. AFAICT, this is actually the desired behavior? Looking for issues about this now. |
||
Series(['a', 'b', 'c', 'd'], name='A').astype('category') | ||
) | ||
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. how is this change related? 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. Not sure :) The fact that it's only not a CategoricalIndex when in the columns and dropna=True seems like a bug on master. # This is on master
In [7]: df.pivot_table(index="B", columns="A", values="C", dropna=False).columns
Out[7]: Index(['a', 'b', 'c', 'd'], dtype='object', name='A')
In [8]: df.pivot_table(index="A", columns="B", values="C", dropna=False).index
Out[8]: CategoricalIndex(['a', 'b', 'c', 'd'], categories=['a', 'b', 'c', 'd'], ordered=False, name='A', dtype='category') I've added a whatsnew. |
||
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]], | ||
|
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.
add ". " at the end of this string? (point + space)