|
2 | 2 | import pytest
|
3 | 3 |
|
4 | 4 | from pandas import (
|
| 5 | + NA, |
5 | 6 | DataFrame,
|
6 | 7 | Interval,
|
7 | 8 | NaT,
|
@@ -271,3 +272,48 @@ def test_fillna_series_empty_arg_inplace(using_copy_on_write):
|
271 | 272 | assert np.shares_memory(get_array(ser), arr)
|
272 | 273 | if using_copy_on_write:
|
273 | 274 | assert ser._mgr._has_no_reference(0)
|
| 275 | + |
| 276 | + |
| 277 | +def test_fillna_ea_noop_shares_memory( |
| 278 | + using_copy_on_write, any_numeric_ea_and_arrow_dtype |
| 279 | +): |
| 280 | + df = DataFrame({"a": [1, NA, 3], "b": 1}, dtype=any_numeric_ea_and_arrow_dtype) |
| 281 | + df_orig = df.copy() |
| 282 | + df2 = df.fillna(100) |
| 283 | + |
| 284 | + assert not np.shares_memory(get_array(df, "a"), get_array(df2, "a")) |
| 285 | + |
| 286 | + if using_copy_on_write: |
| 287 | + assert np.shares_memory(get_array(df, "b"), get_array(df2, "b")) |
| 288 | + assert not df2._mgr._has_no_reference(1) |
| 289 | + else: |
| 290 | + assert not np.shares_memory(get_array(df, "b"), get_array(df2, "b")) |
| 291 | + |
| 292 | + tm.assert_frame_equal(df_orig, df) |
| 293 | + |
| 294 | + df2.iloc[0, 1] = 100 |
| 295 | + if using_copy_on_write: |
| 296 | + assert not np.shares_memory(get_array(df, "b"), get_array(df2, "b")) |
| 297 | + assert df2._mgr._has_no_reference(1) |
| 298 | + assert df._mgr._has_no_reference(1) |
| 299 | + tm.assert_frame_equal(df_orig, df) |
| 300 | + |
| 301 | + |
| 302 | +def test_fillna_inplace_ea_noop_shares_memory( |
| 303 | + using_copy_on_write, any_numeric_ea_and_arrow_dtype |
| 304 | +): |
| 305 | + df = DataFrame({"a": [1, NA, 3], "b": 1}, dtype=any_numeric_ea_and_arrow_dtype) |
| 306 | + df_orig = df.copy() |
| 307 | + view = df[:] |
| 308 | + df.fillna(100, inplace=True) |
| 309 | + |
| 310 | + assert not np.shares_memory(get_array(df, "a"), get_array(view, "a")) |
| 311 | + |
| 312 | + if using_copy_on_write: |
| 313 | + assert np.shares_memory(get_array(df, "b"), get_array(view, "b")) |
| 314 | + assert not df._mgr._has_no_reference(1) |
| 315 | + assert not view._mgr._has_no_reference(1) |
| 316 | + else: |
| 317 | + assert not np.shares_memory(get_array(df, "b"), get_array(view, "b")) |
| 318 | + df.iloc[0, 1] = 100 |
| 319 | + tm.assert_frame_equal(df_orig, view) |
0 commit comments