6
6
from pandas import DataFrame , Series , Timestamp , date_range , to_datetime
7
7
import pandas .util .testing as tm
8
8
9
- from .common import TestData
10
9
10
+ @pytest .fixture
11
+ def date_range_frame ():
12
+ """
13
+ Fixture for DataFrame of ints with date_range index
11
14
12
- class TestFrameAsof (TestData ):
13
- def setup_method (self , method ):
14
- self .N = N = 50
15
- self .rng = date_range ('1/1/1990' , periods = N , freq = '53s' )
16
- self .df = DataFrame ({'A' : np .arange (N ), 'B' : np .arange (N )},
17
- index = self .rng )
15
+ Columns are ['A', 'B'].
16
+ """
17
+ N = 50
18
+ rng = date_range ('1/1/1990' , periods = N , freq = '53s' )
19
+ return DataFrame ({'A' : np .arange (N ), 'B' : np .arange (N )}, index = rng )
18
20
19
- def test_basic (self ):
20
- df = self .df .copy ()
21
+
22
+ class TestFrameAsof ():
23
+
24
+ def test_basic (self , date_range_frame ):
25
+ df = date_range_frame
26
+ N = 50
21
27
df .loc [15 :30 , 'A' ] = np .nan
22
- dates = date_range ('1/1/1990' , periods = self .N * 3 ,
23
- freq = '25s' )
28
+ dates = date_range ('1/1/1990' , periods = N * 3 , freq = '25s' )
24
29
25
30
result = df .asof (dates )
26
31
assert result .notna ().all (1 ).all ()
@@ -35,11 +40,9 @@ def test_basic(self):
35
40
rs = result [mask ]
36
41
assert (rs == 14 ).all (1 ).all ()
37
42
38
- def test_subset (self ):
43
+ def test_subset (self , date_range_frame ):
39
44
N = 10
40
- rng = date_range ('1/1/1990' , periods = N , freq = '53s' )
41
- df = DataFrame ({'A' : np .arange (N ), 'B' : np .arange (N )},
42
- index = rng )
45
+ df = date_range_frame .iloc [:N ].copy ()
43
46
df .loc [4 :8 , 'A' ] = np .nan
44
47
dates = date_range ('1/1/1990' , periods = N * 3 ,
45
48
freq = '25s' )
@@ -54,20 +57,18 @@ def test_subset(self):
54
57
expected = df .asof (dates )
55
58
tm .assert_frame_equal (result , expected )
56
59
57
- # B gives self. df.asof
60
+ # B gives df.asof
58
61
result = df .asof (dates , subset = 'B' )
59
62
expected = df .resample ('25s' , closed = 'right' ).ffill ().reindex (dates )
60
63
expected .iloc [20 :] = 9
61
64
62
65
tm .assert_frame_equal (result , expected )
63
66
64
- def test_missing (self ):
67
+ def test_missing (self , date_range_frame ):
65
68
# GH 15118
66
69
# no match found - `where` value before earliest date in index
67
70
N = 10
68
- rng = date_range ('1/1/1990' , periods = N , freq = '53s' )
69
- df = DataFrame ({'A' : np .arange (N ), 'B' : np .arange (N )},
70
- index = rng )
71
+ df = date_range_frame .iloc [:N ].copy ()
71
72
result = df .asof ('1989-12-31' )
72
73
73
74
expected = Series (index = ['A' , 'B' ], name = Timestamp ('1989-12-31' ))
@@ -78,22 +79,24 @@ def test_missing(self):
78
79
columns = ['A' , 'B' ], dtype = 'float64' )
79
80
tm .assert_frame_equal (result , expected )
80
81
81
- def test_all_nans (self ):
82
+ def test_all_nans (self , date_range_frame ):
82
83
# GH 15713
83
84
# DataFrame is all nans
84
85
result = DataFrame ([np .nan ]).asof ([0 ])
85
86
expected = DataFrame ([np .nan ])
86
87
tm .assert_frame_equal (result , expected )
87
88
88
89
# testing non-default indexes, multiple inputs
89
- dates = date_range ('1/1/1990' , periods = self .N * 3 , freq = '25s' )
90
- result = DataFrame (np .nan , index = self .rng , columns = ['A' ]).asof (dates )
90
+ N = 150
91
+ rng = date_range_frame .index
92
+ dates = date_range ('1/1/1990' , periods = N , freq = '25s' )
93
+ result = DataFrame (np .nan , index = rng , columns = ['A' ]).asof (dates )
91
94
expected = DataFrame (np .nan , index = dates , columns = ['A' ])
92
95
tm .assert_frame_equal (result , expected )
93
96
94
97
# testing multiple columns
95
- dates = date_range ('1/1/1990' , periods = self . N * 3 , freq = '25s' )
96
- result = DataFrame (np .nan , index = self . rng ,
98
+ dates = date_range ('1/1/1990' , periods = N , freq = '25s' )
99
+ result = DataFrame (np .nan , index = rng ,
97
100
columns = ['A' , 'B' , 'C' ]).asof (dates )
98
101
expected = DataFrame (np .nan , index = dates , columns = ['A' , 'B' , 'C' ])
99
102
tm .assert_frame_equal (result , expected )
0 commit comments