6
6
from pandas .compat import IS64
7
7
8
8
from pandas import (
9
- DateOffset ,
10
9
DatetimeIndex ,
11
10
Index ,
12
- Series ,
13
11
bdate_range ,
14
12
date_range ,
15
13
)
16
14
import pandas ._testing as tm
17
15
18
- from pandas .tseries .offsets import (
19
- BDay ,
20
- Day ,
21
- Hour ,
22
- )
23
-
24
16
START , END = datetime (2009 , 1 , 1 ), datetime (2010 , 1 , 1 )
25
17
26
18
27
19
class TestDatetimeIndexOps :
28
- def test_ops_properties_basic (self , datetime_series ):
29
-
30
- # sanity check that the behavior didn't change
31
- # GH#7206
32
- for op in ["year" , "day" , "second" , "weekday" ]:
33
- msg = f"'Series' object has no attribute '{ op } '"
34
- with pytest .raises (AttributeError , match = msg ):
35
- getattr (datetime_series , op )
36
-
37
- # attribute access should still work!
38
- s = Series ({"year" : 2000 , "month" : 1 , "day" : 10 })
39
- assert s .year == 2000
40
- assert s .month == 1
41
- assert s .day == 10
42
- msg = "'Series' object has no attribute 'weekday'"
43
- with pytest .raises (AttributeError , match = msg ):
44
- s .weekday
45
-
46
20
@pytest .mark .parametrize (
47
21
"freq,expected" ,
48
22
[
@@ -74,72 +48,28 @@ def test_infer_freq(self, freq_sample):
74
48
tm .assert_index_equal (idx , result )
75
49
assert result .freq == freq_sample
76
50
77
- @pytest .mark .parametrize ("values" , [["20180101" , "20180103" , "20180105" ], []])
78
- @pytest .mark .parametrize ("freq" , ["2D" , Day (2 ), "2B" , BDay (2 ), "48H" , Hour (48 )])
79
- @pytest .mark .parametrize ("tz" , [None , "US/Eastern" ])
80
- def test_freq_setter (self , values , freq , tz ):
81
- # GH 20678
82
- idx = DatetimeIndex (values , tz = tz )
83
-
84
- # can set to an offset, converting from string if necessary
85
- idx ._data .freq = freq
86
- assert idx .freq == freq
87
- assert isinstance (idx .freq , DateOffset )
88
-
89
- # can reset to None
90
- idx ._data .freq = None
91
- assert idx .freq is None
92
-
93
- def test_freq_setter_errors (self ):
94
- # GH 20678
95
- idx = DatetimeIndex (["20180101" , "20180103" , "20180105" ])
96
-
97
- # setting with an incompatible freq
98
- msg = (
99
- "Inferred frequency 2D from passed values does not conform to "
100
- "passed frequency 5D"
101
- )
102
- with pytest .raises (ValueError , match = msg ):
103
- idx ._data .freq = "5D"
104
-
105
- # setting with non-freq string
106
- with pytest .raises (ValueError , match = "Invalid frequency" ):
107
- idx ._data .freq = "foo"
108
-
109
- def test_freq_view_safe (self ):
110
- # Setting the freq for one DatetimeIndex shouldn't alter the freq
111
- # for another that views the same data
112
-
113
- dti = date_range ("2016-01-01" , periods = 5 )
114
- dta = dti ._data
115
-
116
- dti2 = DatetimeIndex (dta )._with_freq (None )
117
- assert dti2 .freq is None
118
-
119
- # Original was not altered
120
- assert dti .freq == "D"
121
- assert dta .freq == "D"
122
-
123
51
52
+ @pytest .mark .parametrize ("freq" , ["B" , "C" ])
124
53
class TestBusinessDatetimeIndex :
125
- def setup_method (self , method ):
126
- self .rng = bdate_range (START , END )
54
+ @pytest .fixture
55
+ def rng (self , freq ):
56
+ return bdate_range (START , END , freq = freq )
127
57
128
- def test_comparison (self ):
129
- d = self . rng [10 ]
58
+ def test_comparison (self , rng ):
59
+ d = rng [10 ]
130
60
131
- comp = self . rng > d
61
+ comp = rng > d
132
62
assert comp [11 ]
133
63
assert not comp [9 ]
134
64
135
- def test_copy (self ):
136
- cp = self . rng .copy ()
65
+ def test_copy (self , rng ):
66
+ cp = rng .copy ()
137
67
repr (cp )
138
- tm .assert_index_equal (cp , self . rng )
68
+ tm .assert_index_equal (cp , rng )
139
69
140
- def test_identical (self ):
141
- t1 = self . rng .copy ()
142
- t2 = self . rng .copy ()
70
+ def test_identical (self , rng ):
71
+ t1 = rng .copy ()
72
+ t2 = rng .copy ()
143
73
assert t1 .identical (t2 )
144
74
145
75
# name
@@ -153,20 +83,3 @@ def test_identical(self):
153
83
t2v = Index (t2 .values )
154
84
assert t1 .equals (t2v )
155
85
assert not t1 .identical (t2v )
156
-
157
-
158
- class TestCustomDatetimeIndex :
159
- def setup_method (self , method ):
160
- self .rng = bdate_range (START , END , freq = "C" )
161
-
162
- def test_comparison (self ):
163
- d = self .rng [10 ]
164
-
165
- comp = self .rng > d
166
- assert comp [11 ]
167
- assert not comp [9 ]
168
-
169
- def test_copy (self ):
170
- cp = self .rng .copy ()
171
- repr (cp )
172
- tm .assert_index_equal (cp , self .rng )
0 commit comments