Skip to content

Commit a7ee890

Browse files
committed
DOC: add doc-strings to tz_convert/tz_localize in tslib.pyx
TST: more tests, xref pandas-dev#15823, xref pandas-dev#11708
1 parent 44ff21d commit a7ee890

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

pandas/_libs/tslib.pyx

+36-2
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,7 @@ cpdef convert_str_to_tsobject(object ts, object tz, object unit,
15701570
if tz is not None:
15711571
# shift for _localize_tso
15721572
ts = tz_localize_to_utc(np.array([ts], dtype='i8'), tz,
1573-
ambiguous='raise',
1573+
ambiguous='raise',
15741574
errors='raise')[0]
15751575
except ValueError:
15761576
try:
@@ -4075,7 +4075,23 @@ except:
40754075
have_pytz = False
40764076

40774077

4078+
@cython.boundscheck(False)
4079+
@cython.wraparound(False)
40784080
def tz_convert(ndarray[int64_t] vals, object tz1, object tz2):
4081+
"""
4082+
Convert the values (in i8) from timezone1 to timezone2
4083+
4084+
Parameters
4085+
----------
4086+
vals : int64 ndarray
4087+
tz1 : string / timezone object
4088+
tz2 : string / timezone object
4089+
4090+
Returns
4091+
-------
4092+
int64 ndarray of converted
4093+
"""
4094+
40794095
cdef:
40804096
ndarray[int64_t] utc_dates, tt, result, trans, deltas
40814097
Py_ssize_t i, j, pos, n = len(vals)
@@ -4177,6 +4193,23 @@ def tz_convert(ndarray[int64_t] vals, object tz1, object tz2):
41774193

41784194

41794195
def tz_convert_single(int64_t val, object tz1, object tz2):
4196+
"""
4197+
Convert the val (in i8) from timezone1 to timezone2
4198+
4199+
This is a single timezone versoin of tz_convert
4200+
4201+
Parameters
4202+
----------
4203+
val : int64
4204+
tz1 : string / timezone object
4205+
tz2 : string / timezone object
4206+
4207+
Returns
4208+
-------
4209+
int64 converted
4210+
4211+
"""
4212+
41804213
cdef:
41814214
ndarray[int64_t] trans, deltas
41824215
Py_ssize_t pos
@@ -4376,7 +4409,7 @@ cpdef ndarray _unbox_utcoffsets(object transinfo):
43764409
def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None,
43774410
object errors='raise'):
43784411
"""
4379-
Localize tzinfo-naive DateRange to given time zone (using pytz). If
4412+
Localize tzinfo-naive i8 to given time zone (using pytz). If
43804413
there are ambiguities in the values, raise AmbiguousTimeError.
43814414
43824415
Returns
@@ -4548,6 +4581,7 @@ def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None,
45484581

45494582
return result
45504583

4584+
45514585
cdef inline bisect_right_i8(int64_t *data, int64_t val, Py_ssize_t n):
45524586
cdef Py_ssize_t pivot, left = 0, right = n
45534587

pandas/tests/tseries/test_timezones.py

+31-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# pylint: disable-msg=E1101,W0612
2+
import pytest
23
import pytz
34
import numpy as np
45
from distutils.version import LooseVersion
@@ -167,17 +168,43 @@ def test_timestamp_constructor_near_dst_boundary(self):
167168
for tz in ['Europe/Brussels', 'Europe/Prague']:
168169
result = Timestamp('2015-10-25 01:00', tz=tz)
169170
expected = Timestamp('2015-10-25 01:00').tz_localize(tz)
170-
self.assertEqual(result, expected)
171+
assert result == expected
171172

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

175176
result = Timestamp('2017-03-26 01:00', tz='Europe/Paris')
176177
expected = Timestamp('2017-03-26 01:00').tz_localize('Europe/Paris')
177-
self.assertEqual(result, expected)
178+
assert result == expected
179+
180+
with pytest.raises(pytz.NonExistentTimeError):
181+
Timestamp('2017-03-26 02:00', tz='Europe/Paris')
182+
183+
# GH 11708
184+
result = to_datetime("2015-11-18 15:30:00+05:30").tz_localize(
185+
'UTC').tz_convert('Asia/Kolkata')
186+
expected = Timestamp('2015-11-18 15:30:00+0530', tz='Asia/Kolkata')
187+
assert result == expected
188+
189+
# GH 15823
190+
result = Timestamp('2017-03-26 00:00', tz='Europe/Paris')
191+
expected = Timestamp('2017-03-26 00:00:00+0100', tz='Europe/Paris')
192+
assert result == expected
193+
194+
result = Timestamp('2017-03-26 01:00', tz='Europe/Paris')
195+
expected = Timestamp('2017-03-26 01:00:00+0100', tz='Europe/Paris')
196+
assert result == expected
178197

179-
with tm.assertRaises(pytz.NonExistentTimeError):
198+
with pytest.raises(pytz.NonExistentTimeError):
180199
Timestamp('2017-03-26 02:00', tz='Europe/Paris')
200+
result = Timestamp('2017-03-26 02:00:00+0100', tz='Europe/Paris')
201+
expected = Timestamp(result.value).tz_localize(
202+
'UTC').tz_convert('Europe/Paris')
203+
assert result == expected
204+
205+
result = Timestamp('2017-03-26 03:00', tz='Europe/Paris')
206+
expected = Timestamp('2017-03-26 03:00:00+0200', tz='Europe/Paris')
207+
assert result == expected
181208

182209
def test_timestamp_to_datetime_tzoffset(self):
183210
# tzoffset

0 commit comments

Comments
 (0)