|
41 | 41 | is_datetimetz,
|
42 | 42 | is_datetime64_any_dtype,
|
43 | 43 | is_datetime64tz_dtype,
|
| 44 | + is_dict_like, |
44 | 45 | is_bool_dtype,
|
45 | 46 | is_integer_dtype,
|
46 | 47 | is_float_dtype,
|
@@ -2904,27 +2905,30 @@ def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True,
|
2904 | 2905 | limit=limit, fill_value=fill_value)
|
2905 | 2906 |
|
2906 | 2907 | @Appender(_shared_docs['rename'] % _shared_doc_kwargs)
|
2907 |
| - def rename(self, *args, index=None, columns=None, **kwargs): |
2908 |
| - nargs = len(args) |
2909 |
| - if 'axis' in kwargs and (index is not None or columns is not None): |
2910 |
| - raise TypeError("Cannot specify 'index' or 'columns' and 'axis' at" |
2911 |
| - " the same time. Specify either\n" |
2912 |
| - "\t.rename(mapping, axis=axis), or\n" |
2913 |
| - "\t.rename(index=index, columns=columns)") |
2914 |
| - if 'axis' in kwargs and nargs > 1: |
2915 |
| - raise TypeError("Cannot specify 'index', 'axis', and 'columns' at " |
2916 |
| - "the same time.") |
2917 |
| - if nargs > 2: |
2918 |
| - raise TypeError("Too many positional arguments") |
2919 |
| - |
2920 |
| - if nargs and index is not None: |
2921 |
| - raise TypeError("rename() got multiple arguments for argumnet " |
2922 |
| - "'index'") |
2923 |
| - |
2924 |
| - if index is None and nargs: |
2925 |
| - index = args[0] |
2926 |
| - if columns is None and nargs > 1: |
2927 |
| - columns = args[1] |
| 2908 | + def rename(self, mapper=None, index=None, columns=None, axis=None, |
| 2909 | + **kwargs): |
| 2910 | + if axis is not None: |
| 2911 | + axis = self._get_axis_name(axis) |
| 2912 | + # interpreting 'mapper' as a positional argument |
| 2913 | + if index is not None or columns is not None: |
| 2914 | + raise TypeError("Can't specify 'index' or 'columns' and 'axis'" |
| 2915 | + " at the same time. Specify either\n" |
| 2916 | + "\t.rename(mapper, axis=axis), or\n" |
| 2917 | + "\t.rename(index=index, columns=columns)") |
| 2918 | + if axis == 'index': |
| 2919 | + index = mapper |
| 2920 | + elif axis == 'columns': |
| 2921 | + columns = mapper |
| 2922 | + elif all(x is not None for x in (mapper, index, columns)): |
| 2923 | + raise TypeError("Cannot specify all of 'mapper', 'index', and " |
| 2924 | + "'columns'. Specify 'mapper' and 'axis', or " |
| 2925 | + "'index' and 'columns'.") |
| 2926 | + elif axis is None and (mapper is not None and index is not None): |
| 2927 | + # Determine if they meant df.rename(idx_map, col_map) |
| 2928 | + if callable(index) or is_dict_like(index): |
| 2929 | + warnings.warn("Interpreting renaming index and columns. " |
| 2930 | + "Use keyword arguments.", UserWarning) |
| 2931 | + index, columns = mapper, index |
2928 | 2932 |
|
2929 | 2933 | return super(DataFrame, self).rename(index=index, columns=columns,
|
2930 | 2934 | **kwargs)
|
|
0 commit comments