Skip to content

Commit 10c982d

Browse files
author
Marco Gorelli
committed
✅ test with other dtypes
1 parent 7adbc4d commit 10c982d

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

doc/source/whatsnew/v1.1.0.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ Numeric
125125
Conversion
126126
^^^^^^^^^^
127127
- Bug in :class:`Series` construction from NumPy array with big-endian ``datetime64`` dtype (:issue:`29684`)
128-
- Bug in :meth:`DataFrame.replace` was changing other columns' dtypes when values in one column were being replaced with ``NaN`` (:issue:`30512`)
128+
- Bug in :meth:`DataFrame.replace` was casting all columns of the same dtype to the target value's dtype, even for columns which didn't contain
129+
the value to be replaced (:issue:`30512`)
129130
-
130131

131132
Strings

pandas/tests/frame/methods/test_replace.py

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,9 +1357,82 @@ def test_replace_replacer_dtype(self, replacer):
13571357
expected = pd.DataFrame([replacer])
13581358
tm.assert_frame_equal(result, expected)
13591359

1360-
def test_replace_with_nan(self):
1360+
def test_replace_real_dont_change_other_columns(self, any_real_dtype):
13611361
# GH30512
1362-
df = pd.DataFrame({"A": [np.nan, 3], "B": [1.096, 1.086], "C": [1, 2]})
1363-
result = df.replace(to_replace={np.nan: None}).dtypes.drop("A")
1362+
dtype = any_real_dtype
1363+
value_a = 2
1364+
value_b = 3
1365+
df = pd.DataFrame({"A": [value_a], "B": [value_b]}, dtype=dtype)
1366+
result = df.replace(to_replace={value_a: None}).dtypes.drop("A")
1367+
expected = df.dtypes.drop("A")
1368+
tm.assert_series_equal(result, expected)
1369+
1370+
def test_replace_string_dont_change_other_columns(self, string_dtype):
1371+
# GH30512
1372+
dtype = string_dtype
1373+
value_a = "a"
1374+
value_b = "b"
1375+
df = pd.DataFrame({"A": [value_a], "B": [value_b]}, dtype=dtype)
1376+
result = df.replace(to_replace={value_a: None}).dtypes.drop("A")
1377+
expected = df.dtypes.drop("A")
1378+
tm.assert_series_equal(result, expected)
1379+
1380+
def test_replace_datetime64_dont_change_other_columns(self, datetime64_dtype):
1381+
# GH30512
1382+
dtype = datetime64_dtype
1383+
value_a = pd.Timestamp("2020-01-24")
1384+
value_b = pd.Timestamp("2020-01-25")
1385+
df = pd.DataFrame({"A": [value_a], "B": [value_b]}, dtype=dtype)
1386+
result = df.replace(to_replace={value_a: None}).dtypes.drop("A")
1387+
expected = df.dtypes.drop("A")
1388+
tm.assert_series_equal(result, expected)
1389+
1390+
def test_replace_timedelta64_dont_change_other_columns(self, timedelta64_dtype):
1391+
# GH30512
1392+
dtype = timedelta64_dtype
1393+
value_a = pd.Timedelta(3)
1394+
value_b = pd.Timedelta(4)
1395+
df = pd.DataFrame({"A": [value_a], "B": [value_b]}, dtype=dtype)
1396+
result = df.replace(to_replace={value_a: None}).dtypes.drop("A")
1397+
expected = df.dtypes.drop("A")
1398+
tm.assert_series_equal(result, expected)
1399+
1400+
def test_replace_bool_dont_change_other_columns(self):
1401+
# GH30512
1402+
dtype = bool
1403+
value_a = 1
1404+
value_b = 0
1405+
df = pd.DataFrame({"A": [value_a], "B": [value_b]}, dtype=dtype)
1406+
result = df.replace(to_replace={value_a: None}).dtypes.drop("A")
1407+
expected = df.dtypes.drop("A")
1408+
tm.assert_series_equal(result, expected)
1409+
1410+
def test_replace_complex_dont_change_other_columns(self, complex_dtype):
1411+
# GH30512
1412+
dtype = complex_dtype
1413+
value_a = 1 + 2j
1414+
value_b = 1 + 3j
1415+
df = pd.DataFrame({"A": [value_a], "B": [value_b]}, dtype=dtype)
1416+
result = df.replace(to_replace={value_a: None}).dtypes.drop("A")
1417+
expected = df.dtypes.drop("A")
1418+
tm.assert_series_equal(result, expected)
1419+
1420+
def test_replace_bytes_dont_change_other_columns(self, bytes_dtype):
1421+
# GH30512
1422+
dtype = bytes_dtype
1423+
value_a = b"a"
1424+
value_b = b"b"
1425+
df = pd.DataFrame({"A": [value_a], "B": [value_b]}, dtype=dtype)
1426+
result = df.replace(to_replace={value_a: None}).dtypes.drop("A")
1427+
expected = df.dtypes.drop("A")
1428+
tm.assert_series_equal(result, expected)
1429+
1430+
def test_replace_tz_aware_dont_change_other_columns(self):
1431+
# GH30512
1432+
dtype = "datetime64[ns, US/Eastern]"
1433+
value_a = Timestamp("20130102", tz="US/Eastern")
1434+
value_b = Timestamp("20130103", tz="US/Eastern")
1435+
df = pd.DataFrame({"A": [value_a], "B": [value_b]}, dtype=dtype)
1436+
result = df.replace(to_replace={value_a: None}).dtypes.drop("A")
13641437
expected = df.dtypes.drop("A")
13651438
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)