16
16
from pandas .core .reshape .merge import MergeError
17
17
18
18
19
+ @pytest .fixture (params = ["s" , "ms" , "us" , "ns" ])
20
+ def unit (request ):
21
+ """
22
+ Resolution for datetimelike dtypes.
23
+ """
24
+ return request .param
25
+
26
+
19
27
class TestAsOfMerge :
20
28
def read_data (self , datapath , name , dedupe = False ):
21
29
path = datapath ("reshape" , "merge" , "data" , name )
@@ -63,8 +71,13 @@ def test_examples1(self):
63
71
result = merge_asof (left , right , on = "a" )
64
72
tm .assert_frame_equal (result , expected )
65
73
66
- def test_examples2 (self ):
74
+ def test_examples2 (self , unit ):
67
75
"""doc-string examples"""
76
+ if unit == "s" :
77
+ pytest .skip (
78
+ "This test is invalid for unit='s' because that would "
79
+ "round the trades['time']]"
80
+ )
68
81
trades = pd .DataFrame (
69
82
{
70
83
"time" : to_datetime (
@@ -75,7 +88,7 @@ def test_examples2(self):
75
88
"20160525 13:30:00.048" ,
76
89
"20160525 13:30:00.048" ,
77
90
]
78
- ),
91
+ ). astype ( f"M8[ { unit } ]" ) ,
79
92
"ticker" : ["MSFT" , "MSFT" , "GOOG" , "GOOG" , "AAPL" ],
80
93
"price" : [51.95 , 51.95 , 720.77 , 720.92 , 98.00 ],
81
94
"quantity" : [75 , 155 , 100 , 100 , 100 ],
@@ -96,7 +109,7 @@ def test_examples2(self):
96
109
"20160525 13:30:00.072" ,
97
110
"20160525 13:30:00.075" ,
98
111
]
99
- ),
112
+ ). astype ( f"M8[ { unit } ]" ) ,
100
113
"ticker" : [
101
114
"GOOG" ,
102
115
"MSFT" ,
@@ -127,7 +140,7 @@ def test_examples2(self):
127
140
"20160525 13:30:00.048" ,
128
141
"20160525 13:30:00.048" ,
129
142
]
130
- ),
143
+ ). astype ( f"M8[ { unit } ]" ) ,
131
144
"ticker" : ["MSFT" , "MSFT" , "GOOG" , "GOOG" , "AAPL" ],
132
145
"price" : [51.95 , 51.95 , 720.77 , 720.92 , 98.00 ],
133
146
"quantity" : [75 , 155 , 100 , 100 , 100 ],
@@ -639,7 +652,7 @@ def test_tolerance_nearest(self):
639
652
result = merge_asof (left , right , on = "a" , direction = "nearest" , tolerance = 1 )
640
653
tm .assert_frame_equal (result , expected )
641
654
642
- def test_tolerance_tz (self ):
655
+ def test_tolerance_tz (self , unit ):
643
656
# GH 14844
644
657
left = pd .DataFrame (
645
658
{
@@ -648,6 +661,7 @@ def test_tolerance_tz(self):
648
661
freq = "D" ,
649
662
periods = 5 ,
650
663
tz = pytz .timezone ("UTC" ),
664
+ unit = unit ,
651
665
),
652
666
"value1" : np .arange (5 ),
653
667
}
@@ -659,6 +673,7 @@ def test_tolerance_tz(self):
659
673
freq = "D" ,
660
674
periods = 5 ,
661
675
tz = pytz .timezone ("UTC" ),
676
+ unit = unit ,
662
677
),
663
678
"value2" : list ("ABCDE" ),
664
679
}
@@ -672,6 +687,7 @@ def test_tolerance_tz(self):
672
687
freq = "D" ,
673
688
periods = 5 ,
674
689
tz = pytz .timezone ("UTC" ),
690
+ unit = unit ,
675
691
),
676
692
"value1" : np .arange (5 ),
677
693
"value2" : list ("BCDEE" ),
@@ -1314,22 +1330,27 @@ def test_by_mixed_tz_aware(self):
1314
1330
expected ["value_y" ] = np .array ([np .nan ], dtype = object )
1315
1331
tm .assert_frame_equal (result , expected )
1316
1332
1317
- def test_timedelta_tolerance_nearest (self ):
1333
+ def test_timedelta_tolerance_nearest (self , unit ):
1318
1334
# GH 27642
1335
+ if unit == "s" :
1336
+ pytest .skip (
1337
+ "This test is invalid with unit='s' because that would "
1338
+ "round left['time']"
1339
+ )
1319
1340
1320
1341
left = pd .DataFrame (
1321
1342
list (zip ([0 , 5 , 10 , 15 , 20 , 25 ], [0 , 1 , 2 , 3 , 4 , 5 ])),
1322
1343
columns = ["time" , "left" ],
1323
1344
)
1324
1345
1325
- left ["time" ] = pd .to_timedelta (left ["time" ], "ms" )
1346
+ left ["time" ] = pd .to_timedelta (left ["time" ], "ms" ). astype ( f"m8[ { unit } ]" )
1326
1347
1327
1348
right = pd .DataFrame (
1328
1349
list (zip ([0 , 3 , 9 , 12 , 15 , 18 ], [0 , 1 , 2 , 3 , 4 , 5 ])),
1329
1350
columns = ["time" , "right" ],
1330
1351
)
1331
1352
1332
- right ["time" ] = pd .to_timedelta (right ["time" ], "ms" )
1353
+ right ["time" ] = pd .to_timedelta (right ["time" ], "ms" ). astype ( f"m8[ { unit } ]" )
1333
1354
1334
1355
expected = pd .DataFrame (
1335
1356
list (
@@ -1342,7 +1363,7 @@ def test_timedelta_tolerance_nearest(self):
1342
1363
columns = ["time" , "left" , "right" ],
1343
1364
)
1344
1365
1345
- expected ["time" ] = pd .to_timedelta (expected ["time" ], "ms" )
1366
+ expected ["time" ] = pd .to_timedelta (expected ["time" ], "ms" ). astype ( f"m8[ { unit } ]" )
1346
1367
1347
1368
result = merge_asof (
1348
1369
left , right , on = "time" , tolerance = Timedelta ("1ms" ), direction = "nearest"
@@ -1400,12 +1421,17 @@ def test_merge_index_column_tz(self):
1400
1421
)
1401
1422
tm .assert_frame_equal (result , expected )
1402
1423
1403
- def test_left_index_right_index_tolerance (self ):
1424
+ def test_left_index_right_index_tolerance (self , unit ):
1404
1425
# https://github.com/pandas-dev/pandas/issues/35558
1405
- dr1 = pd .date_range (start = "1/1/2020" , end = "1/20/2020" , freq = "2D" ) + Timedelta (
1406
- seconds = 0.4
1407
- )
1408
- dr2 = pd .date_range (start = "1/1/2020" , end = "2/1/2020" )
1426
+ if unit == "s" :
1427
+ pytest .skip (
1428
+ "This test is invalid with unit='s' because that would round dr1"
1429
+ )
1430
+
1431
+ dr1 = pd .date_range (
1432
+ start = "1/1/2020" , end = "1/20/2020" , freq = "2D" , unit = unit
1433
+ ) + Timedelta (seconds = 0.4 ).as_unit (unit )
1434
+ dr2 = pd .date_range (start = "1/1/2020" , end = "2/1/2020" , unit = unit )
1409
1435
1410
1436
df1 = pd .DataFrame ({"val1" : "foo" }, index = pd .DatetimeIndex (dr1 ))
1411
1437
df2 = pd .DataFrame ({"val2" : "bar" }, index = pd .DatetimeIndex (dr2 ))
0 commit comments