@@ -347,10 +347,18 @@ def test_period_from_ordinal(self):
347
347
assert p == res
348
348
assert isinstance (res , Period )
349
349
350
- def test_period_cons_nat (self ):
351
- p = Period ("NaT" , freq = "M" )
352
- assert p is NaT
350
+ @pytest .mark .parametrize ("freq" , ["A" , "M" , "D" , "H" ])
351
+ def test_construct_from_nat_string_and_freq (self , freq ):
352
+ per = Period ("NaT" , freq = freq )
353
+ assert per is NaT
354
+
355
+ per = Period ("NaT" , freq = "2" + freq )
356
+ assert per is NaT
353
357
358
+ per = Period ("NaT" , freq = "3" + freq )
359
+ assert per is NaT
360
+
361
+ def test_period_cons_nat (self ):
354
362
p = Period ("nat" , freq = "W-SUN" )
355
363
assert p is NaT
356
364
@@ -930,87 +938,83 @@ def test_get_period_field_array_raises_on_out_of_range(self):
930
938
libperiod .get_period_field_arr (- 1 , np .empty (1 ), 0 )
931
939
932
940
933
- class TestComparisons :
934
- def setup_method (self , method ):
935
- self .january1 = Period ("2000-01" , "M" )
936
- self .january2 = Period ("2000-01" , "M" )
937
- self .february = Period ("2000-02" , "M" )
938
- self .march = Period ("2000-03" , "M" )
939
- self .day = Period ("2012-01-01" , "D" )
940
-
941
- def test_equal (self ):
942
- assert self .january1 == self .january2
943
-
944
- def test_equal_Raises_Value (self ):
945
- with pytest .raises (IncompatibleFrequency ):
946
- self .january1 == self .day
947
-
948
- def test_notEqual (self ):
949
- assert self .january1 != 1
950
- assert self .january1 != self .february
951
-
952
- def test_greater (self ):
953
- assert self .february > self .january1
954
-
955
- def test_greater_Raises_Value (self ):
956
- with pytest .raises (IncompatibleFrequency ):
957
- self .january1 > self .day
958
-
959
- def test_greater_Raises_Type (self ):
960
- with pytest .raises (TypeError ):
961
- self .january1 > 1
962
-
963
- def test_greaterEqual (self ):
964
- assert self .january1 >= self .january2
965
-
966
- def test_greaterEqual_Raises_Value (self ):
967
- with pytest .raises (IncompatibleFrequency ):
968
- self .january1 >= self .day
969
-
970
- with pytest .raises (TypeError ):
971
- print (self .january1 >= 1 )
972
-
973
- def test_smallerEqual (self ):
974
- assert self .january1 <= self .january2
975
-
976
- def test_smallerEqual_Raises_Value (self ):
977
- with pytest .raises (IncompatibleFrequency ):
978
- self .january1 <= self .day
941
+ class TestPeriodComparisons :
942
+ def test_comparison_same_period_different_object (self ):
943
+ # Separate Period objects for the same period
944
+ left = Period ("2000-01" , "M" )
945
+ right = Period ("2000-01" , "M" )
979
946
980
- def test_smallerEqual_Raises_Type (self ):
981
- with pytest .raises (TypeError ):
982
- self .january1 <= 1
947
+ assert left == right
948
+ assert left >= right
949
+ assert left <= right
950
+ assert not left < right
951
+ assert not left > right
983
952
984
- def test_smaller (self ):
985
- assert self .january1 < self .february
953
+ def test_comparison_same_freq (self ):
954
+ jan = Period ("2000-01" , "M" )
955
+ feb = Period ("2000-02" , "M" )
986
956
987
- def test_smaller_Raises_Value (self ):
988
- with pytest .raises (IncompatibleFrequency ):
989
- self .january1 < self .day
957
+ assert not jan == feb
958
+ assert jan != feb
959
+ assert jan < feb
960
+ assert jan <= feb
961
+ assert not jan > feb
962
+ assert not jan >= feb
990
963
991
- def test_smaller_Raises_Type (self ):
992
- with pytest . raises ( TypeError ):
993
- self . january1 < 1
964
+ def test_comparison_mismatched_freq (self ):
965
+ jan = Period ( "2000-01" , "M" )
966
+ day = Period ( "2012-01-01" , "D" )
994
967
995
- def test_sort (self ):
996
- periods = [self .march , self .january1 , self .february ]
997
- correctPeriods = [self .january1 , self .february , self .march ]
968
+ msg = r"Input has different freq=D from Period\(freq=M\)"
969
+ with pytest .raises (IncompatibleFrequency , match = msg ):
970
+ jan == day
971
+ with pytest .raises (IncompatibleFrequency , match = msg ):
972
+ jan != day
973
+ with pytest .raises (IncompatibleFrequency , match = msg ):
974
+ jan < day
975
+ with pytest .raises (IncompatibleFrequency , match = msg ):
976
+ jan <= day
977
+ with pytest .raises (IncompatibleFrequency , match = msg ):
978
+ jan > day
979
+ with pytest .raises (IncompatibleFrequency , match = msg ):
980
+ jan >= day
981
+
982
+ def test_comparison_invalid_type (self ):
983
+ jan = Period ("2000-01" , "M" )
984
+
985
+ assert not jan == 1
986
+ assert jan != 1
987
+
988
+ msg = "Cannot compare type Period with type int"
989
+ for left , right in [(jan , 1 ), (1 , jan )]:
990
+
991
+ with pytest .raises (TypeError , match = msg ):
992
+ left > right
993
+ with pytest .raises (TypeError , match = msg ):
994
+ left >= right
995
+ with pytest .raises (TypeError , match = msg ):
996
+ left < right
997
+ with pytest .raises (TypeError , match = msg ):
998
+ left <= right
999
+
1000
+ def test_sort_periods (self ):
1001
+ jan = Period ("2000-01" , "M" )
1002
+ feb = Period ("2000-02" , "M" )
1003
+ mar = Period ("2000-03" , "M" )
1004
+ periods = [mar , jan , feb ]
1005
+ correctPeriods = [jan , feb , mar ]
998
1006
assert sorted (periods ) == correctPeriods
999
1007
1000
- def test_period_nat_comp (self ):
1001
- p_nat = Period ("NaT" , freq = "D" )
1008
+ def test_period_cmp_nat (self ):
1002
1009
p = Period ("2011-01-01" , freq = "D" )
1003
1010
1004
- nat = Timestamp ("NaT" )
1005
1011
t = Timestamp ("2011-01-01" )
1006
1012
# confirm Period('NaT') work identical with Timestamp('NaT')
1007
1013
for left , right in [
1008
- (p_nat , p ),
1009
- (p , p_nat ),
1010
- (p_nat , p_nat ),
1011
- (nat , t ),
1012
- (t , nat ),
1013
- (nat , nat ),
1014
+ (NaT , p ),
1015
+ (p , NaT ),
1016
+ (NaT , t ),
1017
+ (t , NaT ),
1014
1018
]:
1015
1019
assert not left < right
1016
1020
assert not left > right
@@ -1043,13 +1047,6 @@ def test_add_sub_nat(self):
1043
1047
assert p - NaT is NaT
1044
1048
assert NaT - p is NaT
1045
1049
1046
- p = Period ("NaT" , freq = "M" )
1047
- assert p is NaT
1048
- assert p + NaT is NaT
1049
- assert NaT + p is NaT
1050
- assert p - NaT is NaT
1051
- assert NaT - p is NaT
1052
-
1053
1050
def test_add_invalid (self ):
1054
1051
# GH#4731
1055
1052
per1 = Period (freq = "D" , year = 2008 , month = 1 , day = 1 )
@@ -1281,91 +1278,6 @@ def test_add_offset(self):
1281
1278
with pytest .raises (IncompatibleFrequency ):
1282
1279
o + p
1283
1280
1284
- def test_add_offset_nat (self ):
1285
- # freq is DateOffset
1286
- for freq in ["A" , "2A" , "3A" ]:
1287
- p = Period ("NaT" , freq = freq )
1288
- assert p is NaT
1289
- for o in [offsets .YearEnd (2 )]:
1290
- assert p + o is NaT
1291
- assert o + p is NaT
1292
-
1293
- for o in [
1294
- offsets .YearBegin (2 ),
1295
- offsets .MonthBegin (1 ),
1296
- offsets .Minute (),
1297
- np .timedelta64 (365 , "D" ),
1298
- timedelta (365 ),
1299
- ]:
1300
- assert p + o is NaT
1301
- assert o + p is NaT
1302
-
1303
- for freq in ["M" , "2M" , "3M" ]:
1304
- p = Period ("NaT" , freq = freq )
1305
- assert p is NaT
1306
- for o in [offsets .MonthEnd (2 ), offsets .MonthEnd (12 )]:
1307
- assert p + o is NaT
1308
- assert o + p is NaT
1309
-
1310
- for o in [
1311
- offsets .YearBegin (2 ),
1312
- offsets .MonthBegin (1 ),
1313
- offsets .Minute (),
1314
- np .timedelta64 (365 , "D" ),
1315
- timedelta (365 ),
1316
- ]:
1317
- assert p + o is NaT
1318
- assert o + p is NaT
1319
-
1320
- # freq is Tick
1321
- for freq in ["D" , "2D" , "3D" ]:
1322
- p = Period ("NaT" , freq = freq )
1323
- assert p is NaT
1324
- for o in [
1325
- offsets .Day (5 ),
1326
- offsets .Hour (24 ),
1327
- np .timedelta64 (2 , "D" ),
1328
- np .timedelta64 (3600 * 24 , "s" ),
1329
- timedelta (- 2 ),
1330
- timedelta (hours = 48 ),
1331
- ]:
1332
- assert p + o is NaT
1333
- assert o + p is NaT
1334
-
1335
- for o in [
1336
- offsets .YearBegin (2 ),
1337
- offsets .MonthBegin (1 ),
1338
- offsets .Minute (),
1339
- np .timedelta64 (4 , "h" ),
1340
- timedelta (hours = 23 ),
1341
- ]:
1342
- assert p + o is NaT
1343
- assert o + p is NaT
1344
-
1345
- for freq in ["H" , "2H" , "3H" ]:
1346
- p = Period ("NaT" , freq = freq )
1347
- assert p is NaT
1348
- for o in [
1349
- offsets .Day (2 ),
1350
- offsets .Hour (3 ),
1351
- np .timedelta64 (3 , "h" ),
1352
- np .timedelta64 (3600 , "s" ),
1353
- timedelta (minutes = 120 ),
1354
- timedelta (days = 4 , minutes = 180 ),
1355
- ]:
1356
- assert p + o is NaT
1357
- assert o + p is NaT
1358
-
1359
- for o in [
1360
- offsets .YearBegin (2 ),
1361
- offsets .MonthBegin (1 ),
1362
- offsets .Minute (),
1363
- np .timedelta64 (3200 , "s" ),
1364
- timedelta (hours = 23 , minutes = 30 ),
1365
- ]:
1366
- assert p + o is NaT
1367
- assert o + p is NaT
1368
-
1369
1281
def test_sub_offset (self ):
1370
1282
# freq is DateOffset
1371
1283
for freq in ["A" , "2A" , "3A" ]:
@@ -1440,92 +1352,10 @@ def test_sub_offset(self):
1440
1352
with pytest .raises (IncompatibleFrequency ):
1441
1353
p - o
1442
1354
1443
- def test_sub_offset_nat (self ):
1444
- # freq is DateOffset
1445
- for freq in ["A" , "2A" , "3A" ]:
1446
- p = Period ("NaT" , freq = freq )
1447
- assert p is NaT
1448
- for o in [offsets .YearEnd (2 )]:
1449
- assert p - o is NaT
1450
-
1451
- for o in [
1452
- offsets .YearBegin (2 ),
1453
- offsets .MonthBegin (1 ),
1454
- offsets .Minute (),
1455
- np .timedelta64 (365 , "D" ),
1456
- timedelta (365 ),
1457
- ]:
1458
- assert p - o is NaT
1459
-
1460
- for freq in ["M" , "2M" , "3M" ]:
1461
- p = Period ("NaT" , freq = freq )
1462
- assert p is NaT
1463
- for o in [offsets .MonthEnd (2 ), offsets .MonthEnd (12 )]:
1464
- assert p - o is NaT
1465
-
1466
- for o in [
1467
- offsets .YearBegin (2 ),
1468
- offsets .MonthBegin (1 ),
1469
- offsets .Minute (),
1470
- np .timedelta64 (365 , "D" ),
1471
- timedelta (365 ),
1472
- ]:
1473
- assert p - o is NaT
1474
-
1475
- # freq is Tick
1476
- for freq in ["D" , "2D" , "3D" ]:
1477
- p = Period ("NaT" , freq = freq )
1478
- assert p is NaT
1479
- for o in [
1480
- offsets .Day (5 ),
1481
- offsets .Hour (24 ),
1482
- np .timedelta64 (2 , "D" ),
1483
- np .timedelta64 (3600 * 24 , "s" ),
1484
- timedelta (- 2 ),
1485
- timedelta (hours = 48 ),
1486
- ]:
1487
- assert p - o is NaT
1488
-
1489
- for o in [
1490
- offsets .YearBegin (2 ),
1491
- offsets .MonthBegin (1 ),
1492
- offsets .Minute (),
1493
- np .timedelta64 (4 , "h" ),
1494
- timedelta (hours = 23 ),
1495
- ]:
1496
- assert p - o is NaT
1497
-
1498
- for freq in ["H" , "2H" , "3H" ]:
1499
- p = Period ("NaT" , freq = freq )
1500
- assert p is NaT
1501
- for o in [
1502
- offsets .Day (2 ),
1503
- offsets .Hour (3 ),
1504
- np .timedelta64 (3 , "h" ),
1505
- np .timedelta64 (3600 , "s" ),
1506
- timedelta (minutes = 120 ),
1507
- timedelta (days = 4 , minutes = 180 ),
1508
- ]:
1509
- assert p - o is NaT
1510
-
1511
- for o in [
1512
- offsets .YearBegin (2 ),
1513
- offsets .MonthBegin (1 ),
1514
- offsets .Minute (),
1515
- np .timedelta64 (3200 , "s" ),
1516
- timedelta (hours = 23 , minutes = 30 ),
1517
- ]:
1518
- assert p - o is NaT
1519
-
1520
1355
@pytest .mark .parametrize ("freq" , ["M" , "2M" , "3M" ])
1521
- def test_nat_ops (self , freq ):
1522
- p = Period ("NaT" , freq = freq )
1523
- assert p is NaT
1524
- assert p + 1 is NaT
1525
- assert 1 + p is NaT
1526
- assert p - 1 is NaT
1527
- assert p - Period ("2011-01" , freq = freq ) is NaT
1528
- assert Period ("2011-01" , freq = freq ) - p is NaT
1356
+ def test_period_addsub_nat (self , freq ):
1357
+ assert NaT - Period ("2011-01" , freq = freq ) is NaT
1358
+ assert Period ("2011-01" , freq = freq ) - NaT is NaT
1529
1359
1530
1360
def test_period_ops_offset (self ):
1531
1361
p = Period ("2011-04-01" , freq = "D" )
0 commit comments