|
4 | 4 | from pandas._libs.tslibs import iNaT |
5 | 5 | from pandas._libs.tslibs.offsets import MonthEnd |
6 | 6 | from pandas._libs.tslibs.period import IncompatibleFrequency |
| 7 | +from pandas.errors import Pandas4Warning |
7 | 8 |
|
8 | 9 | import pandas as pd |
9 | 10 | import pandas._testing as tm |
|
18 | 19 | [ |
19 | 20 | ([pd.Period("2017", "D")], None, [17167]), |
20 | 21 | ([pd.Period("2017", "D")], "D", [17167]), |
21 | | - ([2017], "D", [17167]), |
22 | 22 | (["2017"], "D", [17167]), |
23 | 23 | ([pd.Period("2017", "D")], pd.tseries.offsets.Day(), [17167]), |
24 | 24 | ([pd.Period("2017", "D"), None], None, [17167, iNaT]), |
@@ -111,25 +111,34 @@ def test_period_array_freq_mismatch(): |
111 | 111 | PeriodArray(arr, dtype=dtype) |
112 | 112 |
|
113 | 113 |
|
114 | | -def test_from_sequence_allows_i8(): |
115 | | - # GH#64227 this used to be allowed for PeriodIndex and period_array |
116 | | - # but not PeriodArray._from_sequence |
| 114 | +def test_from_sequence_int_deprecation(): |
| 115 | + # GH#64227 passing integer data is deprecated; integers will be treated |
| 116 | + # as period ordinals in a future version instead of year values. |
117 | 117 | arr = period_array(["1975", "1976"], dtype="period[D]") |
118 | 118 |
|
119 | | - expected = pd.PeriodIndex([pd.Period(x, freq=arr.freq) for x in arr.asi8]).array |
120 | | - |
121 | | - result1 = pd.PeriodIndex(arr.asi8, dtype=arr.dtype).array |
122 | | - result2 = period_array(arr.asi8, dtype=arr.dtype) |
123 | | - result3 = PeriodArray._from_sequence(arr.asi8, dtype=arr.dtype) |
124 | | - # FIXME: GH#64227 this goes through a different path and gives different behavior |
125 | | - # result4 = PeriodArray._from_sequence(arr.asi8.astype(object), dtype=arr.dtype) |
126 | | - result5 = PeriodArray._from_sequence(list(arr.asi8), dtype=arr.dtype) |
| 119 | + # During deprecation, integer inputs are still treated as year values |
| 120 | + expected_years = pd.PeriodIndex( |
| 121 | + [pd.Period(x, freq=arr.freq) for x in arr.asi8] |
| 122 | + ).array |
127 | 123 |
|
128 | | - tm.assert_period_array_equal(result1, expected) |
129 | | - tm.assert_period_array_equal(result2, expected) |
130 | | - tm.assert_period_array_equal(result3, expected) |
131 | | - # tm.assert_period_array_equal(result4, expected) |
132 | | - tm.assert_period_array_equal(result5, expected) |
| 124 | + msg = "Passing integer-dtype data to PeriodArray/PeriodIndex is deprecated" |
| 125 | + with tm.assert_produces_warning(Pandas4Warning, match=msg): |
| 126 | + result1 = pd.PeriodIndex(arr.asi8, dtype=arr.dtype).array |
| 127 | + with tm.assert_produces_warning(Pandas4Warning, match=msg): |
| 128 | + result2 = period_array(arr.asi8, dtype=arr.dtype) |
| 129 | + with tm.assert_produces_warning(Pandas4Warning, match=msg): |
| 130 | + result3 = PeriodArray._from_sequence(arr.asi8, dtype=arr.dtype) |
| 131 | + with tm.assert_produces_warning(Pandas4Warning, match=msg): |
| 132 | + result5 = PeriodArray._from_sequence(list(arr.asi8), dtype=arr.dtype) |
| 133 | + |
| 134 | + tm.assert_period_array_equal(result1, expected_years) |
| 135 | + tm.assert_period_array_equal(result2, expected_years) |
| 136 | + tm.assert_period_array_equal(result3, expected_years) |
| 137 | + tm.assert_period_array_equal(result5, expected_years) |
| 138 | + |
| 139 | + # Object array path treats integers as ordinals (target behavior, no warning) |
| 140 | + result4 = PeriodArray._from_sequence(arr.asi8.astype(object), dtype=arr.dtype) |
| 141 | + tm.assert_period_array_equal(result4, arr) |
133 | 142 |
|
134 | 143 |
|
135 | 144 | def test_from_td64nat_sequence_raises(): |
|
0 commit comments