|
1 | 1 | from datetime import datetime
|
2 | 2 |
|
3 | 3 | import numpy as np
|
| 4 | +import pytest |
4 | 5 |
|
5 | 6 | from pandas import (
|
6 | 7 | DataFrame,
|
7 | 8 | DatetimeIndex,
|
8 | 9 | Series,
|
9 | 10 | date_range,
|
| 11 | + period_range, |
10 | 12 | to_datetime,
|
11 | 13 | )
|
12 | 14 | import pandas._testing as tm
|
|
15 | 17 |
|
16 | 18 |
|
17 | 19 | class TestAsFreq:
|
18 |
| - def test_asfreq_resample_set_correct_freq(self): |
| 20 | + def test_asfreq2(self, frame_or_series): |
| 21 | + ts = frame_or_series( |
| 22 | + [0.0, 1.0, 2.0], |
| 23 | + index=DatetimeIndex( |
| 24 | + [ |
| 25 | + datetime(2009, 10, 30), |
| 26 | + datetime(2009, 11, 30), |
| 27 | + datetime(2009, 12, 31), |
| 28 | + ], |
| 29 | + freq="BM", |
| 30 | + ), |
| 31 | + ) |
| 32 | + |
| 33 | + daily_ts = ts.asfreq("B") |
| 34 | + monthly_ts = daily_ts.asfreq("BM") |
| 35 | + tm.assert_equal(monthly_ts, ts) |
| 36 | + |
| 37 | + daily_ts = ts.asfreq("B", method="pad") |
| 38 | + monthly_ts = daily_ts.asfreq("BM") |
| 39 | + tm.assert_equal(monthly_ts, ts) |
| 40 | + |
| 41 | + daily_ts = ts.asfreq(offsets.BDay()) |
| 42 | + monthly_ts = daily_ts.asfreq(offsets.BMonthEnd()) |
| 43 | + tm.assert_equal(monthly_ts, ts) |
| 44 | + |
| 45 | + result = ts[:0].asfreq("M") |
| 46 | + assert len(result) == 0 |
| 47 | + assert result is not ts |
| 48 | + |
| 49 | + if frame_or_series is Series: |
| 50 | + daily_ts = ts.asfreq("D", fill_value=-1) |
| 51 | + result = daily_ts.value_counts().sort_index() |
| 52 | + expected = Series([60, 1, 1, 1], index=[-1.0, 2.0, 1.0, 0.0]).sort_index() |
| 53 | + tm.assert_series_equal(result, expected) |
| 54 | + |
| 55 | + def test_asfreq_datetimeindex_empty(self, frame_or_series): |
| 56 | + # GH#14320 |
| 57 | + index = DatetimeIndex(["2016-09-29 11:00"]) |
| 58 | + expected = frame_or_series(index=index, dtype=object).asfreq("H") |
| 59 | + result = frame_or_series([3], index=index.copy()).asfreq("H") |
| 60 | + tm.assert_index_equal(expected.index, result.index) |
| 61 | + |
| 62 | + @pytest.mark.parametrize("tz", ["US/Eastern", "dateutil/US/Eastern"]) |
| 63 | + def test_tz_aware_asfreq_smoke(self, tz, frame_or_series): |
| 64 | + dr = date_range("2011-12-01", "2012-07-20", freq="D", tz=tz) |
| 65 | + |
| 66 | + obj = frame_or_series(np.random.randn(len(dr)), index=dr) |
| 67 | + |
| 68 | + # it works! |
| 69 | + obj.asfreq("T") |
| 70 | + |
| 71 | + def test_asfreq_normalize(self, frame_or_series): |
| 72 | + rng = date_range("1/1/2000 09:30", periods=20) |
| 73 | + norm = date_range("1/1/2000", periods=20) |
| 74 | + |
| 75 | + vals = np.random.randn(20, 3) |
| 76 | + |
| 77 | + obj = DataFrame(vals, index=rng) |
| 78 | + expected = DataFrame(vals, index=norm) |
| 79 | + if frame_or_series is Series: |
| 80 | + obj = obj[0] |
| 81 | + expected = expected[0] |
| 82 | + |
| 83 | + result = obj.asfreq("D", normalize=True) |
| 84 | + tm.assert_equal(result, expected) |
| 85 | + |
| 86 | + def test_asfreq_keep_index_name(self, frame_or_series): |
| 87 | + # GH#9854 |
| 88 | + index_name = "bar" |
| 89 | + index = date_range("20130101", periods=20, name=index_name) |
| 90 | + obj = DataFrame(list(range(20)), columns=["foo"], index=index) |
| 91 | + if frame_or_series is Series: |
| 92 | + obj = obj["foo"] |
| 93 | + |
| 94 | + assert index_name == obj.index.name |
| 95 | + assert index_name == obj.asfreq("10D").index.name |
| 96 | + |
| 97 | + def test_asfreq_ts(self, frame_or_series): |
| 98 | + index = period_range(freq="A", start="1/1/2001", end="12/31/2010") |
| 99 | + obj = DataFrame(np.random.randn(len(index), 3), index=index) |
| 100 | + if frame_or_series is Series: |
| 101 | + obj = obj[0] |
| 102 | + |
| 103 | + result = obj.asfreq("D", how="end") |
| 104 | + exp_index = index.asfreq("D", how="end") |
| 105 | + assert len(result) == len(obj) |
| 106 | + tm.assert_index_equal(result.index, exp_index) |
| 107 | + |
| 108 | + result = obj.asfreq("D", how="start") |
| 109 | + exp_index = index.asfreq("D", how="start") |
| 110 | + assert len(result) == len(obj) |
| 111 | + tm.assert_index_equal(result.index, exp_index) |
| 112 | + |
| 113 | + def test_asfreq_resample_set_correct_freq(self, frame_or_series): |
19 | 114 | # GH#5613
|
20 | 115 | # we test if .asfreq() and .resample() set the correct value for .freq
|
21 |
| - df = DataFrame( |
22 |
| - {"date": ["2012-01-01", "2012-01-02", "2012-01-03"], "col": [1, 2, 3]} |
23 |
| - ) |
24 |
| - df = df.set_index(to_datetime(df.date)) |
| 116 | + dti = to_datetime(["2012-01-01", "2012-01-02", "2012-01-03"]) |
| 117 | + obj = DataFrame({"col": [1, 2, 3]}, index=dti) |
| 118 | + if frame_or_series is Series: |
| 119 | + obj = obj["col"] |
25 | 120 |
|
26 | 121 | # testing the settings before calling .asfreq() and .resample()
|
27 |
| - assert df.index.freq is None |
28 |
| - assert df.index.inferred_freq == "D" |
| 122 | + assert obj.index.freq is None |
| 123 | + assert obj.index.inferred_freq == "D" |
29 | 124 |
|
30 | 125 | # does .asfreq() set .freq correctly?
|
31 |
| - assert df.asfreq("D").index.freq == "D" |
| 126 | + assert obj.asfreq("D").index.freq == "D" |
32 | 127 |
|
33 | 128 | # does .resample() set .freq correctly?
|
34 |
| - assert df.resample("D").asfreq().index.freq == "D" |
| 129 | + assert obj.resample("D").asfreq().index.freq == "D" |
| 130 | + |
| 131 | + def test_asfreq_empty(self, datetime_frame): |
| 132 | + # test does not blow up on length-0 DataFrame |
| 133 | + zero_length = datetime_frame.reindex([]) |
| 134 | + result = zero_length.asfreq("BM") |
| 135 | + assert result is not zero_length |
35 | 136 |
|
36 | 137 | def test_asfreq(self, datetime_frame):
|
37 | 138 | offset_monthly = datetime_frame.asfreq(offsets.BMonthEnd())
|
38 | 139 | rule_monthly = datetime_frame.asfreq("BM")
|
39 | 140 |
|
40 |
| - tm.assert_almost_equal(offset_monthly["A"], rule_monthly["A"]) |
| 141 | + tm.assert_frame_equal(offset_monthly, rule_monthly) |
41 | 142 |
|
42 | 143 | filled = rule_monthly.asfreq("B", method="pad") # noqa
|
43 | 144 | # TODO: actually check that this worked.
|
44 | 145 |
|
45 | 146 | # don't forget!
|
46 | 147 | filled_dep = rule_monthly.asfreq("B", method="pad") # noqa
|
47 | 148 |
|
48 |
| - # test does not blow up on length-0 DataFrame |
49 |
| - zero_length = datetime_frame.reindex([]) |
50 |
| - result = zero_length.asfreq("BM") |
51 |
| - assert result is not zero_length |
52 |
| - |
53 | 149 | def test_asfreq_datetimeindex(self):
|
54 | 150 | df = DataFrame(
|
55 | 151 | {"A": [1, 2, 3]},
|
|
0 commit comments