11
11
import pandas as pd
12
12
from typing_extensions import assert_type
13
13
14
- from pandas ._libs .tslibs import BaseOffset
14
+ from pandas ._libs .tslibs import (
15
+ BaseOffset ,
16
+ NaTType ,
17
+ )
15
18
16
19
if TYPE_CHECKING :
17
- from pandas .core .series import PeriodSeries # noqa: F401
18
- from pandas .core .series import TimedeltaSeries # noqa: F401
20
+ from pandas .core .series import (
21
+ PeriodSeries ,
22
+ TimedeltaSeries ,
23
+ )
19
24
20
25
from pandas ._typing import np_ndarray_bool
21
26
else :
22
- np_ndarray_bool = Any
27
+ PeriodSeries = TimedeltaSeries = np_ndarray_bool = Any
23
28
24
29
from tests import check
25
30
26
31
from pandas .tseries .offsets import Day
27
32
28
33
29
- def test_period () -> None :
34
+ def test_period_construction () -> None :
30
35
p = pd .Period ("2012-1-1" , freq = "D" )
31
36
check (assert_type (p , pd .Period ), pd .Period )
32
37
check (assert_type (pd .Period (p ), pd .Period ), pd .Period )
@@ -53,6 +58,11 @@ def test_period() -> None:
53
58
pd .Period ,
54
59
)
55
60
check (assert_type (pd .Period (freq = "Q" , year = 2012 , quarter = 2 ), pd .Period ), pd .Period )
61
+
62
+
63
+ def test_period_properties () -> None :
64
+ p = pd .Period ("2012-1-1" , freq = "D" )
65
+
56
66
check (assert_type (p .day , int ), int )
57
67
check (assert_type (p .day_of_week , int ), int )
58
68
check (assert_type (p .day_of_year , int ), int )
@@ -80,15 +90,22 @@ def test_period() -> None:
80
90
p2 = pd .Period ("2012-1-1" , freq = "2D" )
81
91
check (assert_type (p2 .freq , BaseOffset ), Day )
82
92
93
+
94
+ def test_periof_add_subtract () -> None :
95
+ p = pd .Period ("2012-1-1" , freq = "D" )
96
+
83
97
as0 = pd .Timedelta (1 , "D" )
84
98
as1 = dt .timedelta (days = 1 )
85
99
as2 = np .timedelta64 (1 , "D" )
86
100
as3 = np .int64 (1 )
87
101
as4 = int (1 )
88
102
as5 = pd .period_range ("2012-1-1" , periods = 10 , freq = "D" )
89
103
as6 = pd .Period ("2012-1-1" , freq = "D" )
90
- as7 = cast ("TimedeltaSeries" , pd .Series ([pd .Timedelta (days = 1 )]))
91
- as8 = cast ("PeriodSeries" , pd .Series ([as6 ]))
104
+ scale = 24 * 60 * 60 * 10 ** 9
105
+ as7 = cast (TimedeltaSeries , pd .Series (pd .timedelta_range (scale , scale , freq = "D" )))
106
+ as8 = pd .Series (as5 )
107
+ as9 = pd .timedelta_range (scale , scale , freq = "D" )
108
+ as10 = pd .NaT
92
109
93
110
check (assert_type (p + as0 , pd .Period ), pd .Period )
94
111
check (assert_type (p + as1 , pd .Period ), pd .Period )
@@ -97,25 +114,32 @@ def test_period() -> None:
97
114
check (assert_type (p + as4 , pd .Period ), pd .Period )
98
115
check (assert_type (p + p .freq , pd .Period ), pd .Period )
99
116
check (assert_type (p + (p - as5 ), pd .PeriodIndex ), pd .PeriodIndex )
100
- check (assert_type (p + as7 , "PeriodSeries" ), pd .Series )
101
- das8 = cast ("TimedeltaSeries" , (as8 - as8 ))
102
- check (assert_type (p + das8 , "PeriodSeries" ), pd .Series )
117
+ check (assert_type (p + as7 , PeriodSeries ), pd .Series , pd .Period )
118
+ check (assert_type (p + as9 , pd .PeriodIndex ), pd .PeriodIndex )
119
+ check (assert_type (p + as10 , NaTType ), NaTType )
120
+ das8 = cast (TimedeltaSeries , (as8 - as8 ))
121
+ check (assert_type (p + das8 , PeriodSeries ), pd .Series , pd .Period )
103
122
check (assert_type (p - as0 , pd .Period ), pd .Period )
104
123
check (assert_type (p - as1 , pd .Period ), pd .Period )
105
124
check (assert_type (p - as2 , pd .Period ), pd .Period )
106
125
check (assert_type (p - as3 , pd .Period ), pd .Period )
107
126
check (assert_type (p - as4 , pd .Period ), pd .Period )
108
127
check (assert_type (p - as5 , pd .Index ), pd .Index )
109
128
check (assert_type (p - as6 , BaseOffset ), Day )
110
- check (assert_type (p - as7 , "PeriodSeries" ), pd .Series )
129
+ check (assert_type (p - as7 , PeriodSeries ), pd .Series , pd .Period )
130
+ check (assert_type (p - as9 , pd .PeriodIndex ), pd .PeriodIndex )
131
+ check (assert_type (p - as10 , NaTType ), NaTType )
111
132
check (assert_type (p - p .freq , pd .Period ), pd .Period )
112
133
113
134
check (assert_type (as0 + p , pd .Period ), pd .Period )
114
135
check (assert_type (as1 + p , pd .Period ), pd .Period )
115
136
check (assert_type (as2 + p , pd .Period ), pd .Period )
116
137
check (assert_type (as3 + p , pd .Period ), pd .Period )
117
138
check (assert_type (as4 + p , pd .Period ), pd .Period )
118
- check (assert_type (as7 + p , "PeriodSeries" ), pd .Series )
139
+ check (assert_type (as7 + p , PeriodSeries ), pd .Series , pd .Period )
140
+ # TODO: Improve Index to not handle __add__(period)
141
+ check (assert_type (as9 + p , pd .Index ), pd .PeriodIndex )
142
+ check (assert_type (as10 + p , NaTType ), NaTType )
119
143
check (assert_type (p .freq + p , pd .Period ), pd .Period )
120
144
121
145
check (assert_type (as5 - p , pd .Index ), pd .Index )
@@ -125,41 +149,66 @@ def test_period() -> None:
125
149
check (assert_type (p .__radd__ (as2 ), pd .Period ), pd .Period )
126
150
check (assert_type (p .__radd__ (as3 ), pd .Period ), pd .Period )
127
151
check (assert_type (p .__radd__ (as4 ), pd .Period ), pd .Period )
152
+ check (assert_type (p .__radd__ (as10 ), NaTType ), NaTType )
128
153
check (assert_type (p .__radd__ (p .freq ), pd .Period ), pd .Period )
129
154
155
+
156
+ def test_period_cmp () -> None :
157
+ p = pd .Period ("2012-1-1" , freq = "D" )
158
+
130
159
c0 = pd .Period ("2012-1-1" , freq = "D" )
131
160
c1 = pd .period_range ("2012-1-1" , periods = 10 , freq = "D" )
132
161
133
- check (assert_type (p == c0 , bool ), bool )
134
- check (assert_type (p == c1 , np_ndarray_bool ), np .ndarray )
135
- check (assert_type (c0 == p , bool ), bool )
136
- check (assert_type (c1 == p , np_ndarray_bool ), np .ndarray )
162
+ eq = check (assert_type (p == c0 , bool ), bool )
163
+ ne = check (assert_type (p != c0 , bool ), bool )
164
+ assert eq != ne
165
+
166
+ eq_a = check (assert_type (p == c1 , np_ndarray_bool ), np .ndarray )
167
+ ne_q = check (assert_type (p != c1 , np_ndarray_bool ), np .ndarray )
168
+ assert (eq_a != ne_q ).all ()
169
+
170
+ eq = check (assert_type (c0 == p , bool ), bool )
171
+ ne = check (assert_type (c0 != p , bool ), bool )
172
+ assert eq != ne
173
+
174
+ eq_a = check (assert_type (c1 == p , np_ndarray_bool ), np .ndarray )
175
+ ne_a = check (assert_type (c1 != p , np_ndarray_bool ), np .ndarray )
176
+ assert (eq_a != ne_q ).all ()
177
+
178
+ gt = check (assert_type (p > c0 , bool ), bool )
179
+ le = check (assert_type (p <= c0 , bool ), bool )
180
+ assert gt != le
181
+
182
+ gt_a = check (assert_type (p > c1 , np_ndarray_bool ), np .ndarray )
183
+ le_a = check (assert_type (p <= c1 , np_ndarray_bool ), np .ndarray )
184
+ assert (gt_a != le_a ).all ()
185
+
186
+ gt = check (assert_type (c0 > p , bool ), bool )
187
+ le = check (assert_type (c0 <= p , bool ), bool )
188
+ assert gt != le
189
+
190
+ gt_a = check (assert_type (c1 > p , np_ndarray_bool ), np .ndarray )
191
+ le_a = check (assert_type (c1 <= p , np_ndarray_bool ), np .ndarray )
192
+ assert (gt_a != le_a ).all ()
137
193
138
- check (assert_type (p != c0 , bool ), bool )
139
- check (assert_type (p != c1 , np_ndarray_bool ), np .ndarray )
140
- check (assert_type (c0 != p , bool ), bool )
141
- check (assert_type (c1 != p , np_ndarray_bool ), np .ndarray )
194
+ lt = check (assert_type (p < c0 , bool ), bool )
195
+ ge = check (assert_type (p >= c0 , bool ), bool )
196
+ assert lt != ge
142
197
143
- check (assert_type (p > c0 , bool ), bool )
144
- check (assert_type (p > c1 , np_ndarray_bool ), np .ndarray )
145
- check (assert_type (c0 > p , bool ), bool )
146
- check (assert_type (c1 > p , np_ndarray_bool ), np .ndarray )
198
+ lt_a = check (assert_type (p < c1 , np_ndarray_bool ), np .ndarray )
199
+ ge_a = check (assert_type (p >= c1 , np_ndarray_bool ), np .ndarray )
200
+ assert (lt_a != ge_a ).all ()
147
201
148
- check (assert_type (p < c0 , bool ), bool )
149
- check (assert_type (p < c1 , np_ndarray_bool ), np .ndarray )
150
- check (assert_type (c0 < p , bool ), bool )
151
- check (assert_type (c1 < p , np_ndarray_bool ), np .ndarray )
202
+ lt = check (assert_type (c0 < p , bool ), bool )
203
+ ge = check (assert_type (c0 >= p , bool ), bool )
204
+ assert lt != ge
152
205
153
- check (assert_type (p <= c0 , bool ), bool )
154
- check (assert_type (p <= c1 , np_ndarray_bool ), np .ndarray )
155
- check (assert_type (c0 <= p , bool ), bool )
156
- check (assert_type (c1 <= p , np_ndarray_bool ), np .ndarray )
206
+ lt_a = check (assert_type (c1 < p , np_ndarray_bool ), np .ndarray )
207
+ ge_a = check (assert_type (c1 >= p , np_ndarray_bool ), np .ndarray )
208
+ assert (lt_a != ge_a ).all ()
157
209
158
- check (assert_type (p >= c0 , bool ), bool )
159
- check (assert_type (p >= c1 , np_ndarray_bool ), np .ndarray )
160
- check (assert_type (c0 >= p , bool ), bool )
161
- check (assert_type (c1 >= p , np_ndarray_bool ), np .ndarray )
162
210
211
+ def test_period_methods ():
163
212
p3 = pd .Period ("2007-01" , freq = "M" )
164
213
check (assert_type (p3 .to_timestamp ("D" , "S" ), pd .Timestamp ), pd .Timestamp )
165
214
check (assert_type (p3 .to_timestamp ("D" , "E" ), pd .Timestamp ), pd .Timestamp )
@@ -181,5 +230,5 @@ def test_period() -> None:
181
230
check (assert_type (pd .Period .now ("D" ), pd .Period ), pd .Period )
182
231
check (assert_type (pd .Period .now (Day ()), pd .Period ), pd .Period )
183
232
184
- check (assert_type (p .strftime ("%Y-%m-%d" ), str ), str )
185
- check (assert_type (hash (p ), int ), int )
233
+ check (assert_type (p3 .strftime ("%Y-%m-%d" ), str ), str )
234
+ check (assert_type (hash (p3 ), int ), int )
0 commit comments