Skip to content

Commit 0cef605

Browse files
authored
REF: collect get_dst_info-using functions in tslibs.vectorized (#35168)
1 parent 42fd7e7 commit 0cef605

File tree

17 files changed

+504
-467
lines changed

17 files changed

+504
-467
lines changed

asv_bench/benchmarks/tslibs/resolution.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
import numpy as np
2424
import pytz
2525

26-
from pandas._libs.tslibs.resolution import get_resolution
26+
try:
27+
from pandas._libs.tslibs import get_resolution
28+
except ImportError:
29+
from pandas._libs.tslibs.resolution import get_resolution
2730

2831

2932
class TimeResolution:

asv_bench/benchmarks/tslibs/tslib.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
import numpy as np
2222
import pytz
2323

24-
from pandas._libs.tslib import ints_to_pydatetime
24+
try:
25+
from pandas._libs.tslibs import ints_to_pydatetime
26+
except ImportError:
27+
from pandas._libs.tslib import ints_to_pydatetime
2528

2629
_tzs = [
2730
None,

pandas/_libs/tslib.pyx

+2-168
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@ from cpython.datetime cimport (
44
PyDate_Check,
55
PyDateTime_Check,
66
PyDateTime_IMPORT,
7-
date,
87
datetime,
9-
time,
10-
timedelta,
11-
tzinfo,
128
)
139
# import datetime C API
1410
PyDateTime_IMPORT
1511

1612

1713
cimport numpy as cnp
18-
from numpy cimport float64_t, int64_t, ndarray, uint8_t, intp_t
14+
from numpy cimport float64_t, int64_t, ndarray
1915
import numpy as np
2016
cnp.import_array()
2117

@@ -42,11 +38,6 @@ from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
4238

4339
from pandas._libs.tslibs.parsing import parse_datetime_string
4440

45-
from pandas._libs.tslibs.timezones cimport (
46-
get_dst_info,
47-
is_utc,
48-
is_tzlocal,
49-
)
5041
from pandas._libs.tslibs.conversion cimport (
5142
_TSObject,
5243
cast_from_unit,
@@ -60,174 +51,17 @@ from pandas._libs.tslibs.nattype cimport (
6051
c_nat_strings as nat_strings,
6152
)
6253

63-
from pandas._libs.tslibs.offsets cimport to_offset
64-
65-
from pandas._libs.tslibs.timestamps cimport create_timestamp_from_ts, _Timestamp
54+
from pandas._libs.tslibs.timestamps cimport _Timestamp
6655
from pandas._libs.tslibs.timestamps import Timestamp
6756

6857
from pandas._libs.tslibs.tzconversion cimport (
69-
tz_convert_utc_to_tzlocal,
7058
tz_localize_to_utc_single,
7159
)
7260

7361
# Note: this is the only non-tslibs intra-pandas dependency here
7462
from pandas._libs.missing cimport checknull_with_nat_and_na
7563

7664

77-
cdef inline object create_datetime_from_ts(
78-
int64_t value,
79-
npy_datetimestruct dts,
80-
tzinfo tz,
81-
object freq,
82-
bint fold,
83-
):
84-
"""
85-
Convenience routine to construct a datetime.datetime from its parts.
86-
"""
87-
return datetime(
88-
dts.year, dts.month, dts.day, dts.hour, dts.min, dts.sec, dts.us, tz, fold=fold
89-
)
90-
91-
92-
cdef inline object create_date_from_ts(
93-
int64_t value,
94-
npy_datetimestruct dts,
95-
tzinfo tz,
96-
object freq,
97-
bint fold
98-
):
99-
"""
100-
Convenience routine to construct a datetime.date from its parts.
101-
"""
102-
# GH 25057 add fold argument to match other func_create signatures
103-
return date(dts.year, dts.month, dts.day)
104-
105-
106-
cdef inline object create_time_from_ts(
107-
int64_t value,
108-
npy_datetimestruct dts,
109-
tzinfo tz,
110-
object freq,
111-
bint fold
112-
):
113-
"""
114-
Convenience routine to construct a datetime.time from its parts.
115-
"""
116-
return time(dts.hour, dts.min, dts.sec, dts.us, tz, fold=fold)
117-
118-
119-
@cython.wraparound(False)
120-
@cython.boundscheck(False)
121-
def ints_to_pydatetime(
122-
const int64_t[:] arr,
123-
tzinfo tz=None,
124-
object freq=None,
125-
bint fold=False,
126-
str box="datetime"
127-
):
128-
"""
129-
Convert an i8 repr to an ndarray of datetimes, date, time or Timestamp.
130-
131-
Parameters
132-
----------
133-
arr : array of i8
134-
tz : str, optional
135-
convert to this timezone
136-
freq : str/Offset, optional
137-
freq to convert
138-
fold : bint, default is 0
139-
Due to daylight saving time, one wall clock time can occur twice
140-
when shifting from summer to winter time; fold describes whether the
141-
datetime-like corresponds to the first (0) or the second time (1)
142-
the wall clock hits the ambiguous time
143-
144-
.. versionadded:: 1.1.0
145-
box : {'datetime', 'timestamp', 'date', 'time'}, default 'datetime'
146-
* If datetime, convert to datetime.datetime
147-
* If date, convert to datetime.date
148-
* If time, convert to datetime.time
149-
* If Timestamp, convert to pandas.Timestamp
150-
151-
Returns
152-
-------
153-
ndarray of dtype specified by box
154-
"""
155-
cdef:
156-
Py_ssize_t i, n = len(arr)
157-
ndarray[int64_t] trans
158-
int64_t[:] deltas
159-
intp_t[:] pos
160-
npy_datetimestruct dts
161-
object dt, new_tz
162-
str typ
163-
int64_t value, local_value, delta = NPY_NAT # dummy for delta
164-
ndarray[object] result = np.empty(n, dtype=object)
165-
object (*func_create)(int64_t, npy_datetimestruct, tzinfo, object, bint)
166-
bint use_utc = False, use_tzlocal = False, use_fixed = False
167-
bint use_pytz = False
168-
169-
if box == "date":
170-
assert (tz is None), "tz should be None when converting to date"
171-
172-
func_create = create_date_from_ts
173-
elif box == "timestamp":
174-
func_create = create_timestamp_from_ts
175-
176-
if isinstance(freq, str):
177-
freq = to_offset(freq)
178-
elif box == "time":
179-
func_create = create_time_from_ts
180-
elif box == "datetime":
181-
func_create = create_datetime_from_ts
182-
else:
183-
raise ValueError(
184-
"box must be one of 'datetime', 'date', 'time' or 'timestamp'"
185-
)
186-
187-
if is_utc(tz) or tz is None:
188-
use_utc = True
189-
elif is_tzlocal(tz):
190-
use_tzlocal = True
191-
else:
192-
trans, deltas, typ = get_dst_info(tz)
193-
if typ not in ["pytz", "dateutil"]:
194-
# static/fixed; in this case we know that len(delta) == 1
195-
use_fixed = True
196-
delta = deltas[0]
197-
else:
198-
pos = trans.searchsorted(arr, side="right") - 1
199-
use_pytz = typ == "pytz"
200-
201-
for i in range(n):
202-
new_tz = tz
203-
value = arr[i]
204-
205-
if value == NPY_NAT:
206-
result[i] = <object>NaT
207-
else:
208-
if use_utc:
209-
local_value = value
210-
elif use_tzlocal:
211-
local_value = tz_convert_utc_to_tzlocal(value, tz)
212-
elif use_fixed:
213-
local_value = value + delta
214-
elif not use_pytz:
215-
# i.e. dateutil
216-
# no zone-name change for dateutil tzs - dst etc
217-
# represented in single object.
218-
local_value = value + deltas[pos[i]]
219-
else:
220-
# pytz
221-
# find right representation of dst etc in pytz timezone
222-
new_tz = tz._tzinfos[tz._transition_info[pos[i]]]
223-
local_value = value + deltas[pos[i]]
224-
225-
dt64_to_dtstruct(local_value, &dts)
226-
result[i] = func_create(value, dts, new_tz, freq, fold)
227-
228-
return result
229-
230-
23165
def _test_parse_iso8601(ts: str):
23266
"""
23367
TESTING ONLY: Parse string into Timestamp using iso8601 parser. Used

pandas/_libs/tslibs/__init__.py

+12
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111
"Period",
1212
"Resolution",
1313
"Timedelta",
14+
"normalize_i8_timestamps",
15+
"is_date_array_normalized",
16+
"dt64arr_to_periodarr",
1417
"delta_to_nanoseconds",
18+
"ints_to_pydatetime",
1519
"ints_to_pytimedelta",
20+
"get_resolution",
1621
"Timestamp",
1722
"tz_convert_single",
1823
"to_offset",
@@ -30,3 +35,10 @@
3035
from .timedeltas import Timedelta, delta_to_nanoseconds, ints_to_pytimedelta
3136
from .timestamps import Timestamp
3237
from .tzconversion import tz_convert_single
38+
from .vectorized import (
39+
dt64arr_to_periodarr,
40+
get_resolution,
41+
ints_to_pydatetime,
42+
is_date_array_normalized,
43+
normalize_i8_timestamps,
44+
)

pandas/_libs/tslibs/conversion.pxd

-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@ cdef int64_t get_datetime64_nanos(object val) except? -1
2525
cpdef datetime localize_pydatetime(datetime dt, object tz)
2626
cdef int64_t cast_from_unit(object ts, str unit) except? -1
2727

28-
cpdef ndarray[int64_t] normalize_i8_timestamps(const int64_t[:] stamps, tzinfo tz)
2928
cdef int64_t normalize_i8_stamp(int64_t local_val) nogil

0 commit comments

Comments
 (0)