Skip to content

PERF: tz_convert/tz_convert_single #35087

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 3 commits into from
Jul 2, 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
48 changes: 24 additions & 24 deletions pandas/_libs/tslibs/tzconversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -345,36 +345,28 @@ cpdef int64_t tz_convert_single(int64_t val, tzinfo tz1, tzinfo tz2):
converted: int64
"""
cdef:
int64_t utc_date
int64_t arr[1]
bint to_utc = is_utc(tz2)
tzinfo tz

# See GH#17734 We should always be converting either from UTC or to UTC
assert is_utc(tz1) or is_utc(tz2)
assert is_utc(tz1) or to_utc

if val == NPY_NAT:
return val

# Convert to UTC
if is_tzlocal(tz1):
utc_date = _tz_convert_tzlocal_utc(val, tz1, to_utc=True)
elif not is_utc(tz1):
arr[0] = val
utc_date = _tz_convert_dst(arr, tz1, to_utc=True)[0]
if to_utc:
tz = tz1
else:
utc_date = val
tz = tz2

if is_utc(tz2):
return utc_date
elif is_tzlocal(tz2):
return _tz_convert_tzlocal_utc(utc_date, tz2, to_utc=False)
if is_utc(tz):
return val
elif is_tzlocal(tz):
return _tz_convert_tzlocal_utc(val, tz, to_utc=to_utc)
else:
# Convert UTC to other timezone
arr[0] = utc_date
# Note: at least with cython 0.28.3, doing a lookup `[0]` in the next
# line is sensitive to the declared return type of _tz_convert_dst;
# if it is declared as returning ndarray[int64_t], a compile-time error
# is raised.
return _tz_convert_dst(arr, tz2, to_utc=False)[0]
arr[0] = val
return _tz_convert_dst(arr, tz, to_utc=to_utc)[0]


def tz_convert(int64_t[:] vals, tzinfo tz1, tzinfo tz2):
Expand All @@ -392,14 +384,22 @@ def tz_convert(int64_t[:] vals, tzinfo tz1, tzinfo tz2):
int64 ndarray of converted
"""
cdef:
int64_t[:] utc_dates, converted
int64_t[:] converted
bint to_utc = is_utc(tz2)
tzinfo tz

# See GH#17734 We should always be converting either from UTC or to UTC
assert is_utc(tz1) or to_utc

if len(vals) == 0:
return np.array([], dtype=np.int64)

# Convert to UTC
utc_dates = _tz_convert_one_way(vals, tz1, to_utc=True)
converted = _tz_convert_one_way(utc_dates, tz2, to_utc=False)
if to_utc:
tz = tz1
else:
tz = tz2

converted = _tz_convert_one_way(vals, tz, to_utc=to_utc)
return np.array(converted, dtype=np.int64)


Expand Down
7 changes: 4 additions & 3 deletions pandas/tests/tslibs/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ def test_tz_convert_single_matches_tz_convert(tz_aware_fixture, freq):
],
)
def test_tz_convert_corner(arr):
result = tzconversion.tz_convert(
arr, timezones.maybe_get_tz("US/Eastern"), timezones.maybe_get_tz("Asia/Tokyo")
)
Copy link
Member

Choose a reason for hiding this comment

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

is this test no longer relevant?

Copy link
Member Author

Choose a reason for hiding this comment

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

this PR requires that one of the two args be UTC

Copy link
Member

Choose a reason for hiding this comment

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

hmm, that's what tz_convert_single was doing before? so what's the difference now between tz_convert and tz_convert_single? and is converting between 2 tzs no longer needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

so what's the difference now between tz_convert and tz_convert_single?

One handles a scalar (i.e. Timestamp) the other handles an array. I'm trying to de-duplicate these further, havent found a way to do so without a perf hit.

and is converting between 2 tzs no longer needed?

correct.

result = tzconversion.tz_convert(arr, timezones.maybe_get_tz("US/Eastern"), UTC)
tm.assert_numpy_array_equal(result, arr)

result = tzconversion.tz_convert(arr, UTC, timezones.maybe_get_tz("Asia/Tokyo"))
tm.assert_numpy_array_equal(result, arr)


Expand Down