Repository: pandas-dev/pandas

Issue:
Pandas replace does not work with booleans
#### Code Sample, a copy-pastable example if possible

```python
import pandas as pd

df = pd.DataFrame({"col":[False, True]})
df.replace({"col": {False:0, True:1}})

```
#### Problem description

The previous code triggers the following error:
ValueError: Replacement not allowed with overlapping keys and values

Though, there is no overlapping and the replacement should be done properly.

#### Expected Output
     col
0  0
1  1

#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.4.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
byteorder: little
LC_ALL: None
LANG: fr
LOCALE: None.None

pandas: 0.22.0
pytest: 3.3.2
pip: 19.0.2
setuptools: 39.0.1
Cython: 0.27.3
numpy: 1.14.2
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.6.6
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: 2.2.2
openpyxl: 2.4.10
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.1
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

</details>



Discussion:

Resolving patch:
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -6669,11 +6669,7 @@ def replace(
 
                 for k, v in items:
                     keys, values = list(zip(*v.items())) or ([], [])
-                    if set(keys) & set(values):
-                        raise ValueError(
-                            "Replacement not allowed with "
-                            "overlapping keys and values"
-                        )
+
                     to_rep_dict[k] = list(keys)
                     value_dict[k] = list(values)
 


Test patch:
diff --git a/pandas/tests/frame/test_replace.py b/pandas/tests/frame/test_replace.py
--- a/pandas/tests/frame/test_replace.py
+++ b/pandas/tests/frame/test_replace.py
@@ -1069,18 +1069,24 @@ def test_replace_truthy(self):
         e = df
         assert_frame_equal(r, e)
 
-    def test_replace_int_to_int_chain(self):
+    def test_nested_dict_overlapping_keys_replace_int(self):
+        # GH 27660 keep behaviour consistent for simple dictionary and
+        # nested dictionary replacement
         df = DataFrame({"a": list(range(1, 5))})
-        with pytest.raises(ValueError, match="Replacement not allowed .+"):
-            df.replace({"a": dict(zip(range(1, 5), range(2, 6)))})
 
-    def test_replace_str_to_str_chain(self):
+        result = df.replace({"a": dict(zip(range(1, 5), range(2, 6)))})
+        expected = df.replace(dict(zip(range(1, 5), range(2, 6))))
+        assert_frame_equal(result, expected)
+
+    def test_nested_dict_overlapping_keys_replace_str(self):
+        # GH 27660
         a = np.arange(1, 5)
         astr = a.astype(str)
         bstr = np.arange(2, 6).astype(str)
         df = DataFrame({"a": astr})
-        with pytest.raises(ValueError, match="Replacement not allowed .+"):
-            df.replace({"a": dict(zip(astr, bstr))})
+        result = df.replace(dict(zip(astr, bstr)))
+        expected = df.replace({"a": dict(zip(astr, bstr))})
+        assert_frame_equal(result, expected)
 
     def test_replace_swapping_bug(self):
         df = pd.DataFrame({"a": [True, False, True]})