Skip to content

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

Merged
merged 5 commits into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 1 addition & 85 deletions pandas/tests/tseries/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@
import pytest

import pytz
import dateutil
import numpy as np

from datetime import datetime

import pandas.util.testing as tm
from pandas.core.indexes.datetimes import date_range
from pandas._libs import tslib
Copy link
Contributor

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?

Copy link
Member Author

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.

from pandas._libs.tslibs import timezones, conversion
from pandas._libs.tslibs import timezones
from pandas import Timestamp


Expand Down Expand Up @@ -111,82 +106,3 @@ def localize(self, tz, x):
def normalize(self, ts):
# no-op for dateutil
return ts

def test_tzlocal(self):
# 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


class TestTimeZoneCacheKey(object):

@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))


class TestTslib(object):

def test_tslib_tz_convert(self):
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)

for tz in ['UTC', 'Asia/Tokyo', 'US/Eastern', 'Europe/Moscow']:
# 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
# of DST
compare_local_to_utc(tz_didx, utc_didx)

tz_didx = date_range('2000-01-01', '2020-01-01', freq='D', tz=tz)
utc_didx = date_range('2000-01-01', '2020-01-01', freq='D')
compare_utc_to_local(tz_didx, utc_didx)
compare_local_to_utc(tz_didx, utc_didx)

tz_didx = date_range('2000-01-01', '2100-01-01', freq='A', tz=tz)
utc_didx = date_range('2000-01-01', '2100-01-01', freq='A')
compare_utc_to_local(tz_didx, utc_didx)
compare_local_to_utc(tz_didx, utc_didx)

# Check empty array
result = conversion.tz_convert(np.array([], dtype=np.int64),
timezones.maybe_get_tz('US/Eastern'),
timezones.maybe_get_tz('Asia/Tokyo'))
tm.assert_numpy_array_equal(result, np.array([], dtype=np.int64))

# Check all-NaT array
result = conversion.tz_convert(np.array([tslib.iNaT], dtype=np.int64),
timezones.maybe_get_tz('US/Eastern'),
timezones.maybe_get_tz('Asia/Tokyo'))
tm.assert_numpy_array_equal(result, np.array(
[tslib.iNaT], dtype=np.int64))
63 changes: 63 additions & 0 deletions pandas/tests/tslibs/test_conversion.py
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could parametrize

Copy link
Contributor

Choose a reason for hiding this comment

The 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):
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
40 changes: 40 additions & 0 deletions pandas/tests/tslibs/test_timezones.py
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):
Copy link
Contributor

Choose a reason for hiding this comment

The 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