Skip to content

CLN: stronger typing in tzconversion #34504

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 2 commits into from
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/tzconversion.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ from numpy cimport int64_t


cdef int64_t tz_convert_utc_to_tzlocal(int64_t utc_val, tzinfo tz, bint* fold=*)
cpdef int64_t tz_convert_single(int64_t val, object tz1, object tz2)
cpdef int64_t tz_convert_single(int64_t val, tzinfo tz1, tzinfo tz2)
29 changes: 12 additions & 17 deletions pandas/_libs/tslibs/tzconversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ from pandas._libs.tslibs.timezones cimport (
# TODO: cdef scalar version to call from convert_str_to_tsobject
@cython.boundscheck(False)
@cython.wraparound(False)
def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None,
def tz_localize_to_utc(ndarray[int64_t] vals, tzinfo tz, object ambiguous=None,
object nonexistent=None):
"""
Localize tzinfo-naive i8 to given time zone (using pytz). If
Expand Down Expand Up @@ -329,7 +329,7 @@ cdef int64_t tz_convert_utc_to_tzlocal(int64_t utc_val, tzinfo tz, bint* fold=NU
return _tz_convert_tzlocal_utc(utc_val, tz, to_utc=False, fold=fold)


cpdef int64_t tz_convert_single(int64_t val, object tz1, object tz2):
cpdef int64_t tz_convert_single(int64_t val, tzinfo tz1, tzinfo tz2):
"""
Convert the val (in i8) from timezone1 to timezone2

Expand All @@ -338,18 +338,15 @@ cpdef int64_t tz_convert_single(int64_t val, object tz1, object tz2):
Parameters
----------
val : int64
tz1 : string / timezone object
tz2 : string / timezone object
tz1 : tzinfo
tz2 : tzinfo

Returns
-------
converted: int64
"""
cdef:
int64_t[:] deltas
Py_ssize_t pos
int64_t v, offset, utc_date
npy_datetimestruct dts
int64_t utc_date
int64_t arr[1]

# See GH#17734 We should always be converting either from UTC or to UTC
Expand Down Expand Up @@ -381,17 +378,15 @@ cpdef int64_t tz_convert_single(int64_t val, object tz1, object tz2):
return _tz_convert_dst(arr, tz2, to_utc=False)[0]


@cython.boundscheck(False)
@cython.wraparound(False)
def tz_convert(int64_t[:] vals, object tz1, object tz2):
def tz_convert(int64_t[:] vals, tzinfo tz1, tzinfo tz2):
"""
Convert the values (in i8) from timezone1 to timezone2

Parameters
----------
vals : int64 ndarray
tz1 : string / timezone object
tz2 : string / timezone object
tz1 : tzinfo
tz2 : tzinfo

Returns
-------
Expand All @@ -411,15 +406,15 @@ def tz_convert(int64_t[:] vals, object tz1, object tz2):

@cython.boundscheck(False)
@cython.wraparound(False)
cdef int64_t[:] _tz_convert_one_way(int64_t[:] vals, object tz, bint to_utc):
cdef int64_t[:] _tz_convert_one_way(int64_t[:] vals, tzinfo tz, bint to_utc):
"""
Convert the given values (in i8) either to UTC or from UTC.

Parameters
----------
vals : int64 ndarray
tz1 : string / timezone object
to_utc : bint
tz1 : tzinfo
to_utc : bool

Returns
-------
Expand All @@ -430,7 +425,7 @@ cdef int64_t[:] _tz_convert_one_way(int64_t[:] vals, object tz, bint to_utc):
Py_ssize_t i, n = len(vals)
int64_t val

if not is_utc(get_timezone(tz)):
if not is_utc(tz):
converted = np.empty(n, dtype=np.int64)
if is_tzlocal(tz):
for i in range(n):
Expand Down