11from __future__ import annotations
22
3- import contextlib
43import sys
54import typing
65from collections import OrderedDict
1817import polars .selectors as cs
1918from polars ._utils .construction import iterable_to_pydf
2019from polars .datatypes import DTYPE_TEMPORAL_UNITS , INTEGER_DTYPES
21- from polars .exceptions import TimeZoneAwareConstructorWarning
2220from polars .testing import (
2321 assert_frame_equal ,
2422 assert_frame_not_equal ,
@@ -2427,7 +2425,10 @@ def test_init_datetimes_with_timezone() -> None:
24272425 },
24282426 ):
24292427 result = pl .DataFrame ( # type: ignore[arg-type]
2430- data = {"d1" : [dtm ], "d2" : [dtm ]},
2428+ data = {
2429+ "d1" : [dtm .replace (tzinfo = ZoneInfo (tz_us ))],
2430+ "d2" : [dtm .replace (tzinfo = ZoneInfo (tz_europe ))],
2431+ },
24312432 ** type_overrides ,
24322433 )
24332434 expected = pl .DataFrame (
@@ -2446,25 +2447,22 @@ def test_init_datetimes_with_timezone() -> None:
24462447 "dtype_time_zone" ,
24472448 "expected_time_zone" ,
24482449 "expected_item" ,
2449- "warn" ,
24502450 ),
24512451 [
2452- (None , "" , None , None , datetime (2020 , 1 , 1 ), False ),
2452+ (None , "" , None , None , datetime (2020 , 1 , 1 )),
24532453 (
24542454 timezone (timedelta (hours = - 8 )),
24552455 "-08:00" ,
24562456 "UTC" ,
24572457 "UTC" ,
24582458 datetime (2020 , 1 , 1 , 8 , tzinfo = timezone .utc ),
2459- False ,
24602459 ),
24612460 (
24622461 timezone (timedelta (hours = - 8 )),
24632462 "-08:00" ,
24642463 None ,
24652464 "UTC" ,
24662465 datetime (2020 , 1 , 1 , 8 , tzinfo = timezone .utc ),
2467- True ,
24682466 ),
24692467 ],
24702468)
@@ -2474,19 +2472,11 @@ def test_init_vs_strptime_consistency(
24742472 dtype_time_zone : str | None ,
24752473 expected_time_zone : str ,
24762474 expected_item : datetime ,
2477- warn : bool ,
24782475) -> None :
2479- msg = r"UTC time zone"
2480- context_manager : contextlib .AbstractContextManager [pytest .WarningsRecorder | None ]
2481- if warn :
2482- context_manager = pytest .warns (TimeZoneAwareConstructorWarning , match = msg )
2483- else :
2484- context_manager = contextlib .nullcontext ()
2485- with context_manager :
2486- result_init = pl .Series (
2487- [datetime (2020 , 1 , 1 , tzinfo = tzinfo )],
2488- dtype = pl .Datetime ("us" , dtype_time_zone ),
2489- )
2476+ result_init = pl .Series (
2477+ [datetime (2020 , 1 , 1 , tzinfo = tzinfo )],
2478+ dtype = pl .Datetime ("us" , dtype_time_zone ),
2479+ )
24902480 result_strptime = pl .Series ([f"2020-01-01 00:00{ offset } " ]).str .strptime (
24912481 pl .Datetime ("us" , dtype_time_zone )
24922482 )
@@ -2495,13 +2485,12 @@ def test_init_vs_strptime_consistency(
24952485 assert_series_equal (result_init , result_strptime )
24962486
24972487
2498- def test_init_vs_strptime_consistency_raises () -> None :
2499- msg = "-aware datetimes are converted to UTC"
2500- with pytest .raises (ValueError , match = msg ):
2501- pl .Series (
2502- [datetime (2020 , 1 , 1 , tzinfo = timezone (timedelta (hours = - 8 )))],
2503- dtype = pl .Datetime ("us" , "US/Pacific" ),
2504- )
2488+ def test_init_vs_strptime_consistency_converts () -> None :
2489+ result = pl .Series (
2490+ [datetime (2020 , 1 , 1 , tzinfo = timezone (timedelta (hours = - 8 )))],
2491+ dtype = pl .Datetime ("us" , "US/Pacific" ),
2492+ ).item ()
2493+ assert result == datetime (2020 , 1 , 1 , 0 , 0 , tzinfo = ZoneInfo (key = "US/Pacific" ))
25052494 result = (
25062495 pl .Series (["2020-01-01 00:00-08:00" ])
25072496 .str .strptime (pl .Datetime ("us" , "US/Pacific" ))
0 commit comments