-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
/
Copy pathtest_first_and_last.py
124 lines (103 loc) · 4.5 KB
/
test_first_and_last.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""
Note: includes tests for `last`
"""
import pytest
import pandas as pd
from pandas import (
DataFrame,
bdate_range,
)
import pandas._testing as tm
deprecated_msg = "first is deprecated"
last_deprecated_msg = "last is deprecated"
class TestFirst:
def test_first_subset(self, frame_or_series):
ts = tm.makeTimeDataFrame(freq="12h")
ts = tm.get_obj(ts, frame_or_series)
with tm.assert_produces_warning(FutureWarning, match=deprecated_msg):
result = ts.first("10d")
assert len(result) == 20
ts = tm.makeTimeDataFrame(freq="D")
ts = tm.get_obj(ts, frame_or_series)
with tm.assert_produces_warning(FutureWarning, match=deprecated_msg):
result = ts.first("10d")
assert len(result) == 10
with tm.assert_produces_warning(FutureWarning, match=deprecated_msg):
result = ts.first("3M")
expected = ts[:"3/31/2000"]
tm.assert_equal(result, expected)
with tm.assert_produces_warning(FutureWarning, match=deprecated_msg):
result = ts.first("21D")
expected = ts[:21]
tm.assert_equal(result, expected)
with tm.assert_produces_warning(FutureWarning, match=deprecated_msg):
result = ts[:0].first("3M")
tm.assert_equal(result, ts[:0])
def test_first_last_raises(self, frame_or_series):
# GH#20725
obj = DataFrame([[1, 2, 3], [4, 5, 6]])
obj = tm.get_obj(obj, frame_or_series)
msg = "'first' only supports a DatetimeIndex index"
with tm.assert_produces_warning(
FutureWarning, match=deprecated_msg
), pytest.raises(
TypeError, match=msg
): # index is not a DatetimeIndex
obj.first("1D")
msg = "'last' only supports a DatetimeIndex index"
with tm.assert_produces_warning(
FutureWarning, match=last_deprecated_msg
), pytest.raises(
TypeError, match=msg
): # index is not a DatetimeIndex
obj.last("1D")
def test_last_subset(self, frame_or_series):
ts = tm.makeTimeDataFrame(freq="12h")
ts = tm.get_obj(ts, frame_or_series)
with tm.assert_produces_warning(FutureWarning, match=last_deprecated_msg):
result = ts.last("10d")
assert len(result) == 20
ts = tm.makeTimeDataFrame(nper=30, freq="D")
ts = tm.get_obj(ts, frame_or_series)
with tm.assert_produces_warning(FutureWarning, match=last_deprecated_msg):
result = ts.last("10d")
assert len(result) == 10
with tm.assert_produces_warning(FutureWarning, match=last_deprecated_msg):
result = ts.last("21D")
expected = ts["2000-01-10":]
tm.assert_equal(result, expected)
with tm.assert_produces_warning(FutureWarning, match=last_deprecated_msg):
result = ts.last("21D")
expected = ts[-21:]
tm.assert_equal(result, expected)
with tm.assert_produces_warning(FutureWarning, match=last_deprecated_msg):
result = ts[:0].last("3M")
tm.assert_equal(result, ts[:0])
@pytest.mark.parametrize("start, periods", [("2010-03-31", 1), ("2010-03-30", 2)])
def test_first_with_first_day_last_of_month(self, frame_or_series, start, periods):
# GH#29623
x = frame_or_series([1] * 100, index=bdate_range(start, periods=100))
with tm.assert_produces_warning(FutureWarning, match=deprecated_msg):
result = x.first("1M")
expected = frame_or_series(
[1] * periods, index=bdate_range(start, periods=periods)
)
tm.assert_equal(result, expected)
def test_first_with_first_day_end_of_frq_n_greater_one(self, frame_or_series):
# GH#29623
x = frame_or_series([1] * 100, index=bdate_range("2010-03-31", periods=100))
with tm.assert_produces_warning(FutureWarning, match=deprecated_msg):
result = x.first("2M")
expected = frame_or_series(
[1] * 23, index=bdate_range("2010-03-31", "2010-04-30")
)
tm.assert_equal(result, expected)
def test_empty_not_input(self):
# GH#51032
df = DataFrame(index=pd.DatetimeIndex([]))
with tm.assert_produces_warning(FutureWarning, match=last_deprecated_msg):
result = df.last(offset=1)
with tm.assert_produces_warning(FutureWarning, match=deprecated_msg):
result = df.first(offset=1)
tm.assert_frame_equal(df, result)
assert df is not result