6363
6464
6565def 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 ,
7797 )
7898
7999
@@ -82,18 +102,18 @@ def test_types_arithmetic() -> None:
82102 ts2 : pd .Timestamp = pd .to_datetime ("2021-01-01" )
83103 delta : pd .Timedelta = pd .to_timedelta ("1 day" )
84104
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 )
89109
90110
91111def test_types_comparison () -> None :
92112 ts : pd .Timestamp = pd .to_datetime ("2021-03-01" )
93113 ts2 : pd .Timestamp = pd .to_datetime ("2021-01-01" )
94114
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 )
97117
98118
99119def test_types_timestamp_series_comparisons () -> None :
@@ -121,32 +141,37 @@ def test_types_timestamp_series_comparisons() -> None:
121141def test_types_pydatetime () -> None :
122142 ts : pd .Timestamp = pd .Timestamp ("2021-03-01T12" )
123143
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 )
127147
128148
129149def 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+ )
132155
133156
134157def test_timedelta_arithmetic () -> None :
135158 td1 : pd .Timedelta = pd .to_timedelta (3 , "days" )
136159 td2 : pd .Timedelta = pd .to_timedelta (4 , "hours" )
137160 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 )
141164
142165
143166def 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 )
150175
151176
152177def test_timedelta_float_value () -> None :
@@ -190,11 +215,18 @@ def test_timestamp_timedelta_series_arithmetic() -> None:
190215def test_timestamp_dateoffset_arithmetic () -> None :
191216 ts = pd .Timestamp ("2022-03-18" )
192217 do = pd .DateOffset (days = 366 )
193- r1 : pd . Timestamp = ts + do
218+ check ( assert_type ( ts + do , pd . Timestamp ), pd . Timestamp )
194219
195220
196221def 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+ )
198230 dti = pd .to_datetime (["2022-03-08" , "2022-03-15" ])
199231 td_s = pd .to_timedelta (pd .Series ([10 , 20 ]), "minutes" )
200232 dti_td_s = dti + td_s
@@ -222,7 +254,14 @@ def test_datetimeindex_plus_timedelta() -> None:
222254
223255def test_datetimeindex_minus_timedelta () -> None :
224256 # 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+ )
226265 dti = pd .to_datetime (["2022-03-08" , "2022-03-15" ])
227266 td_s = pd .to_timedelta (pd .Series ([10 , 20 ]), "minutes" )
228267 dti_td_s = dti - td_s
@@ -241,7 +280,14 @@ def test_datetimeindex_minus_timedelta() -> None:
241280
242281
243282def 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+ )
245291 ts = pd .Timestamp ("2022-03-05" )
246292 td = pd .to_timedelta (pd .Series ([10 , 20 ]), "minutes" )
247293 r3 = td + ts
@@ -265,10 +311,10 @@ def test_timedelta_series_sum() -> None:
265311 pd .to_datetime (["04/05/2022 08:00" , "04/03/2022 09:00" ])
266312 )
267313 ssum = s .sum ()
268- ires : int = ssum .days
314+ check ( assert_type ( ssum .days , int ), int )
269315
270316 sf = pd .Series ([1.0 , 2.2 , 3.3 ])
271- sfsum : float = sf .sum ()
317+ check ( assert_type ( sf .sum (), float ), float )
272318
273319
274320def test_iso_calendar () -> None :
0 commit comments