|
3 | 3 |
|
4 | 4 | from pandas import (
|
5 | 5 | DataFrame,
|
| 6 | + Interval, |
6 | 7 | NaT,
|
7 | 8 | Series,
|
8 | 9 | Timestamp,
|
| 10 | + interval_range, |
9 | 11 | )
|
10 | 12 | import pandas._testing as tm
|
11 | 13 | from pandas.tests.copy_view.util import get_array
|
@@ -162,3 +164,71 @@ def test_interpolate_downcast_reference_triggers_copy(using_copy_on_write):
|
162 | 164 | tm.assert_frame_equal(df_orig, view)
|
163 | 165 | else:
|
164 | 166 | tm.assert_frame_equal(df, view)
|
| 167 | + |
| 168 | + |
| 169 | +def test_fillna(using_copy_on_write): |
| 170 | + df = DataFrame({"a": [1.5, np.nan], "b": 1}) |
| 171 | + df_orig = df.copy() |
| 172 | + |
| 173 | + df2 = df.fillna(5.5) |
| 174 | + if using_copy_on_write: |
| 175 | + assert np.shares_memory(get_array(df, "b"), get_array(df2, "b")) |
| 176 | + else: |
| 177 | + assert not np.shares_memory(get_array(df, "b"), get_array(df2, "b")) |
| 178 | + |
| 179 | + df2.iloc[0, 1] = 100 |
| 180 | + tm.assert_frame_equal(df_orig, df) |
| 181 | + |
| 182 | + |
| 183 | +@pytest.mark.parametrize("downcast", [None, False]) |
| 184 | +def test_fillna_inplace(using_copy_on_write, downcast): |
| 185 | + df = DataFrame({"a": [1.5, np.nan], "b": 1}) |
| 186 | + arr_a = get_array(df, "a") |
| 187 | + arr_b = get_array(df, "b") |
| 188 | + |
| 189 | + df.fillna(5.5, inplace=True, downcast=downcast) |
| 190 | + assert np.shares_memory(get_array(df, "a"), arr_a) |
| 191 | + assert np.shares_memory(get_array(df, "b"), arr_b) |
| 192 | + if using_copy_on_write: |
| 193 | + assert df._mgr._has_no_reference(0) |
| 194 | + assert df._mgr._has_no_reference(1) |
| 195 | + |
| 196 | + |
| 197 | +def test_fillna_inplace_reference(using_copy_on_write): |
| 198 | + df = DataFrame({"a": [1.5, np.nan], "b": 1}) |
| 199 | + df_orig = df.copy() |
| 200 | + arr_a = get_array(df, "a") |
| 201 | + arr_b = get_array(df, "b") |
| 202 | + view = df[:] |
| 203 | + |
| 204 | + df.fillna(5.5, inplace=True) |
| 205 | + if using_copy_on_write: |
| 206 | + assert not np.shares_memory(get_array(df, "a"), arr_a) |
| 207 | + assert np.shares_memory(get_array(df, "b"), arr_b) |
| 208 | + assert view._mgr._has_no_reference(0) |
| 209 | + assert df._mgr._has_no_reference(0) |
| 210 | + tm.assert_frame_equal(view, df_orig) |
| 211 | + else: |
| 212 | + assert np.shares_memory(get_array(df, "a"), arr_a) |
| 213 | + assert np.shares_memory(get_array(df, "b"), arr_b) |
| 214 | + expected = DataFrame({"a": [1.5, 5.5], "b": 1}) |
| 215 | + tm.assert_frame_equal(df, expected) |
| 216 | + |
| 217 | + |
| 218 | +def test_fillna_interval_inplace_reference(using_copy_on_write): |
| 219 | + ser = Series(interval_range(start=0, end=5), name="a") |
| 220 | + ser.iloc[1] = np.nan |
| 221 | + |
| 222 | + ser_orig = ser.copy() |
| 223 | + view = ser[:] |
| 224 | + ser.fillna(value=Interval(left=0, right=5), inplace=True) |
| 225 | + |
| 226 | + if using_copy_on_write: |
| 227 | + assert not np.shares_memory( |
| 228 | + get_array(ser, "a").left.values, get_array(view, "a").left.values |
| 229 | + ) |
| 230 | + tm.assert_series_equal(view, ser_orig) |
| 231 | + else: |
| 232 | + assert np.shares_memory( |
| 233 | + get_array(ser, "a").left.values, get_array(view, "a").left.values |
| 234 | + ) |
0 commit comments