Skip to content

Commit afec0e9

Browse files
authored
clean import/comments (#46434)
1 parent 567fb55 commit afec0e9

File tree

4 files changed

+127
-112
lines changed

4 files changed

+127
-112
lines changed

pandas/_libs/tslibs/conversion.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ from pandas._libs.tslibs.nattype cimport (
7171
)
7272
from pandas._libs.tslibs.tzconversion cimport (
7373
bisect_right_i8,
74-
tz_convert_utc_to_tzlocal,
74+
localize_tzinfo_api,
7575
tz_localize_to_utc_single,
7676
)
7777

@@ -556,7 +556,7 @@ cdef _TSObject _create_tsobject_tz_using_offset(npy_datetimestruct dts,
556556
if is_utc(tz):
557557
pass
558558
elif is_tzlocal(tz):
559-
tz_convert_utc_to_tzlocal(obj.value, tz, &obj.fold)
559+
localize_tzinfo_api(obj.value, tz, &obj.fold)
560560
else:
561561
trans, deltas, typ = get_dst_info(tz)
562562

@@ -725,7 +725,7 @@ cdef inline void _localize_tso(_TSObject obj, tzinfo tz):
725725
elif obj.value == NPY_NAT:
726726
pass
727727
elif is_tzlocal(tz):
728-
local_val = tz_convert_utc_to_tzlocal(obj.value, tz, &obj.fold)
728+
local_val = obj.value + localize_tzinfo_api(obj.value, tz, &obj.fold)
729729
dt64_to_dtstruct(local_val, &obj.dts)
730730
else:
731731
# Adjust datetime64 timestamp, recompute datetimestruct

pandas/_libs/tslibs/tzconversion.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ from cpython.datetime cimport tzinfo
22
from numpy cimport int64_t
33

44

5-
cdef int64_t tz_convert_utc_to_tzlocal(
5+
cdef int64_t localize_tzinfo_api(
66
int64_t utc_val, tzinfo tz, bint* fold=*
77
) except? -1
88
cpdef int64_t tz_convert_from_utc_single(int64_t val, tzinfo tz)

pandas/_libs/tslibs/tzconversion.pyx

+59-53
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ from cython import Py_ssize_t
77
from cpython.datetime cimport (
88
PyDelta_Check,
99
datetime,
10+
datetime_new,
1011
import_datetime,
1112
timedelta,
1213
tzinfo,
@@ -43,6 +44,7 @@ from pandas._libs.tslibs.timezones cimport (
4344
is_fixed_offset,
4445
is_tzlocal,
4546
is_utc,
47+
utc_pytz,
4648
)
4749

4850

@@ -61,7 +63,7 @@ cdef int64_t tz_localize_to_utc_single(
6163
return val
6264

6365
elif is_tzlocal(tz):
64-
return _tz_convert_tzlocal_utc(val, tz, to_utc=True)
66+
return val - _tz_localize_using_tzinfo_api(val, tz, to_utc=True)
6567

6668
elif is_fixed_offset(tz):
6769
# TODO: in this case we should be able to use get_utcoffset,
@@ -142,7 +144,7 @@ timedelta-like}
142144
if v == NPY_NAT:
143145
result[i] = NPY_NAT
144146
else:
145-
result[i] = _tz_convert_tzlocal_utc(v, tz, to_utc=True)
147+
result[i] = v - _tz_localize_using_tzinfo_api(v, tz, to_utc=True)
146148
return result
147149

148150
# silence false-positive compiler warning
@@ -402,7 +404,7 @@ cdef ndarray[int64_t] _get_dst_hours(
402404
# ----------------------------------------------------------------------
403405
# Timezone Conversion
404406

405-
cdef int64_t tz_convert_utc_to_tzlocal(
407+
cdef int64_t localize_tzinfo_api(
406408
int64_t utc_val, tzinfo tz, bint* fold=NULL
407409
) except? -1:
408410
"""
@@ -416,20 +418,21 @@ cdef int64_t tz_convert_utc_to_tzlocal(
416418
417419
Returns
418420
-------
419-
local_val : int64_t
421+
delta : int64_t
422+
Value to add when converting from utc.
420423
"""
421-
return _tz_convert_tzlocal_utc(utc_val, tz, to_utc=False, fold=fold)
424+
return _tz_localize_using_tzinfo_api(utc_val, tz, to_utc=False, fold=fold)
422425

423426

424-
cpdef int64_t tz_convert_from_utc_single(int64_t val, tzinfo tz):
427+
cpdef int64_t tz_convert_from_utc_single(int64_t utc_val, tzinfo tz):
425428
"""
426429
Convert the val (in i8) from UTC to tz
427430
428431
This is a single value version of tz_convert_from_utc.
429432
430433
Parameters
431434
----------
432-
val : int64
435+
utc_val : int64
433436
tz : tzinfo
434437
435438
Returns
@@ -443,22 +446,22 @@ cpdef int64_t tz_convert_from_utc_single(int64_t val, tzinfo tz):
443446
int64_t* tdata
444447
intp_t pos
445448

446-
if val == NPY_NAT:
447-
return val
449+
if utc_val == NPY_NAT:
450+
return utc_val
448451

449452
if is_utc(tz):
450-
return val
453+
return utc_val
451454
elif is_tzlocal(tz):
452-
return _tz_convert_tzlocal_utc(val, tz, to_utc=False)
455+
return utc_val + _tz_localize_using_tzinfo_api(utc_val, tz, to_utc=False)
453456
elif is_fixed_offset(tz):
454457
_, deltas, _ = get_dst_info(tz)
455458
delta = deltas[0]
456-
return val + delta
459+
return utc_val + delta
457460
else:
458461
trans, deltas, _ = get_dst_info(tz)
459462
tdata = <int64_t*>cnp.PyArray_DATA(trans)
460-
pos = bisect_right_i8(tdata, val, trans.shape[0]) - 1
461-
return val + deltas[pos]
463+
pos = bisect_right_i8(tdata, utc_val, trans.shape[0]) - 1
464+
return utc_val + deltas[pos]
462465

463466

464467
def tz_convert_from_utc(const int64_t[:] vals, tzinfo tz):
@@ -486,32 +489,34 @@ def tz_convert_from_utc(const int64_t[:] vals, tzinfo tz):
486489

487490
@cython.boundscheck(False)
488491
@cython.wraparound(False)
489-
cdef const int64_t[:] _tz_convert_from_utc(const int64_t[:] vals, tzinfo tz):
492+
cdef const int64_t[:] _tz_convert_from_utc(const int64_t[:] stamps, tzinfo tz):
490493
"""
491494
Convert the given values (in i8) either to UTC or from UTC.
492495
493496
Parameters
494497
----------
495-
vals : int64 ndarray
498+
stamps : int64 ndarray
496499
tz : tzinfo
497500
498501
Returns
499502
-------
500503
converted : ndarray[int64_t]
501504
"""
502505
cdef:
503-
int64_t[::1] converted, deltas
504-
Py_ssize_t i, ntrans = -1, n = vals.shape[0]
505-
int64_t val, delta = 0 # avoid not-initialized-warning
506-
intp_t pos
506+
Py_ssize_t i, ntrans = -1, n = stamps.shape[0]
507507
ndarray[int64_t] trans
508+
int64_t[::1] deltas
508509
int64_t* tdata = NULL
510+
intp_t pos
511+
int64_t utc_val, local_val, delta = NPY_NAT
512+
bint use_utc = False, use_tzlocal = False, use_fixed = False
509513
str typ
510-
bint use_tzlocal = False, use_fixed = False, use_utc = True
514+
515+
int64_t[::1] result
511516

512517
if is_utc(tz):
513518
# Much faster than going through the "standard" pattern below
514-
return vals.copy()
519+
return stamps.copy()
515520

516521
if is_utc(tz) or tz is None:
517522
use_utc = True
@@ -520,59 +525,62 @@ cdef const int64_t[:] _tz_convert_from_utc(const int64_t[:] vals, tzinfo tz):
520525
else:
521526
trans, deltas, typ = get_dst_info(tz)
522527
ntrans = trans.shape[0]
523-
524528
if typ not in ["pytz", "dateutil"]:
525-
# FixedOffset, we know len(deltas) == 1
526-
delta = deltas[0]
529+
# static/fixed; in this case we know that len(delta) == 1
527530
use_fixed = True
531+
delta = deltas[0]
528532
else:
529533
tdata = <int64_t*>cnp.PyArray_DATA(trans)
530534

531-
converted = np.empty(n, dtype=np.int64)
535+
result = np.empty(n, dtype=np.int64)
532536

533537
for i in range(n):
534-
val = vals[i]
535-
if val == NPY_NAT:
536-
converted[i] = NPY_NAT
538+
utc_val = stamps[i]
539+
if utc_val == NPY_NAT:
540+
result[i] = NPY_NAT
537541
continue
538542

539543
# The pattern used in vectorized.pyx checks for use_utc here,
540544
# but we handle that case above.
541545
if use_tzlocal:
542-
converted[i] = _tz_convert_tzlocal_utc(val, tz, to_utc=False)
546+
local_val = utc_val + _tz_localize_using_tzinfo_api(utc_val, tz, to_utc=False)
543547
elif use_fixed:
544-
converted[i] = val + delta
548+
local_val = utc_val + delta
545549
else:
546-
pos = bisect_right_i8(tdata, val, ntrans) - 1
547-
converted[i] = val + deltas[pos]
550+
pos = bisect_right_i8(tdata, utc_val, ntrans) - 1
551+
local_val = utc_val + deltas[pos]
552+
553+
result[i] = local_val
548554

549-
return converted
555+
return result
550556

551557

552558
# OSError may be thrown by tzlocal on windows at or close to 1970-01-01
553559
# see https://github.com/pandas-dev/pandas/pull/37591#issuecomment-720628241
554-
cdef int64_t _tz_convert_tzlocal_utc(int64_t val, tzinfo tz, bint to_utc=True,
555-
bint* fold=NULL) except? -1:
560+
cdef int64_t _tz_localize_using_tzinfo_api(
561+
int64_t val, tzinfo tz, bint to_utc=True, bint* fold=NULL
562+
) except? -1:
556563
"""
557-
Convert the i8 representation of a datetime from a tzlocal timezone to
558-
UTC, or vice-versa.
564+
Convert the i8 representation of a datetime from a general-cast timezone to
565+
UTC, or vice-versa using the datetime/tzinfo API.
559566
560-
Private, not intended for use outside of tslibs.conversion
567+
Private, not intended for use outside of tslibs.tzconversion.
561568
562569
Parameters
563570
----------
564571
val : int64_t
565572
tz : tzinfo
566573
to_utc : bint
567-
True if converting tzlocal _to_ UTC, False if going the other direction
574+
True if converting _to_ UTC, False if going the other direction.
568575
fold : bint*, default NULL
569576
pointer to fold: whether datetime ends up in a fold or not
570-
after adjustment
577+
after adjustment.
571578
Only passed with to_utc=False.
572579
573580
Returns
574581
-------
575-
result : int64_t
582+
delta : int64_t
583+
Value to add when converting from utc, subtract when converting to utc.
576584
577585
Notes
578586
-----
@@ -586,23 +594,21 @@ cdef int64_t _tz_convert_tzlocal_utc(int64_t val, tzinfo tz, bint to_utc=True,
586594

587595
dt64_to_dtstruct(val, &dts)
588596

589-
dt = datetime(dts.year, dts.month, dts.day, dts.hour,
590-
dts.min, dts.sec, dts.us)
591-
592-
# tz.utcoffset only makes sense if datetime
593-
# is _wall time_, so if val is a UTC timestamp convert to wall time
597+
# datetime_new is cython-optimized constructor
594598
if not to_utc:
595-
dt = dt.replace(tzinfo=tzutc())
599+
# tz.utcoffset only makes sense if datetime
600+
# is _wall time_, so if val is a UTC timestamp convert to wall time
601+
dt = datetime_new(dts.year, dts.month, dts.day, dts.hour,
602+
dts.min, dts.sec, dts.us, utc_pytz)
596603
dt = dt.astimezone(tz)
597604

598605
if fold is not NULL:
599606
# NB: fold is only passed with to_utc=False
600607
fold[0] = dt.fold
608+
else:
609+
dt = datetime_new(dts.year, dts.month, dts.day, dts.hour,
610+
dts.min, dts.sec, dts.us, None)
601611

602612
td = tz.utcoffset(dt)
603613
delta = int(td.total_seconds() * 1_000_000_000)
604-
605-
if to_utc:
606-
return val - delta
607-
else:
608-
return val + delta
614+
return delta

0 commit comments

Comments
 (0)