63
63
64
64
65
65
def test_types_init () -> None :
66
- ts : pd .Timestamp = pd .Timestamp ("2021-03-01T12" )
67
- ts1 : pd .Timestamp = pd .Timestamp (dt .date (2021 , 3 , 15 ))
68
- ts2 : pd .Timestamp = pd .Timestamp (dt .datetime (2021 , 3 , 10 , 12 ))
69
- ts3 : pd .Timestamp = pd .Timestamp (pd .Timestamp ("2021-03-01T12" ))
70
- ts4 : pd .Timestamp = pd .Timestamp (1515590000.1 , unit = "s" )
71
- ts5 : pd .Timestamp = pd .Timestamp (1515590000.1 , unit = "s" , tz = "US/Pacific" )
72
- ts6 : pd .Timestamp = pd .Timestamp (1515590000100000000 ) # plain integer (nanosecond)
73
- ts7 : pd .Timestamp = pd .Timestamp (2021 , 3 , 10 , 12 )
74
- ts8 : pd .Timestamp = pd .Timestamp (year = 2021 , month = 3 , day = 10 , hour = 12 )
75
- ts9 : pd .Timestamp = pd .Timestamp (
76
- year = 2021 , month = 3 , day = 10 , hour = 12 , tz = "US/Pacific"
66
+ check (assert_type (pd .Timestamp ("2021-03-01T12" ), pd .Timestamp ), pd .Timestamp )
67
+ check (assert_type (pd .Timestamp (dt .date (2021 , 3 , 15 )), pd .Timestamp ), pd .Timestamp )
68
+ check (
69
+ assert_type (pd .Timestamp (dt .datetime (2021 , 3 , 10 , 12 )), pd .Timestamp ),
70
+ pd .Timestamp ,
71
+ )
72
+ check (
73
+ assert_type (pd .Timestamp (pd .Timestamp ("2021-03-01T12" )), pd .Timestamp ),
74
+ pd .Timestamp ,
75
+ )
76
+ check (assert_type (pd .Timestamp (1515590000.1 , unit = "s" ), pd .Timestamp ), pd .Timestamp )
77
+ check (
78
+ assert_type (
79
+ pd .Timestamp (1515590000.1 , unit = "s" , tz = "US/Pacific" ), pd .Timestamp
80
+ ),
81
+ pd .Timestamp ,
82
+ )
83
+ check (
84
+ assert_type (pd .Timestamp (1515590000100000000 ), pd .Timestamp ), pd .Timestamp
85
+ ) # plain integer (nanosecond)
86
+ check (assert_type (pd .Timestamp (2021 , 3 , 10 , 12 ), pd .Timestamp ), pd .Timestamp )
87
+ check (
88
+ assert_type (pd .Timestamp (year = 2021 , month = 3 , day = 10 , hour = 12 ), pd .Timestamp ),
89
+ pd .Timestamp ,
90
+ )
91
+ check (
92
+ assert_type (
93
+ pd .Timestamp (year = 2021 , month = 3 , day = 10 , hour = 12 , tz = "US/Pacific" ),
94
+ pd .Timestamp ,
95
+ ),
96
+ pd .Timestamp ,
77
97
)
78
98
79
99
@@ -82,18 +102,18 @@ def test_types_arithmetic() -> None:
82
102
ts2 : pd .Timestamp = pd .to_datetime ("2021-01-01" )
83
103
delta : pd .Timedelta = pd .to_timedelta ("1 day" )
84
104
85
- tsr : pd . Timedelta = ts - ts2
86
- tsr2 : pd . Timestamp = ts + delta
87
- tsr3 : pd . Timestamp = ts - delta
88
- tsr4 : pd . Timedelta = ts - dt .datetime (2021 , 1 , 3 )
105
+ check ( assert_type ( ts - ts2 , pd . Timedelta ), pd . Timedelta )
106
+ check ( assert_type ( ts + delta , pd . Timestamp ), pd . Timestamp )
107
+ check ( assert_type ( ts - delta , pd . Timestamp ), pd . Timestamp )
108
+ check ( assert_type ( ts - dt .datetime (2021 , 1 , 3 ), pd . Timedelta ), pd . Timedelta )
89
109
90
110
91
111
def test_types_comparison () -> None :
92
112
ts : pd .Timestamp = pd .to_datetime ("2021-03-01" )
93
113
ts2 : pd .Timestamp = pd .to_datetime ("2021-01-01" )
94
114
95
- tsr : bool = ts < ts2
96
- tsr2 : bool = ts > ts2
115
+ check ( assert_type ( ts < ts2 , bool ), bool )
116
+ check ( assert_type ( ts > ts2 , bool ), bool )
97
117
98
118
99
119
def test_types_timestamp_series_comparisons () -> None :
@@ -121,32 +141,37 @@ def test_types_timestamp_series_comparisons() -> None:
121
141
def test_types_pydatetime () -> None :
122
142
ts : pd .Timestamp = pd .Timestamp ("2021-03-01T12" )
123
143
124
- datet : dt .datetime = ts . to_pydatetime ( )
125
- datet2 : dt .datetime = ts . to_pydatetime ( False )
126
- datet3 : dt . datetime = ts .to_pydatetime (warn = True )
144
+ check ( assert_type ( ts . to_pydatetime (), dt .datetime ), dt . datetime )
145
+ check ( assert_type ( ts . to_pydatetime ( False ), dt .datetime ), dt . datetime )
146
+ check ( assert_type ( ts .to_pydatetime (warn = True ), dt . datetime ), dt . datetime )
127
147
128
148
129
149
def test_to_timedelta () -> None :
130
- td : pd .Timedelta = pd .to_timedelta (3 , "days" )
131
- tds : pd .TimedeltaIndex = pd .to_timedelta ([2 , 3 ], "minutes" )
150
+ check (assert_type (pd .to_timedelta (3 , "days" ), pd .Timedelta ), pd .Timedelta )
151
+ check (
152
+ assert_type (pd .to_timedelta ([2 , 3 ], "minutes" ), pd .TimedeltaIndex ),
153
+ pd .TimedeltaIndex ,
154
+ )
132
155
133
156
134
157
def test_timedelta_arithmetic () -> None :
135
158
td1 : pd .Timedelta = pd .to_timedelta (3 , "days" )
136
159
td2 : pd .Timedelta = pd .to_timedelta (4 , "hours" )
137
160
td3 : pd .Timedelta = td1 + td2
138
- td4 : pd . Timedelta = td1 - td2
139
- td5 : pd . Timedelta = td1 * 4.3
140
- td6 : pd . Timedelta = td3 / 10.2
161
+ check ( assert_type ( td1 - td2 , pd . Timedelta ), pd . Timedelta )
162
+ check ( assert_type ( td1 * 4.3 , pd . Timedelta ), pd . Timedelta )
163
+ check ( assert_type ( td3 / 10.2 , pd . Timedelta ), pd . Timedelta )
141
164
142
165
143
166
def test_timedelta_series_arithmetic () -> None :
144
- tds1 : pd .TimedeltaIndex = pd .to_timedelta ([2 , 3 ], "minutes" )
145
- td1 : pd .Timedelta = pd .Timedelta ("2 days" )
146
- r1 : pd .TimedeltaIndex = tds1 + td1
147
- r2 : pd .TimedeltaIndex = tds1 - td1
148
- r3 : pd .TimedeltaIndex = tds1 * 4.3
149
- r4 : pd .TimedeltaIndex = tds1 / 10.2
167
+ tds1 = pd .to_timedelta ([2 , 3 ], "minutes" )
168
+ td1 = pd .Timedelta ("2 days" )
169
+ check (assert_type (tds1 , pd .TimedeltaIndex ), pd .TimedeltaIndex )
170
+ check (assert_type (td1 , pd .Timedelta ), pd .Timedelta )
171
+ check (assert_type (tds1 + td1 , pd .TimedeltaIndex ), pd .TimedeltaIndex )
172
+ check (assert_type (tds1 - td1 , pd .TimedeltaIndex ), pd .TimedeltaIndex )
173
+ check (assert_type (tds1 * 4.3 , pd .TimedeltaIndex ), pd .TimedeltaIndex )
174
+ check (assert_type (tds1 / 10.2 , pd .TimedeltaIndex ), pd .TimedeltaIndex )
150
175
151
176
152
177
def test_timedelta_float_value () -> None :
@@ -190,11 +215,18 @@ def test_timestamp_timedelta_series_arithmetic() -> None:
190
215
def test_timestamp_dateoffset_arithmetic () -> None :
191
216
ts = pd .Timestamp ("2022-03-18" )
192
217
do = pd .DateOffset (days = 366 )
193
- r1 : pd . Timestamp = ts + do
218
+ check ( assert_type ( ts + do , pd . Timestamp ), pd . Timestamp )
194
219
195
220
196
221
def test_datetimeindex_plus_timedelta () -> None :
197
- tscheck = pd .Series ([pd .Timestamp ("2022-03-05" ), pd .Timestamp ("2022-03-06" )])
222
+ check (
223
+ assert_type (
224
+ pd .Series ([pd .Timestamp ("2022-03-05" ), pd .Timestamp ("2022-03-06" )]),
225
+ "TimestampSeries" ,
226
+ ),
227
+ pd .Series ,
228
+ pd .Timestamp ,
229
+ )
198
230
dti = pd .to_datetime (["2022-03-08" , "2022-03-15" ])
199
231
td_s = pd .to_timedelta (pd .Series ([10 , 20 ]), "minutes" )
200
232
dti_td_s = dti + td_s
@@ -222,7 +254,14 @@ def test_datetimeindex_plus_timedelta() -> None:
222
254
223
255
def test_datetimeindex_minus_timedelta () -> None :
224
256
# GH 280
225
- tscheck = pd .Series ([pd .Timestamp ("2022-03-05" ), pd .Timestamp ("2022-03-06" )])
257
+ check (
258
+ assert_type (
259
+ pd .Series ([pd .Timestamp ("2022-03-05" ), pd .Timestamp ("2022-03-06" )]),
260
+ "TimestampSeries" ,
261
+ ),
262
+ pd .Series ,
263
+ pd .Timestamp ,
264
+ )
226
265
dti = pd .to_datetime (["2022-03-08" , "2022-03-15" ])
227
266
td_s = pd .to_timedelta (pd .Series ([10 , 20 ]), "minutes" )
228
267
dti_td_s = dti - td_s
@@ -241,7 +280,14 @@ def test_datetimeindex_minus_timedelta() -> None:
241
280
242
281
243
282
def test_timestamp_plus_timedelta_series () -> None :
244
- tscheck = pd .Series ([pd .Timestamp ("2022-03-05" ), pd .Timestamp ("2022-03-06" )])
283
+ check (
284
+ assert_type (
285
+ pd .Series ([pd .Timestamp ("2022-03-05" ), pd .Timestamp ("2022-03-06" )]),
286
+ "TimestampSeries" ,
287
+ ),
288
+ pd .Series ,
289
+ pd .Timestamp ,
290
+ )
245
291
ts = pd .Timestamp ("2022-03-05" )
246
292
td = pd .to_timedelta (pd .Series ([10 , 20 ]), "minutes" )
247
293
r3 = td + ts
@@ -265,10 +311,10 @@ def test_timedelta_series_sum() -> None:
265
311
pd .to_datetime (["04/05/2022 08:00" , "04/03/2022 09:00" ])
266
312
)
267
313
ssum = s .sum ()
268
- ires : int = ssum .days
314
+ check ( assert_type ( ssum .days , int ), int )
269
315
270
316
sf = pd .Series ([1.0 , 2.2 , 3.3 ])
271
- sfsum : float = sf .sum ()
317
+ check ( assert_type ( sf .sum (), float ), float )
272
318
273
319
274
320
def test_iso_calendar () -> None :
0 commit comments