|
6 | 6 | import numpy as np
|
7 | 7 | import pytest
|
8 | 8 |
|
| 9 | +import pandas.util._test_decorators as td |
| 10 | + |
9 | 11 | import pandas as pd
|
10 | 12 | from pandas import (
|
11 | 13 | DataFrame,
|
@@ -1430,6 +1432,49 @@ def test_replace_with_nil_na(self):
|
1430 | 1432 | result = ser.replace("nil", "anything else")
|
1431 | 1433 | tm.assert_frame_equal(expected, result)
|
1432 | 1434 |
|
| 1435 | + @pytest.mark.parametrize( |
| 1436 | + "dtype", |
| 1437 | + [ |
| 1438 | + "Float64", |
| 1439 | + pytest.param("float64[pyarrow]", marks=td.skip_if_no("pyarrow")), |
| 1440 | + ], |
| 1441 | + ) |
| 1442 | + def test_replace_na_to_nan_nullable_floats(self, dtype, using_nan_is_na): |
| 1443 | + # GH#55127 |
| 1444 | + df = DataFrame({0: [1, np.nan, 1], 1: Series([0, pd.NA, 1], dtype=dtype)}) |
| 1445 | + |
| 1446 | + result = df.replace(pd.NA, np.nan) |
| 1447 | + |
| 1448 | + if using_nan_is_na: |
| 1449 | + expected = result |
| 1450 | + else: |
| 1451 | + expected = DataFrame( |
| 1452 | + {0: [1, np.nan, 1], 1: Series([0, np.nan, 1], dtype=dtype)} |
| 1453 | + ) |
| 1454 | + assert np.isnan(expected.loc[1, 1]) |
| 1455 | + |
| 1456 | + tm.assert_frame_equal(result, expected) |
| 1457 | + |
| 1458 | + @pytest.mark.parametrize( |
| 1459 | + "dtype", |
| 1460 | + [ |
| 1461 | + "Int64", |
| 1462 | + pytest.param("int64[pyarrow]", marks=td.skip_if_no("pyarrow")), |
| 1463 | + ], |
| 1464 | + ) |
| 1465 | + def test_replace_nan_nullable_ints(self, dtype, using_nan_is_na): |
| 1466 | + # GH#51237 with nan_is_na=False, replacing NaN should be a no-op here |
| 1467 | + ser = Series([1, 2, None], dtype=dtype) |
| 1468 | + |
| 1469 | + result = ser.replace(np.nan, -1) |
| 1470 | + |
| 1471 | + if using_nan_is_na: |
| 1472 | + # np.nan is equivalent to pd.NA here |
| 1473 | + expected = Series([1, 2, -1], dtype=dtype) |
| 1474 | + else: |
| 1475 | + expected = ser |
| 1476 | + tm.assert_series_equal(result, expected) |
| 1477 | + |
1433 | 1478 |
|
1434 | 1479 | class TestDataFrameReplaceRegex:
|
1435 | 1480 | @pytest.mark.parametrize(
|
|
0 commit comments