-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
tests for tslibs.conversion and tslibs.timezones #19642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
984e94b
48e2dd1
91787b1
0179f57
6c30451
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
import pandas.util.testing as tm | ||
from pandas import date_range | ||
from pandas._libs.tslib import iNaT | ||
from pandas._libs.tslibs import conversion, timezones | ||
|
||
|
||
def compare_utc_to_local(tz_didx, utc_didx): | ||
f = lambda x: conversion.tz_convert_single(x, 'UTC', tz_didx.tz) | ||
result = conversion.tz_convert(tz_didx.asi8, 'UTC', tz_didx.tz) | ||
result_single = np.vectorize(f)(tz_didx.asi8) | ||
tm.assert_numpy_array_equal(result, result_single) | ||
|
||
|
||
def compare_local_to_utc(tz_didx, utc_didx): | ||
f = lambda x: conversion.tz_convert_single(x, tz_didx.tz, 'UTC') | ||
result = conversion.tz_convert(utc_didx.asi8, tz_didx.tz, 'UTC') | ||
result_single = np.vectorize(f)(utc_didx.asi8) | ||
tm.assert_numpy_array_equal(result, result_single) | ||
|
||
|
||
class TestTslib(object): | ||
|
||
@pytest.mark.parametrize('tz', ['UTC', 'Asia/Tokyo', | ||
'US/Eastern', 'Europe/Moscow']) | ||
def test_tz_convert_single_matches_tz_convert_hourly(self, tz): | ||
# US: 2014-03-09 - 2014-11-11 | ||
# MOSCOW: 2014-10-26 / 2014-12-31 | ||
tz_didx = date_range('2014-03-01', '2015-01-10', freq='H', tz=tz) | ||
utc_didx = date_range('2014-03-01', '2015-01-10', freq='H') | ||
compare_utc_to_local(tz_didx, utc_didx) | ||
# local tz to UTC can be differ in hourly (or higher) freqs because | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. blank line before comment |
||
# of DST | ||
compare_local_to_utc(tz_didx, utc_didx) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could parametrize There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls do this |
||
|
||
@pytest.mark.parametrize('tz', ['UTC', 'Asia/Tokyo', | ||
'US/Eastern', 'Europe/Moscow']) | ||
@pytest.mark.parametrize('freq', ['D', 'A']) | ||
def test_tz_convert_single_matches_tz_convert(self, tz, freq): | ||
tz_didx = date_range('2000-01-01', '2020-01-01', freq=freq, tz=tz) | ||
utc_didx = date_range('2000-01-01', '2020-01-01', freq=freq) | ||
compare_utc_to_local(tz_didx, utc_didx) | ||
compare_local_to_utc(tz_didx, utc_didx) | ||
|
||
def test_tz_convert_empty(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's parameterize empty/nat and make a single test |
||
# Check empty array | ||
arr = np.array([], dtype=np.int64) | ||
result = conversion.tz_convert(arr, | ||
timezones.maybe_get_tz('US/Eastern'), | ||
timezones.maybe_get_tz('Asia/Tokyo')) | ||
tm.assert_numpy_array_equal(result, arr) | ||
|
||
def test_tz_convert_nat(self): | ||
# Check all-NaT array | ||
arr = np.array([iNaT], dtype=np.int64) | ||
result = conversion.tz_convert(arr, | ||
timezones.maybe_get_tz('US/Eastern'), | ||
timezones.maybe_get_tz('Asia/Tokyo')) | ||
tm.assert_numpy_array_equal(result, arr) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# -*- coding: utf-8 -*- | ||
from datetime import datetime | ||
|
||
import pytest | ||
import pytz | ||
import dateutil.tz | ||
|
||
from pandas._libs.tslibs import timezones | ||
from pandas import Timestamp | ||
|
||
|
||
class TestTimeZoneCacheKey(object): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no reason to make this part of a class |
||
|
||
@pytest.mark.parametrize('tz_name', list(pytz.common_timezones)) | ||
def test_cache_keys_are_distinct_for_pytz_vs_dateutil(self, tz_name): | ||
if tz_name == 'UTC': | ||
# skip utc as it's a special case in dateutil | ||
return | ||
tz_p = timezones.maybe_get_tz(tz_name) | ||
tz_d = timezones.maybe_get_tz('dateutil/' + tz_name) | ||
if tz_d is None: | ||
# skip timezones that dateutil doesn't know about. | ||
return | ||
assert (timezones._p_tz_cache_key(tz_p) != | ||
timezones._p_tz_cache_key(tz_d)) | ||
|
||
|
||
def test_tzlocal(): | ||
# GH#13583 | ||
ts = Timestamp('2011-01-01', tz=dateutil.tz.tzlocal()) | ||
assert ts.tz == dateutil.tz.tzlocal() | ||
assert "tz='tzlocal()')" in repr(ts) | ||
|
||
tz = timezones.maybe_get_tz('tzlocal()') | ||
assert tz == dateutil.tz.tzlocal() | ||
|
||
# get offset using normal datetime for test | ||
offset = dateutil.tz.tzlocal().utcoffset(datetime(2011, 1, 1)) | ||
offset = offset.total_seconds() * 1000000000 | ||
assert ts.value + offset == Timestamp('2011-01-01').value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's left in this file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not much. I'll try to kill it off after clearing out the current PR backlog.