Repository: pandas-dev/pandas

Issue:
BUG: Series.rename raises error on values accepted by Series constructor.
#### Sample

```python
import pandas as pd

class MyIndexer:
    pass

i1 = MyIndexer()
s = pd.Series([1, 2, 3], name=i1) # allowed
s.rename(i1) # raises error
```

The error stack trace is the following:

```python
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    s.rename(i1) # raises error
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/series.py", line 3736, in rename
    return super(Series, self).rename(index=index, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py", line 1091, in rename
    level=level)
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py", line 171, in rename_axis
    obj.set_axis(axis, _transform_index(self.axes[axis], mapper, level))
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py", line 2004, in _transform_index
    items = [func(x) for x in index]
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py", line 2004, in <listcomp>
    items = [func(x) for x in index]
TypeError: 'MyIndexer' object is not callable
```

#### Description

Series.rename handle anything that isn't a scalar or list-like as a mapping. 

#### Proposed change

Change the following code (from Series.rename):
```python
non_mapping = is_scalar(index) or (is_list_like(index) and not is_dict_like(index))
if non_mapping:
    return self._set_name(index, inplace=kwargs.get("inplace"))
return super().rename(index=index, **kwargs)
```
to
```python
if callable(index) or is_dict_like(index):
    return super().rename(index=index, **kwargs)
else:
    return self._set_name(index, inplace=kwargs.get("inplace"))
````

so anything that isn't a dict or a callable will be treated the same way as a scalar or list-like.

#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.8.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-55-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: pt_BR.UTF-8

pandas: 0.24.2
pytest: 3.6.0
pip: 19.1.1
setuptools: 41.0.0
Cython: 0.26.1
numpy: 1.16.4
scipy: 1.3.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: None
patsy: 0.5.1
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.1.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: 4.2.1
bs4: 4.6.0
html5lib: 0.999999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None


</details>



Discussion:

Resolving patch:
diff --git a/pandas/core/series.py b/pandas/core/series.py
--- a/pandas/core/series.py
+++ b/pandas/core/series.py
@@ -4165,12 +4165,10 @@ def rename(self, index=None, **kwargs):
         """
         kwargs["inplace"] = validate_bool_kwarg(kwargs.get("inplace", False), "inplace")
 
-        non_mapping = is_scalar(index) or (
-            is_list_like(index) and not is_dict_like(index)
-        )
-        if non_mapping:
+        if callable(index) or is_dict_like(index):
+            return super().rename(index=index, **kwargs)
+        else:
             return self._set_name(index, inplace=kwargs.get("inplace"))
-        return super().rename(index=index, **kwargs)
 
     @Substitution(**_shared_doc_kwargs)
     @Appender(generic.NDFrame.reindex.__doc__)


Test patch:
diff --git a/pandas/tests/series/test_alter_axes.py b/pandas/tests/series/test_alter_axes.py
--- a/pandas/tests/series/test_alter_axes.py
+++ b/pandas/tests/series/test_alter_axes.py
@@ -267,6 +267,25 @@ def test_rename_axis_none(self, kwargs):
         expected = Series([1, 2, 3], index=expected_index)
         tm.assert_series_equal(result, expected)
 
+    def test_rename_with_custom_indexer(self):
+        # GH 27814
+        class MyIndexer:
+            pass
+
+        ix = MyIndexer()
+        s = Series([1, 2, 3]).rename(ix)
+        assert s.name is ix
+
+    def test_rename_with_custom_indexer_inplace(self):
+        # GH 27814
+        class MyIndexer:
+            pass
+
+        ix = MyIndexer()
+        s = Series([1, 2, 3])
+        s.rename(ix, inplace=True)
+        assert s.name is ix
+
     def test_set_axis_inplace_axes(self, axis_series):
         # GH14636
         ser = Series(np.arange(4), index=[1, 3, 5, 7], dtype="int64")