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
@@ -360,6 +360,8 @@ def test_subtraction_ops_with_tz(self):
360
360
ts_tz2 = Timestamp ("20130101" ).tz_localize ("CET" )
361
361
dt_tz = ts_tz .to_pydatetime ()
362
362
td = Timedelta ("1 days" )
363
+ ts_utc = Timestamp ("2020-10-22T22:00:00+00:00" )
364
+ dt_utc = datetime (2020 , 10 , 22 , 22 , tzinfo = timezone .utc )
363
365
364
366
def _check (result , expected ):
365
367
assert result == expected
@@ -378,36 +380,54 @@ def _check(result, expected):
378
380
expected = Timedelta ("0 days" )
379
381
_check (result , expected )
380
382
383
+ result = ts_utc - dt_utc
384
+ expected = Timedelta ("0 days" )
385
+ _check (result , expected )
386
+
387
+ result = dt_tz - ts_tz2
388
+ expected = Timedelta ("0 days 06:00:00" )
389
+ _check (result , expected )
390
+
391
+ result = ts_tz2 - dt_tz
392
+ expected = Timedelta ("-1 days +18:00:00" )
393
+ _check (result , expected )
394
+
395
+ # gotta chedk this
381
396
# tz mismatches
382
- msg = "Timestamp subtraction must have the same timezones or no timezones "
397
+ msg = "Cannot subtract offset-naive and offset-aware datetimes. "
383
398
with pytest .raises (TypeError , match = msg ):
384
399
dt_tz - ts
385
400
msg = "can't subtract offset-naive and offset-aware datetimes"
386
401
with pytest .raises (TypeError , match = msg ):
387
402
dt_tz - dt
388
- msg = "Timestamp subtraction must have the same timezones or no timezones"
389
- with pytest .raises (TypeError , match = msg ):
390
- dt_tz - ts_tz2
391
403
msg = "can't subtract offset-naive and offset-aware datetimes"
392
404
with pytest .raises (TypeError , match = msg ):
393
405
dt - dt_tz
394
- msg = "Timestamp subtraction must have the same timezones or no timezones "
406
+ msg = "Cannot subtract offset-naive and offset-aware datetimes. "
395
407
with pytest .raises (TypeError , match = msg ):
396
408
ts - dt_tz
397
409
with pytest .raises (TypeError , match = msg ):
398
410
ts_tz2 - ts
399
411
with pytest .raises (TypeError , match = msg ):
400
412
ts_tz2 - dt
401
- with pytest .raises (TypeError , match = msg ):
402
- ts_tz - ts_tz2
403
413
404
414
# with dti
405
415
with pytest .raises (TypeError , match = msg ):
406
416
dti - ts_tz
407
417
with pytest .raises (TypeError , match = msg ):
408
418
dti_tz - ts
409
- with pytest .raises (TypeError , match = msg ):
410
- dti_tz - ts_tz2
419
+
420
+ result = dti_tz - ts_tz2
421
+ expected = TimedeltaIndex (
422
+ ["0 days 06:00:00" , "1 days 06:00:00" , "2 days 06:00:00" ]
423
+ )
424
+ tm .assert_index_equal (result , expected )
425
+
426
+ result = ts_tz2 - dti_tz
427
+ expected = TimedeltaIndex (
428
+ ["-1 days +18:00:00" , "-2 days +18:00:00" , "-3 days +18:00:00" ]
429
+ )
430
+ tm .assert_index_equal (result , expected )
411
431
412
432
result = dti_tz - dt_tz
413
433
expected = TimedeltaIndex (["0 days" , "1 days" , "2 days" ])
0 commit comments