|
11 | 11 | import polars as pl
|
12 | 12 | from polars.exceptions import ComputeError, InvalidOperationError
|
13 | 13 | from polars.testing import assert_series_equal
|
| 14 | +from polars.testing.asserts.frame import assert_frame_equal |
14 | 15 |
|
15 | 16 | if TYPE_CHECKING:
|
16 | 17 | from hypothesis.strategies import DrawFn
|
@@ -197,3 +198,94 @@ def test_to_datetime_two_digit_year_17213(
|
197 | 198 | ) -> None:
|
198 | 199 | result = pl.Series([inputs]).str.to_date(format=format).item()
|
199 | 200 | assert result == expected
|
| 201 | + |
| 202 | + |
| 203 | +def test_to_datetime_column_input_to_ambiguous() -> None: |
| 204 | + q = pl.LazyFrame( |
| 205 | + { |
| 206 | + "a": ["2020-01-01 01:00Z", "2020-01-01 02:00Z"], |
| 207 | + "b": ["raise", "earliest"], |
| 208 | + } |
| 209 | + ).select(pl.col.a.str.to_datetime("%Y-%m-%d %H:%M%#z", ambiguous=pl.col.b)) |
| 210 | + |
| 211 | + expect = pl.DataFrame( |
| 212 | + [ |
| 213 | + pl.Series( |
| 214 | + "a", |
| 215 | + [ |
| 216 | + datetime(2020, 1, 1, 1, 0, tzinfo=ZoneInfo(key="UTC")), |
| 217 | + datetime(2020, 1, 1, 2, 0, tzinfo=ZoneInfo(key="UTC")), |
| 218 | + ], |
| 219 | + dtype=pl.Datetime(time_unit="us", time_zone="UTC"), |
| 220 | + ), |
| 221 | + ] |
| 222 | + ) |
| 223 | + |
| 224 | + assert_frame_equal(q.collect(), expect) |
| 225 | + |
| 226 | + |
| 227 | +def test_to_datetime_fallible_predicate_pushdown() -> None: |
| 228 | + df = pl.DataFrame({"x": ["2020-10-25 01:00", "X"]}) |
| 229 | + |
| 230 | + c = pl.first() |
| 231 | + |
| 232 | + expect_fail = [ |
| 233 | + c.str.to_datetime( |
| 234 | + "%Y-%m-%d %H:%M", |
| 235 | + time_zone="Europe/London", |
| 236 | + ambiguous="raise", |
| 237 | + strict=False, |
| 238 | + ), |
| 239 | + c.str.to_datetime( |
| 240 | + "%Y-%m-%d %H:%M", |
| 241 | + time_zone="Europe/London", |
| 242 | + ambiguous="null", |
| 243 | + strict=True, |
| 244 | + ), |
| 245 | + c.str.to_datetime("%Y-%m-%d %H:%M", strict=True), |
| 246 | + ] |
| 247 | + |
| 248 | + expect_pass = [ |
| 249 | + c.str.to_datetime( |
| 250 | + "%Y-%m-%d %H:%M", |
| 251 | + time_zone="Europe/London", |
| 252 | + ambiguous="null", |
| 253 | + strict=False, |
| 254 | + ), |
| 255 | + c.str.to_datetime( |
| 256 | + "%Y-%m-%d %H:%M", |
| 257 | + time_zone="Europe/London", |
| 258 | + ambiguous="earliest", |
| 259 | + strict=False, |
| 260 | + ), |
| 261 | + c.str.to_datetime( |
| 262 | + "%Y-%m-%d %H:%M", |
| 263 | + time_zone="Europe/London", |
| 264 | + ambiguous="latest", |
| 265 | + strict=False, |
| 266 | + ), |
| 267 | + ] |
| 268 | + |
| 269 | + for expr in expect_fail: |
| 270 | + with pytest.raises(Exception): # noqa: B017 |
| 271 | + df.select(expr) |
| 272 | + |
| 273 | + for expr in expect_pass: |
| 274 | + df.select(expr) |
| 275 | + |
| 276 | + lf = df.with_columns(false=False).lazy().filter("false") |
| 277 | + |
| 278 | + for e in expect_pass: |
| 279 | + q = lf.filter(e.is_not_null()) |
| 280 | + plan = q.explain() |
| 281 | + assert plan.count("FILTER") == 1 |
| 282 | + assert_frame_equal( |
| 283 | + q.collect(), q.collect(optimizations=pl.QueryOptFlags.none()) |
| 284 | + ) |
| 285 | + |
| 286 | + for e in expect_fail: |
| 287 | + q = lf.filter(e.is_not_null()) |
| 288 | + plan = q.explain() |
| 289 | + assert_frame_equal( |
| 290 | + q.collect(), q.collect(optimizations=pl.QueryOptFlags.none()) |
| 291 | + ) |
0 commit comments