1
1
# Arithmetic tests for DataFrame/Series/Index/Array classes that should
2
2
# behave identically.
3
- from datetime import datetime , timedelta
3
+ from datetime import datetime , timedelta , timezone
4
4
5
5
import numpy as np
6
6
import pytest
@@ -337,6 +337,8 @@ def test_subtraction_ops_with_tz(self):
337
337
ts_tz2 = Timestamp ("20130101" ).tz_localize ("CET" )
338
338
dt_tz = ts_tz .to_pydatetime ()
339
339
td = Timedelta ("1 days" )
340
+ ts_utc = Timestamp ("2020-10-22T22:00:00+00:00" )
341
+ dt_utc = datetime (2020 , 10 , 22 , 22 , tzinfo = timezone .utc )
340
342
341
343
def _check (result , expected ):
342
344
assert result == expected
@@ -355,36 +357,51 @@ def _check(result, expected):
355
357
expected = Timedelta ("0 days" )
356
358
_check (result , expected )
357
359
360
+ result = ts_utc - dt_utc
361
+ expected = Timedelta ("0 days" )
362
+ _check (result , expected )
363
+
364
+ result = dt_tz - ts_tz2
365
+ expected = Timedelta ("0 days 06:00:00" )
366
+ _check (result , expected )
367
+
368
+ result = ts_tz2 - dt_tz
369
+ expected = Timedelta ("-1 days +18:00:00" )
370
+ _check (result , expected )
371
+
372
+ # gotta chedk this
358
373
# tz mismatches
359
- msg = "Timestamp subtraction must have the same timezones or no timezones "
374
+ msg = "Cannot subtract offset-naive and offset-aware datetimes. "
360
375
with pytest .raises (TypeError , match = msg ):
361
376
dt_tz - ts
362
377
msg = "can't subtract offset-naive and offset-aware datetimes"
363
378
with pytest .raises (TypeError , match = msg ):
364
379
dt_tz - dt
365
- msg = "Timestamp subtraction must have the same timezones or no timezones"
366
- with pytest .raises (TypeError , match = msg ):
367
- dt_tz - ts_tz2
368
380
msg = "can't subtract offset-naive and offset-aware datetimes"
369
381
with pytest .raises (TypeError , match = msg ):
370
382
dt - dt_tz
371
- msg = "Timestamp subtraction must have the same timezones or no timezones "
383
+ msg = "Cannot subtract offset-naive and offset-aware datetimes. "
372
384
with pytest .raises (TypeError , match = msg ):
373
385
ts - dt_tz
374
386
with pytest .raises (TypeError , match = msg ):
375
387
ts_tz2 - ts
376
388
with pytest .raises (TypeError , match = msg ):
377
389
ts_tz2 - dt
378
- with pytest .raises (TypeError , match = msg ):
379
- ts_tz - ts_tz2
380
390
381
391
# with dti
382
392
with pytest .raises (TypeError , match = msg ):
383
393
dti - ts_tz
384
394
with pytest .raises (TypeError , match = msg ):
385
395
dti_tz - ts
386
- with pytest .raises (TypeError , match = msg ):
387
- dti_tz - ts_tz2
396
+
397
+ result = dti_tz - ts_tz2
398
+ expected = TimedeltaIndex (["0 days 06:00:00" , "1 days 06:00:00" , "2 days 06:00:00" ])
399
+ tm .assert_index_equal (result , expected )
400
+
401
+
402
+ result = ts_tz2 - dti_tz
403
+ expected = TimedeltaIndex (["-1 days +18:00:00" , "-2 days +18:00:00" , "-3 days +18:00:00" ])
404
+ tm .assert_index_equal (result , expected )
388
405
389
406
result = dti_tz - dt_tz
390
407
expected = TimedeltaIndex (["0 days" , "1 days" , "2 days" ])
0 commit comments