Skip to content

Commit

Permalink
DOC: add doc-strings to tz_convert/tz_localize in tslib.pyx
Browse files Browse the repository at this point in the history
TST: more tests, xref pandas-dev#15823, xref pandas-dev#11708
  • Loading branch information
jreback committed Apr 8, 2017
1 parent 44ff21d commit a7ee890
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
38 changes: 36 additions & 2 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,7 @@ cpdef convert_str_to_tsobject(object ts, object tz, object unit,
if tz is not None:
# shift for _localize_tso
ts = tz_localize_to_utc(np.array([ts], dtype='i8'), tz,
ambiguous='raise',
ambiguous='raise',
errors='raise')[0]
except ValueError:
try:
Expand Down Expand Up @@ -4075,7 +4075,23 @@ except:
have_pytz = False


@cython.boundscheck(False)
@cython.wraparound(False)
def tz_convert(ndarray[int64_t] vals, object tz1, object tz2):
"""
Convert the values (in i8) from timezone1 to timezone2
Parameters
----------
vals : int64 ndarray
tz1 : string / timezone object
tz2 : string / timezone object
Returns
-------
int64 ndarray of converted
"""

cdef:
ndarray[int64_t] utc_dates, tt, result, trans, deltas
Py_ssize_t i, j, pos, n = len(vals)
Expand Down Expand Up @@ -4177,6 +4193,23 @@ def tz_convert(ndarray[int64_t] vals, object tz1, object tz2):


def tz_convert_single(int64_t val, object tz1, object tz2):
"""
Convert the val (in i8) from timezone1 to timezone2
This is a single timezone versoin of tz_convert
Parameters
----------
val : int64
tz1 : string / timezone object
tz2 : string / timezone object
Returns
-------
int64 converted
"""

cdef:
ndarray[int64_t] trans, deltas
Py_ssize_t pos
Expand Down Expand Up @@ -4376,7 +4409,7 @@ cpdef ndarray _unbox_utcoffsets(object transinfo):
def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None,
object errors='raise'):
"""
Localize tzinfo-naive DateRange to given time zone (using pytz). If
Localize tzinfo-naive i8 to given time zone (using pytz). If
there are ambiguities in the values, raise AmbiguousTimeError.
Returns
Expand Down Expand Up @@ -4548,6 +4581,7 @@ def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None,

return result


cdef inline bisect_right_i8(int64_t *data, int64_t val, Py_ssize_t n):
cdef Py_ssize_t pivot, left = 0, right = n

Expand Down
35 changes: 31 additions & 4 deletions pandas/tests/tseries/test_timezones.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# pylint: disable-msg=E1101,W0612
import pytest
import pytz
import numpy as np
from distutils.version import LooseVersion
Expand Down Expand Up @@ -167,17 +168,43 @@ def test_timestamp_constructor_near_dst_boundary(self):
for tz in ['Europe/Brussels', 'Europe/Prague']:
result = Timestamp('2015-10-25 01:00', tz=tz)
expected = Timestamp('2015-10-25 01:00').tz_localize(tz)
self.assertEqual(result, expected)
assert result == expected

with tm.assertRaises(pytz.AmbiguousTimeError):
with pytest.raises(pytz.AmbiguousTimeError):
Timestamp('2015-10-25 02:00', tz=tz)

result = Timestamp('2017-03-26 01:00', tz='Europe/Paris')
expected = Timestamp('2017-03-26 01:00').tz_localize('Europe/Paris')
self.assertEqual(result, expected)
assert result == expected

with pytest.raises(pytz.NonExistentTimeError):
Timestamp('2017-03-26 02:00', tz='Europe/Paris')

# GH 11708
result = to_datetime("2015-11-18 15:30:00+05:30").tz_localize(
'UTC').tz_convert('Asia/Kolkata')
expected = Timestamp('2015-11-18 15:30:00+0530', tz='Asia/Kolkata')
assert result == expected

# GH 15823
result = Timestamp('2017-03-26 00:00', tz='Europe/Paris')
expected = Timestamp('2017-03-26 00:00:00+0100', tz='Europe/Paris')
assert result == expected

result = Timestamp('2017-03-26 01:00', tz='Europe/Paris')
expected = Timestamp('2017-03-26 01:00:00+0100', tz='Europe/Paris')
assert result == expected

with tm.assertRaises(pytz.NonExistentTimeError):
with pytest.raises(pytz.NonExistentTimeError):
Timestamp('2017-03-26 02:00', tz='Europe/Paris')
result = Timestamp('2017-03-26 02:00:00+0100', tz='Europe/Paris')
expected = Timestamp(result.value).tz_localize(
'UTC').tz_convert('Europe/Paris')
assert result == expected

result = Timestamp('2017-03-26 03:00', tz='Europe/Paris')
expected = Timestamp('2017-03-26 03:00:00+0200', tz='Europe/Paris')
assert result == expected

def test_timestamp_to_datetime_tzoffset(self):
# tzoffset
Expand Down

0 comments on commit a7ee890

Please sign in to comment.