Repository: pandas-dev/pandas
Pull request: Replace with nested dict raises for overlapping keys
- [x] closes #27660 
- [ ] tests added / passed
- [ ] passes `black pandas`
- [ ] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry


Problem statement:
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>

Hints from issue discussion:
Thanks for the report. AFAICT this is only an issue when replacing a bool dtype with a value of 0 or 1. Investigation and PRs would certainly be welcome
PR is coming

Commit message: tst

Patch to review:
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
index 821c35e0cce2f..9913a44006ea7 100644
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -6640,7 +6640,11 @@ def replace(
 
                 for k, v in items:
                     keys, values = list(zip(*v.items())) or ([], [])
-                    if set(keys) & set(values):
+                    # add another check to avoid boolean being regarded
+                    # as binary in python set
+                    if set(keys) & set(values) and set(map(str, keys)) & set(
+                        map(str, values)
+                    ):
                         raise ValueError(
                             "Replacement not allowed with "
                             "overlapping keys and values"
diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py
index 7b9e50ebbf342..a5da14724180e 100644
--- a/pandas/tests/generic/test_generic.py
+++ b/pandas/tests/generic/test_generic.py
@@ -948,3 +948,18 @@ def test_deprecated_get_dtype_counts(self):
         df = DataFrame([1])
         with tm.assert_produces_warning(FutureWarning):
             df.get_dtype_counts()
+
+    def test_boolean_in_replace(self):
+        # GH 27660
+        df = DataFrame({"col": [False, True, 0, 1]})
+
+#        result = df.replace({"col": {False: 0, True: 1}})
+#        expected = DataFrame({"col": [0, 1, 0, 1]})
+#        assert_frame_equal(result, expected)
+
+#        msg = "Replacement not allowed with overlapping keys and values"
+#        with pytest.raises(ValueError, match=msg):
+#            df.replace({"col": {0: 1, 1: "a"}})
+
+        dd = df.replace({False:1, True:0})
+        assert dd['col'].values == 0
\ No newline at end of file


Code review comments:
diff_hunk: @@ -948,3 +948,18 @@ def test_deprecated_get_dtype_counts(self):
         df = DataFrame([1])
         with tm.assert_produces_warning(FutureWarning):
             df.get_dtype_counts()
+
+    def test_boolean_in_replace(self):
+        # GH 27660
+        df = DataFrame({"col": [False, True, 0, 1]})
+
+#        result = df.replace({"col": {False: 0, True: 1}})
path: pandas/tests/generic/test_generic.py
text: @user1:
Typically better to delete than to comment out

@author:
wooow... I am so sorry... i didn't intend to push, I am still working on it.. 

when i was doing `git checkout`, it says the branch is one commit ahead, so have to do `git push`... I am really sorry, I will ping when the PR is ready for review. @user1