|
5 | 5 |
|
6 | 6 | import pandas as pd
|
7 | 7 | import pandas._testing as tm
|
| 8 | +from pandas.core.arrays import IntervalArray |
8 | 9 |
|
9 | 10 |
|
10 | 11 | class TestSeriesReplace:
|
@@ -259,7 +260,7 @@ def test_replace2(self):
|
259 | 260 | def test_replace_with_dictlike_and_string_dtype(self):
|
260 | 261 | # GH 32621
|
261 | 262 | s = pd.Series(["one", "two", np.nan], dtype="string")
|
262 |
| - expected = pd.Series(["1", "2", np.nan]) |
| 263 | + expected = pd.Series(["1", "2", np.nan], dtype="string") |
263 | 264 | result = s.replace({"one": "1", "two": "2"})
|
264 | 265 | tm.assert_series_equal(expected, result)
|
265 | 266 |
|
@@ -460,3 +461,39 @@ def test_str_replace_regex_default_raises_warning(self, pattern):
|
460 | 461 | with tm.assert_produces_warning(FutureWarning, check_stacklevel=False) as w:
|
461 | 462 | s.str.replace(pattern, "")
|
462 | 463 | assert re.match(msg, str(w[0].message))
|
| 464 | + |
| 465 | + @pytest.mark.parametrize( |
| 466 | + "dtype, input_data, to_replace, expected_data", |
| 467 | + [ |
| 468 | + ("bool", [True, False], {True: False}, [False, False]), |
| 469 | + ("int64", [1, 2], {1: 10, 2: 20}, [10, 20]), |
| 470 | + ("Int64", [1, 2], {1: 10, 2: 20}, [10, 20]), |
| 471 | + ("float64", [1.1, 2.2], {1.1: 10.1, 2.2: 20.5}, [10.1, 20.5]), |
| 472 | + ("Float64", [1.1, 2.2], {1.1: 10.1, 2.2: 20.5}, [10.1, 20.5]), |
| 473 | + ("string", ["one", "two"], {"one": "1", "two": "2"}, ["1", "2"]), |
| 474 | + ( |
| 475 | + pd.IntervalDtype("int64"), |
| 476 | + IntervalArray([pd.Interval(1, 2), pd.Interval(2, 3)]), |
| 477 | + {pd.Interval(1, 2): pd.Interval(10, 20)}, |
| 478 | + IntervalArray([pd.Interval(10, 20), pd.Interval(2, 3)]), |
| 479 | + ), |
| 480 | + ( |
| 481 | + pd.IntervalDtype("float64"), |
| 482 | + IntervalArray([pd.Interval(1.0, 2.7), pd.Interval(2.8, 3.1)]), |
| 483 | + {pd.Interval(1.0, 2.7): pd.Interval(10.6, 20.8)}, |
| 484 | + IntervalArray([pd.Interval(10.6, 20.8), pd.Interval(2.8, 3.1)]), |
| 485 | + ), |
| 486 | + ( |
| 487 | + pd.PeriodDtype("M"), |
| 488 | + [pd.Period("2020-05", freq="M")], |
| 489 | + {pd.Period("2020-05", freq="M"): pd.Period("2020-06", freq="M")}, |
| 490 | + [pd.Period("2020-06", freq="M")], |
| 491 | + ), |
| 492 | + ], |
| 493 | + ) |
| 494 | + def test_replace_dtype(self, dtype, input_data, to_replace, expected_data): |
| 495 | + # GH 33484 |
| 496 | + s = pd.Series(input_data, dtype=dtype) |
| 497 | + result = s.replace(to_replace) |
| 498 | + expected = pd.Series(expected_data, dtype=dtype) |
| 499 | + tm.assert_series_equal(result, expected) |
0 commit comments