|
5 | 5 | import pytz
|
6 | 6 | import dateutil.tz
|
7 | 7 |
|
| 8 | +from pandas._libs import tslib |
8 | 9 | from pandas._libs.tslibs import timezones
|
9 | 10 | from pandas import Timestamp
|
10 | 11 |
|
@@ -35,3 +36,33 @@ def test_tzlocal():
|
35 | 36 | offset = dateutil.tz.tzlocal().utcoffset(datetime(2011, 1, 1))
|
36 | 37 | offset = offset.total_seconds() * 1000000000
|
37 | 38 | assert ts.value + offset == Timestamp('2011-01-01').value
|
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize('eastern, localize', [ |
| 42 | + (pytz.timezone('US/Eastern'), lambda tz, x: tz.localize(x)), |
| 43 | + (dateutil.tz.gettz('US/Eastern'), lambda tz, x: x.replace(tzinfo=tz))]) |
| 44 | +def test_infer_tz(eastern, localize): |
| 45 | + utc = pytz.utc |
| 46 | + |
| 47 | + start_naive = datetime(2001, 1, 1) |
| 48 | + end_naive = datetime(2009, 1, 1) |
| 49 | + |
| 50 | + start = localize(eastern, start_naive) |
| 51 | + end = localize(eastern, end_naive) |
| 52 | + |
| 53 | + assert (timezones.infer_tzinfo(start, end) is |
| 54 | + tslib._localize_pydatetime(start_naive, eastern).tzinfo) |
| 55 | + assert (timezones.infer_tzinfo(start, None) is |
| 56 | + tslib._localize_pydatetime(start_naive, eastern).tzinfo) |
| 57 | + assert (timezones.infer_tzinfo(None, end) is |
| 58 | + tslib._localize_pydatetime(end_naive, eastern).tzinfo) |
| 59 | + |
| 60 | + start = utc.localize(start_naive) |
| 61 | + end = utc.localize(end_naive) |
| 62 | + assert timezones.infer_tzinfo(start, end) is utc |
| 63 | + |
| 64 | + end = tslib._localize_pydatetime(end_naive, eastern) |
| 65 | + with pytest.raises(Exception): |
| 66 | + timezones.infer_tzinfo(start, end) |
| 67 | + with pytest.raises(Exception): |
| 68 | + timezones.infer_tzinfo(end, start) |
0 commit comments