|
| 1 | +import dateutil.tz |
| 2 | +from dateutil.tz import tzlocal |
| 3 | +import pytest |
| 4 | +import pytz |
| 5 | + |
| 6 | +from pandas._libs.tslibs.ccalendar import MONTHS |
| 7 | +from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG |
| 8 | + |
| 9 | +from pandas import ( |
| 10 | + DatetimeIndex, |
| 11 | + Period, |
| 12 | + PeriodIndex, |
| 13 | + Timestamp, |
| 14 | + date_range, |
| 15 | + period_range, |
| 16 | +) |
| 17 | +import pandas._testing as tm |
| 18 | + |
| 19 | + |
| 20 | +class TestToPeriod: |
| 21 | + def test_dti_to_period(self): |
| 22 | + dti = date_range(start="1/1/2005", end="12/1/2005", freq="M") |
| 23 | + pi1 = dti.to_period() |
| 24 | + pi2 = dti.to_period(freq="D") |
| 25 | + pi3 = dti.to_period(freq="3D") |
| 26 | + |
| 27 | + assert pi1[0] == Period("Jan 2005", freq="M") |
| 28 | + assert pi2[0] == Period("1/31/2005", freq="D") |
| 29 | + assert pi3[0] == Period("1/31/2005", freq="3D") |
| 30 | + |
| 31 | + assert pi1[-1] == Period("Nov 2005", freq="M") |
| 32 | + assert pi2[-1] == Period("11/30/2005", freq="D") |
| 33 | + assert pi3[-1], Period("11/30/2005", freq="3D") |
| 34 | + |
| 35 | + tm.assert_index_equal(pi1, period_range("1/1/2005", "11/1/2005", freq="M")) |
| 36 | + tm.assert_index_equal( |
| 37 | + pi2, period_range("1/1/2005", "11/1/2005", freq="M").asfreq("D") |
| 38 | + ) |
| 39 | + tm.assert_index_equal( |
| 40 | + pi3, period_range("1/1/2005", "11/1/2005", freq="M").asfreq("3D") |
| 41 | + ) |
| 42 | + |
| 43 | + @pytest.mark.parametrize("month", MONTHS) |
| 44 | + def test_to_period_quarterly(self, month): |
| 45 | + # make sure we can make the round trip |
| 46 | + freq = "Q-{month}".format(month=month) |
| 47 | + rng = period_range("1989Q3", "1991Q3", freq=freq) |
| 48 | + stamps = rng.to_timestamp() |
| 49 | + result = stamps.to_period(freq) |
| 50 | + tm.assert_index_equal(rng, result) |
| 51 | + |
| 52 | + @pytest.mark.parametrize("off", ["BQ", "QS", "BQS"]) |
| 53 | + def test_to_period_quarterlyish(self, off): |
| 54 | + rng = date_range("01-Jan-2012", periods=8, freq=off) |
| 55 | + prng = rng.to_period() |
| 56 | + assert prng.freq == "Q-DEC" |
| 57 | + |
| 58 | + @pytest.mark.parametrize("off", ["BA", "AS", "BAS"]) |
| 59 | + def test_to_period_annualish(self, off): |
| 60 | + rng = date_range("01-Jan-2012", periods=8, freq=off) |
| 61 | + prng = rng.to_period() |
| 62 | + assert prng.freq == "A-DEC" |
| 63 | + |
| 64 | + def test_to_period_monthish(self): |
| 65 | + offsets = ["MS", "BM"] |
| 66 | + for off in offsets: |
| 67 | + rng = date_range("01-Jan-2012", periods=8, freq=off) |
| 68 | + prng = rng.to_period() |
| 69 | + assert prng.freq == "M" |
| 70 | + |
| 71 | + rng = date_range("01-Jan-2012", periods=8, freq="M") |
| 72 | + prng = rng.to_period() |
| 73 | + assert prng.freq == "M" |
| 74 | + |
| 75 | + with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG): |
| 76 | + date_range("01-Jan-2012", periods=8, freq="EOM") |
| 77 | + |
| 78 | + def test_period_dt64_round_trip(self): |
| 79 | + dti = date_range("1/1/2000", "1/7/2002", freq="B") |
| 80 | + pi = dti.to_period() |
| 81 | + tm.assert_index_equal(pi.to_timestamp(), dti) |
| 82 | + |
| 83 | + dti = date_range("1/1/2000", "1/7/2002", freq="B") |
| 84 | + pi = dti.to_period(freq="H") |
| 85 | + tm.assert_index_equal(pi.to_timestamp(), dti) |
| 86 | + |
| 87 | + def test_to_period_millisecond(self): |
| 88 | + index = DatetimeIndex( |
| 89 | + [ |
| 90 | + Timestamp("2007-01-01 10:11:12.123456Z"), |
| 91 | + Timestamp("2007-01-01 10:11:13.789123Z"), |
| 92 | + ] |
| 93 | + ) |
| 94 | + |
| 95 | + with tm.assert_produces_warning(UserWarning): |
| 96 | + # warning that timezone info will be lost |
| 97 | + period = index.to_period(freq="L") |
| 98 | + assert 2 == len(period) |
| 99 | + assert period[0] == Period("2007-01-01 10:11:12.123Z", "L") |
| 100 | + assert period[1] == Period("2007-01-01 10:11:13.789Z", "L") |
| 101 | + |
| 102 | + def test_to_period_microsecond(self): |
| 103 | + index = DatetimeIndex( |
| 104 | + [ |
| 105 | + Timestamp("2007-01-01 10:11:12.123456Z"), |
| 106 | + Timestamp("2007-01-01 10:11:13.789123Z"), |
| 107 | + ] |
| 108 | + ) |
| 109 | + |
| 110 | + with tm.assert_produces_warning(UserWarning): |
| 111 | + # warning that timezone info will be lost |
| 112 | + period = index.to_period(freq="U") |
| 113 | + assert 2 == len(period) |
| 114 | + assert period[0] == Period("2007-01-01 10:11:12.123456Z", "U") |
| 115 | + assert period[1] == Period("2007-01-01 10:11:13.789123Z", "U") |
| 116 | + |
| 117 | + @pytest.mark.parametrize( |
| 118 | + "tz", |
| 119 | + ["US/Eastern", pytz.utc, tzlocal(), "dateutil/US/Eastern", dateutil.tz.tzutc()], |
| 120 | + ) |
| 121 | + def test_to_period_tz(self, tz): |
| 122 | + ts = date_range("1/1/2000", "2/1/2000", tz=tz) |
| 123 | + |
| 124 | + with tm.assert_produces_warning(UserWarning): |
| 125 | + # GH#21333 warning that timezone info will be lost |
| 126 | + result = ts.to_period()[0] |
| 127 | + expected = ts[0].to_period() |
| 128 | + |
| 129 | + assert result == expected |
| 130 | + |
| 131 | + expected = date_range("1/1/2000", "2/1/2000").to_period() |
| 132 | + |
| 133 | + with tm.assert_produces_warning(UserWarning): |
| 134 | + # GH#21333 warning that timezone info will be lost |
| 135 | + result = ts.to_period() |
| 136 | + |
| 137 | + tm.assert_index_equal(result, expected) |
| 138 | + |
| 139 | + @pytest.mark.parametrize("tz", ["Etc/GMT-1", "Etc/GMT+1"]) |
| 140 | + def test_to_period_tz_utc_offset_consistency(self, tz): |
| 141 | + # GH#22905 |
| 142 | + ts = date_range("1/1/2000", "2/1/2000", tz="Etc/GMT-1") |
| 143 | + with tm.assert_produces_warning(UserWarning): |
| 144 | + result = ts.to_period()[0] |
| 145 | + expected = ts[0].to_period() |
| 146 | + assert result == expected |
| 147 | + |
| 148 | + def test_to_period_nofreq(self): |
| 149 | + idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-04"]) |
| 150 | + with pytest.raises(ValueError): |
| 151 | + idx.to_period() |
| 152 | + |
| 153 | + idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"], freq="infer") |
| 154 | + assert idx.freqstr == "D" |
| 155 | + expected = PeriodIndex(["2000-01-01", "2000-01-02", "2000-01-03"], freq="D") |
| 156 | + tm.assert_index_equal(idx.to_period(), expected) |
| 157 | + |
| 158 | + # GH#7606 |
| 159 | + idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"]) |
| 160 | + assert idx.freqstr is None |
| 161 | + tm.assert_index_equal(idx.to_period(), expected) |
0 commit comments