|
41 | 41 | is_datetimetz, |
42 | 42 | is_datetime64_any_dtype, |
43 | 43 | is_datetime64tz_dtype, |
44 | | - is_dict_like, |
45 | 44 | is_bool_dtype, |
46 | 45 | is_integer_dtype, |
47 | 46 | is_float_dtype, |
@@ -2769,6 +2768,46 @@ def reindexer(value): |
2769 | 2768 |
|
2770 | 2769 | return np.atleast_2d(np.asarray(value)) |
2771 | 2770 |
|
| 2771 | + def _validate_axis_style_args(self, arg, arg_name, index, columns, |
| 2772 | + axis, method_name): |
| 2773 | + if axis is not None: |
| 2774 | + # Using "axis" style, along with a positional arg |
| 2775 | + # Both index and columns should be None then |
| 2776 | + axis = self._get_axis_name(axis) |
| 2777 | + if index is not None or columns is not None: |
| 2778 | + msg = ( |
| 2779 | + "Can't specify both 'axis' and 'index' or 'columns'. " |
| 2780 | + "Specify either\n" |
| 2781 | + "\t.{method_name}.rename({arg_name}, axis=axis), or\n" |
| 2782 | + "\t.{method_name}.rename(index=index, columns=columns)" |
| 2783 | + ).format(arg_name=arg_name, method_name=method_name) |
| 2784 | + raise TypeError(msg) |
| 2785 | + if axis == 'index': |
| 2786 | + index = arg |
| 2787 | + elif axis == 'columns': |
| 2788 | + columns = arg |
| 2789 | + |
| 2790 | + elif all(x is not None for x in (arg, index, columns)): |
| 2791 | + msg = ( |
| 2792 | + "Cannot specify all of '{arg_name}', 'index', and 'columns'. " |
| 2793 | + "Specify either {arg_name} and 'axis', or 'index' and " |
| 2794 | + "'columns'." |
| 2795 | + ).format(arg_name=arg_name) |
| 2796 | + raise TypeError(msg) |
| 2797 | + |
| 2798 | + elif axis is None and (arg is not None and index is not None): |
| 2799 | + # This is the "ambiguous" case, so emit a warning |
| 2800 | + msg = ( |
| 2801 | + "Interpreting call to '.{method_name}(a, b)' as " |
| 2802 | + "'.{method_name}(index=a, columns=b)'. " |
| 2803 | + "Use keyword arguments to remove any ambiguity." |
| 2804 | + ).format(method_name=method_name) |
| 2805 | + warnings.warn(msg) |
| 2806 | + index, columns = arg, index |
| 2807 | + elif index is None and columns is None: |
| 2808 | + index = arg |
| 2809 | + return index, columns |
| 2810 | + |
2772 | 2811 | @property |
2773 | 2812 | def _series(self): |
2774 | 2813 | result = {} |
@@ -2910,34 +2949,9 @@ def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True, |
2910 | 2949 | @Appender(_shared_docs['rename'] % _shared_doc_kwargs) |
2911 | 2950 | def rename(self, mapper=None, index=None, columns=None, axis=None, |
2912 | 2951 | **kwargs): |
2913 | | - if axis is not None: |
2914 | | - # Using "axis" style, along with a positional mapper |
2915 | | - # Both index and columns should be None then |
2916 | | - axis = self._get_axis_name(axis) |
2917 | | - if index is not None or columns is not None: |
2918 | | - raise TypeError("Can't specify both 'axis' and 'index' or " |
2919 | | - "'columns' Specify either\n" |
2920 | | - "\t.rename(mapper, axis=axis), or\n" |
2921 | | - "\t.rename(index=index, columns=columns)") |
2922 | | - if axis == 'index': |
2923 | | - index = mapper |
2924 | | - elif axis == 'columns': |
2925 | | - columns = mapper |
2926 | | - elif all(x is not None for x in (mapper, index, columns)): |
2927 | | - raise TypeError("Cannot specify all of 'mapper', 'index', and " |
2928 | | - "'columns'. Specify 'mapper' and 'axis', or " |
2929 | | - "'index' and 'columns'.") |
2930 | | - elif axis is None and (mapper is not None and index is not None): |
2931 | | - # Determine if they meant df.rename(idx_map, col_map) |
2932 | | - if callable(index) or is_dict_like(index): |
2933 | | - warnings.warn("Interpreting call to '.rename(a, b)' as " |
2934 | | - "'.rename(index=a, columns=b)'. " |
2935 | | - "Use keyword arguments to remove ambiguity.", |
2936 | | - UserWarning) |
2937 | | - index, columns = mapper, index |
2938 | | - elif index is None and columns is None: |
2939 | | - index = mapper |
2940 | | - |
| 2952 | + index, columns = self._validate_axis_style_args(mapper, 'mapper', |
| 2953 | + index, columns, |
| 2954 | + axis, 'rename') |
2941 | 2955 | return super(DataFrame, self).rename(index=index, columns=columns, |
2942 | 2956 | **kwargs) |
2943 | 2957 |
|
|
0 commit comments