|
3 | 3 |
|
4 | 4 | from pandas import (
|
5 | 5 | DataFrame,
|
| 6 | + DatetimeIndex, |
| 7 | + Index, |
| 8 | + Period, |
| 9 | + PeriodIndex, |
6 | 10 | Series,
|
| 11 | + Timedelta, |
| 12 | + TimedeltaIndex, |
| 13 | + Timestamp, |
7 | 14 | )
|
8 | 15 | import pandas._testing as tm
|
9 | 16 | from pandas.tests.copy_view.util import get_array
|
@@ -82,6 +89,35 @@ def test_series_from_series_with_reindex(using_copy_on_write):
|
82 | 89 | assert not result._mgr.blocks[0].refs.has_reference()
|
83 | 90 |
|
84 | 91 |
|
| 92 | +@pytest.mark.parametrize( |
| 93 | + "idx", |
| 94 | + [ |
| 95 | + Index([1, 2]), |
| 96 | + DatetimeIndex([Timestamp("2019-12-31"), Timestamp("2020-12-31")]), |
| 97 | + PeriodIndex([Period("2019-12-31"), Period("2020-12-31")]), |
| 98 | + TimedeltaIndex([Timedelta("1 days"), Timedelta("2 days")]), |
| 99 | + ], |
| 100 | +) |
| 101 | +def test_series_from_index(using_copy_on_write, idx): |
| 102 | + ser = Series(idx) |
| 103 | + expected = idx.copy(deep=True) |
| 104 | + if using_copy_on_write: |
| 105 | + assert np.shares_memory(get_array(ser), get_array(idx)) |
| 106 | + assert not ser._mgr._has_no_reference(0) |
| 107 | + else: |
| 108 | + assert not np.shares_memory(get_array(ser), get_array(idx)) |
| 109 | + ser.iloc[0] = ser.iloc[1] |
| 110 | + tm.assert_index_equal(idx, expected) |
| 111 | + |
| 112 | + |
| 113 | +def test_series_from_index_different_dtypes(using_copy_on_write): |
| 114 | + idx = Index([1, 2, 3], dtype="int64") |
| 115 | + ser = Series(idx, dtype="int32") |
| 116 | + assert not np.shares_memory(get_array(ser), get_array(idx)) |
| 117 | + if using_copy_on_write: |
| 118 | + assert ser._mgr._has_no_reference(0) |
| 119 | + |
| 120 | + |
85 | 121 | @pytest.mark.parametrize("func", [lambda x: x, lambda x: x._mgr])
|
86 | 122 | @pytest.mark.parametrize("columns", [None, ["a"]])
|
87 | 123 | def test_dataframe_constructor_mgr_or_df(using_copy_on_write, columns, func):
|
|
0 commit comments