14
14
15
15
16
16
import datetime
17
+ import operator
17
18
18
19
import numpy as np
19
20
import pandas as pd
@@ -28,12 +29,23 @@ def temporal_dfs(session):
28
29
"datetime_col" : [
29
30
pd .Timestamp ("2025-02-01 01:00:01" ),
30
31
pd .Timestamp ("2019-01-02 02:00:00" ),
32
+ pd .Timestamp ("1997-01-01 19:00:00" ),
31
33
],
32
34
"timestamp_col" : [
33
35
pd .Timestamp ("2023-01-01 01:00:01" , tz = "UTC" ),
34
36
pd .Timestamp ("2024-01-02 02:00:00" , tz = "UTC" ),
37
+ pd .Timestamp ("2005-03-05 02:00:00" , tz = "UTC" ),
38
+ ],
39
+ "timedelta_col_1" : [
40
+ pd .Timedelta (3 , "s" ),
41
+ pd .Timedelta (- 4 , "d" ),
42
+ pd .Timedelta (5 , "h" ),
43
+ ],
44
+ "timedelta_col_2" : [
45
+ pd .Timedelta (2 , "s" ),
46
+ pd .Timedelta (- 4 , "d" ),
47
+ pd .Timedelta (6 , "h" ),
35
48
],
36
- "timedelta_col" : [pd .Timedelta (3 , "s" ), pd .Timedelta (- 4 , "d" )],
37
49
}
38
50
)
39
51
@@ -53,10 +65,10 @@ def test_timestamp_add__ts_series_plus_td_series(temporal_dfs, column, pd_dtype)
53
65
bf_df , pd_df = temporal_dfs
54
66
55
67
actual_result = (
56
- (bf_df [column ] + bf_df ["timedelta_col " ]).to_pandas ().astype (pd_dtype )
68
+ (bf_df [column ] + bf_df ["timedelta_col_1 " ]).to_pandas ().astype (pd_dtype )
57
69
)
58
70
59
- expected_result = pd_df [column ] + pd_df ["timedelta_col " ]
71
+ expected_result = pd_df [column ] + pd_df ["timedelta_col_1 " ]
60
72
pandas .testing .assert_series_equal (
61
73
actual_result , expected_result , check_index_type = False
62
74
)
@@ -94,10 +106,10 @@ def test_timestamp_add__td_series_plus_ts_series(temporal_dfs, column, pd_dtype)
94
106
bf_df , pd_df = temporal_dfs
95
107
96
108
actual_result = (
97
- (bf_df ["timedelta_col " ] + bf_df [column ]).to_pandas ().astype (pd_dtype )
109
+ (bf_df ["timedelta_col_1 " ] + bf_df [column ]).to_pandas ().astype (pd_dtype )
98
110
)
99
111
100
- expected_result = pd_df ["timedelta_col " ] + pd_df [column ]
112
+ expected_result = pd_df ["timedelta_col_1 " ] + pd_df [column ]
101
113
pandas .testing .assert_series_equal (
102
114
actual_result , expected_result , check_index_type = False
103
115
)
@@ -120,10 +132,10 @@ def test_timestamp_add__ts_literal_plus_td_series(temporal_dfs):
120
132
timestamp = pd .Timestamp ("2025-01-01" , tz = "UTC" )
121
133
122
134
actual_result = (
123
- (timestamp + bf_df ["timedelta_col " ]).to_pandas ().astype ("datetime64[ns, UTC]" )
135
+ (timestamp + bf_df ["timedelta_col_1 " ]).to_pandas ().astype ("datetime64[ns, UTC]" )
124
136
)
125
137
126
- expected_result = timestamp + pd_df ["timedelta_col " ]
138
+ expected_result = timestamp + pd_df ["timedelta_col_1 " ]
127
139
pandas .testing .assert_series_equal (
128
140
actual_result , expected_result , check_index_type = False
129
141
)
@@ -140,10 +152,10 @@ def test_timestamp_add_with_numpy_op(temporal_dfs, column, pd_dtype):
140
152
bf_df , pd_df = temporal_dfs
141
153
142
154
actual_result = (
143
- np .add (bf_df [column ], bf_df ["timedelta_col " ]).to_pandas ().astype (pd_dtype )
155
+ np .add (bf_df [column ], bf_df ["timedelta_col_1 " ]).to_pandas ().astype (pd_dtype )
144
156
)
145
157
146
- expected_result = np .add (pd_df [column ], pd_df ["timedelta_col " ])
158
+ expected_result = np .add (pd_df [column ], pd_df ["timedelta_col_1 " ])
147
159
pandas .testing .assert_series_equal (
148
160
actual_result , expected_result , check_index_type = False
149
161
)
@@ -164,3 +176,105 @@ def test_timestamp_add_dataframes(temporal_dfs):
164
176
pandas .testing .assert_frame_equal (
165
177
actual_result , expected_result , check_index_type = False
166
178
)
179
+
180
+
181
+ @pytest .mark .parametrize (
182
+ "compare_func" ,
183
+ [
184
+ pytest .param (operator .gt , id = "gt" ),
185
+ pytest .param (operator .ge , id = "ge" ),
186
+ pytest .param (operator .eq , id = "eq" ),
187
+ pytest .param (operator .ne , id = "ne" ),
188
+ pytest .param (operator .lt , id = "lt" ),
189
+ pytest .param (operator .le , id = "le" ),
190
+ ],
191
+ )
192
+ def test_timedelta_series_comparison (temporal_dfs , compare_func ):
193
+ bf_df , pd_df = temporal_dfs
194
+
195
+ actual_result = compare_func (
196
+ bf_df ["timedelta_col_1" ], bf_df ["timedelta_col_2" ]
197
+ ).to_pandas ()
198
+
199
+ expected_result = compare_func (
200
+ pd_df ["timedelta_col_1" ], pd_df ["timedelta_col_2" ]
201
+ ).astype ("boolean" )
202
+ pandas .testing .assert_series_equal (
203
+ actual_result , expected_result , check_index_type = False
204
+ )
205
+
206
+
207
+ @pytest .mark .parametrize (
208
+ "compare_func" ,
209
+ [
210
+ pytest .param (operator .gt , id = "gt" ),
211
+ pytest .param (operator .ge , id = "ge" ),
212
+ pytest .param (operator .eq , id = "eq" ),
213
+ pytest .param (operator .ne , id = "ne" ),
214
+ pytest .param (operator .lt , id = "lt" ),
215
+ pytest .param (operator .le , id = "le" ),
216
+ ],
217
+ )
218
+ def test_timedelta_series_and_literal_comparison (temporal_dfs , compare_func ):
219
+ bf_df , pd_df = temporal_dfs
220
+ literal = pd .Timedelta (3 , "s" )
221
+
222
+ actual_result = compare_func (literal , bf_df ["timedelta_col_2" ]).to_pandas ()
223
+
224
+ expected_result = compare_func (literal , pd_df ["timedelta_col_2" ]).astype ("boolean" )
225
+ pandas .testing .assert_series_equal (
226
+ actual_result , expected_result , check_index_type = False
227
+ )
228
+
229
+
230
+ def test_timedelta_filtering (session ):
231
+ pd_series = pd .Series (
232
+ [
233
+ pd .Timestamp ("2025-01-01 01:00:00" ),
234
+ pd .Timestamp ("2025-01-01 02:00:00" ),
235
+ pd .Timestamp ("2025-01-01 03:00:00" ),
236
+ ]
237
+ )
238
+ bf_series = session .read_pandas (pd_series )
239
+ timestamp = pd .Timestamp ("2025-01-01, 00:00:01" )
240
+
241
+ actual_result = (
242
+ bf_series [((bf_series - timestamp ) > pd .Timedelta (1 , "h" ))]
243
+ .to_pandas ()
244
+ .astype ("<M8[ns]" )
245
+ )
246
+
247
+ expected_result = pd_series [(pd_series - timestamp ) > pd .Timedelta (1 , "h" )]
248
+ pandas .testing .assert_series_equal (
249
+ actual_result , expected_result , check_index_type = False
250
+ )
251
+
252
+
253
+ def test_timedelta_ordering (session ):
254
+ pd_df = pd .DataFrame (
255
+ {
256
+ "col_1" : [
257
+ pd .Timestamp ("2025-01-01 01:00:00" ),
258
+ pd .Timestamp ("2025-01-01 02:00:00" ),
259
+ pd .Timestamp ("2025-01-01 03:00:00" ),
260
+ ],
261
+ "col_2" : [
262
+ pd .Timestamp ("2025-01-01 01:00:02" ),
263
+ pd .Timestamp ("2025-01-01 02:00:01" ),
264
+ pd .Timestamp ("2025-01-01 02:59:59" ),
265
+ ],
266
+ }
267
+ )
268
+ bf_df = session .read_pandas (pd_df )
269
+
270
+ actual_result = (
271
+ (bf_df ["col_2" ] - bf_df ["col_1" ])
272
+ .sort_values ()
273
+ .to_pandas ()
274
+ .astype ("timedelta64[ns]" )
275
+ )
276
+
277
+ expected_result = (pd_df ["col_2" ] - pd_df ["col_1" ]).sort_values ()
278
+ pandas .testing .assert_series_equal (
279
+ actual_result , expected_result , check_index_type = False
280
+ )
0 commit comments