Skip to content

Follow-up to #17805 #18023

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 1 commit into from
Oct 29, 2017
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
43 changes: 16 additions & 27 deletions pandas/_libs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ from pandas.compat import PY2

cimport cython

from datetime cimport (
is_leapyear,
pandas_datetimestruct,
pandas_datetimestruct_to_datetime,
pandas_datetime_to_datetimestruct,
PANDAS_FR_ns)
from tslibs.np_datetime cimport (pandas_datetimestruct,
dtstruct_to_dt64, dt64_to_dtstruct)
from datetime cimport is_leapyear


cimport util
Expand Down Expand Up @@ -137,7 +134,7 @@ def dt64arr_to_periodarr(ndarray[int64_t] dtarr, int freq, tz=None):
if dtarr[i] == NPY_NAT:
out[i] = NPY_NAT
continue
pandas_datetime_to_datetimestruct(dtarr[i], PANDAS_FR_ns, &dts)
dt64_to_dtstruct(dtarr[i], &dts)
out[i] = get_period_ordinal(dts.year, dts.month, dts.day,
dts.hour, dts.min, dts.sec,
dts.us, dts.ps, freq)
Expand Down Expand Up @@ -268,7 +265,7 @@ cpdef int64_t period_ordinal_to_dt64(int64_t ordinal, int freq) nogil:
dts.us = int((subsecond_fraction) * 1e6)
dts.ps = int(((subsecond_fraction) * 1e6 - dts.us) * 1e6)

return pandas_datetimestruct_to_datetime(PANDAS_FR_ns, &dts)
return dtstruct_to_dt64(&dts)


def period_format(int64_t value, int freq, object fmt=None):
Expand Down Expand Up @@ -499,7 +496,7 @@ cpdef resolution(ndarray[int64_t] stamps, tz=None):
for i in range(n):
if stamps[i] == NPY_NAT:
continue
pandas_datetime_to_datetimestruct(stamps[i], PANDAS_FR_ns, &dts)
dt64_to_dtstruct(stamps[i], &dts)
curr_reso = _reso_stamp(&dts)
if curr_reso < reso:
reso = curr_reso
Expand Down Expand Up @@ -530,21 +527,19 @@ cdef _reso_local(ndarray[int64_t] stamps, object tz):
for i in range(n):
if stamps[i] == NPY_NAT:
continue
pandas_datetime_to_datetimestruct(stamps[i], PANDAS_FR_ns, &dts)
dt64_to_dtstruct(stamps[i], &dts)
curr_reso = _reso_stamp(&dts)
if curr_reso < reso:
reso = curr_reso
elif is_tzlocal(tz):
for i in range(n):
if stamps[i] == NPY_NAT:
continue
pandas_datetime_to_datetimestruct(stamps[i], PANDAS_FR_ns,
&dts)
dt64_to_dtstruct(stamps[i], &dts)
dt = datetime(dts.year, dts.month, dts.day, dts.hour,
dts.min, dts.sec, dts.us, tz)
delta = int(get_utcoffset(tz, dt).total_seconds()) * 1000000000
pandas_datetime_to_datetimestruct(stamps[i] + delta,
PANDAS_FR_ns, &dts)
dt64_to_dtstruct(stamps[i] + delta, &dts)
curr_reso = _reso_stamp(&dts)
if curr_reso < reso:
reso = curr_reso
Expand All @@ -562,17 +557,15 @@ cdef _reso_local(ndarray[int64_t] stamps, object tz):
for i in range(n):
if stamps[i] == NPY_NAT:
continue
pandas_datetime_to_datetimestruct(stamps[i] + deltas[0],
PANDAS_FR_ns, &dts)
dt64_to_dtstruct(stamps[i] + deltas[0], &dts)
curr_reso = _reso_stamp(&dts)
if curr_reso < reso:
reso = curr_reso
else:
for i in range(n):
if stamps[i] == NPY_NAT:
continue
pandas_datetime_to_datetimestruct(stamps[i] + deltas[pos[i]],
PANDAS_FR_ns, &dts)
dt64_to_dtstruct(stamps[i] + deltas[pos[i]], &dts)
curr_reso = _reso_stamp(&dts)
if curr_reso < reso:
reso = curr_reso
Expand All @@ -595,7 +588,7 @@ cdef ndarray[int64_t] localize_dt64arr_to_period(ndarray[int64_t] stamps,
if stamps[i] == NPY_NAT:
result[i] = NPY_NAT
continue
pandas_datetime_to_datetimestruct(stamps[i], PANDAS_FR_ns, &dts)
dt64_to_dtstruct(stamps[i], &dts)
result[i] = get_period_ordinal(dts.year, dts.month, dts.day,
dts.hour, dts.min, dts.sec,
dts.us, dts.ps, freq)
Expand All @@ -605,13 +598,11 @@ cdef ndarray[int64_t] localize_dt64arr_to_period(ndarray[int64_t] stamps,
if stamps[i] == NPY_NAT:
result[i] = NPY_NAT
continue
pandas_datetime_to_datetimestruct(stamps[i], PANDAS_FR_ns,
&dts)
dt64_to_dtstruct(stamps[i], &dts)
dt = datetime(dts.year, dts.month, dts.day, dts.hour,
dts.min, dts.sec, dts.us, tz)
delta = int(get_utcoffset(tz, dt).total_seconds()) * 1000000000
pandas_datetime_to_datetimestruct(stamps[i] + delta,
PANDAS_FR_ns, &dts)
dt64_to_dtstruct(stamps[i] + delta, &dts)
result[i] = get_period_ordinal(dts.year, dts.month, dts.day,
dts.hour, dts.min, dts.sec,
dts.us, dts.ps, freq)
Expand All @@ -630,8 +621,7 @@ cdef ndarray[int64_t] localize_dt64arr_to_period(ndarray[int64_t] stamps,
if stamps[i] == NPY_NAT:
result[i] = NPY_NAT
continue
pandas_datetime_to_datetimestruct(stamps[i] + deltas[0],
PANDAS_FR_ns, &dts)
dt64_to_dtstruct(stamps[i] + deltas[0], &dts)
result[i] = get_period_ordinal(dts.year, dts.month, dts.day,
dts.hour, dts.min, dts.sec,
dts.us, dts.ps, freq)
Expand All @@ -640,8 +630,7 @@ cdef ndarray[int64_t] localize_dt64arr_to_period(ndarray[int64_t] stamps,
if stamps[i] == NPY_NAT:
result[i] = NPY_NAT
continue
pandas_datetime_to_datetimestruct(stamps[i] + deltas[pos[i]],
PANDAS_FR_ns, &dts)
dt64_to_dtstruct(stamps[i] + deltas[pos[i]], &dts)
result[i] = get_period_ordinal(dts.year, dts.month, dts.day,
dts.hour, dts.min, dts.sec,
dts.us, dts.ps, freq)
Expand Down
42 changes: 18 additions & 24 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ cimport util
from cpython.datetime cimport PyDelta_Check, PyTZInfo_Check
# this is our datetime.pxd
from datetime cimport (
pandas_datetimestruct,
pandas_datetime_to_datetimestruct,
pandas_datetimestruct_to_datetime,
days_per_month_table,
Expand All @@ -50,14 +49,15 @@ from datetime cimport (
PANDAS_FR_ns,
PyDateTime_Check, PyDate_Check,
PyDateTime_IMPORT,
timedelta, datetime
)
timedelta, datetime)

# stdlib datetime imports
from datetime import timedelta, datetime
from datetime import time as datetime_time

from tslibs.np_datetime cimport check_dts_bounds
from tslibs.np_datetime cimport (check_dts_bounds,
pandas_datetimestruct,
dt64_to_dtstruct, dtstruct_to_dt64)
from tslibs.np_datetime import OutOfBoundsDatetime

from khash cimport (
Expand Down Expand Up @@ -149,17 +149,15 @@ def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None, box=False):
if value == NPY_NAT:
result[i] = NaT
else:
pandas_datetime_to_datetimestruct(
value, PANDAS_FR_ns, &dts)
dt64_to_dtstruct(value, &dts)
result[i] = func_create(value, dts, tz, freq)
elif is_tzlocal(tz) or is_fixed_offset(tz):
for i in range(n):
value = arr[i]
if value == NPY_NAT:
result[i] = NaT
else:
pandas_datetime_to_datetimestruct(
value, PANDAS_FR_ns, &dts)
dt64_to_dtstruct(value, &dts)
dt = create_datetime_from_ts(value, dts, tz, freq)
dt = dt + tz.utcoffset(dt)
if box:
Expand All @@ -185,8 +183,7 @@ def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None, box=False):
# represented in single object.
new_tz = tz

pandas_datetime_to_datetimestruct(
value + deltas[pos], PANDAS_FR_ns, &dts)
dt64_to_dtstruct(value + deltas[pos], &dts)
result[i] = func_create(value, dts, new_tz, freq)
else:
for i in range(n):
Expand All @@ -195,7 +192,7 @@ def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None, box=False):
if value == NPY_NAT:
result[i] = NaT
else:
pandas_datetime_to_datetimestruct(value, PANDAS_FR_ns, &dts)
dt64_to_dtstruct(value, &dts)
result[i] = func_create(value, dts, None, freq)

return result
Expand Down Expand Up @@ -698,7 +695,7 @@ class Timestamp(_Timestamp):
value += value - value_tz

# setup components
pandas_datetime_to_datetimestruct(value, PANDAS_FR_ns, &dts)
dt64_to_dtstruct(value, &dts)
dts.ps = self.nanosecond * 1000

# replace
Expand Down Expand Up @@ -1811,7 +1808,7 @@ def _test_parse_iso8601(object ts):
obj = _TSObject()

_string_to_dts(ts, &obj.dts, &out_local, &out_tzoffset)
obj.value = pandas_datetimestruct_to_datetime(PANDAS_FR_ns, &obj.dts)
obj.value = dtstruct_to_dt64(&obj.dts)
check_dts_bounds(&obj.dts)
if out_local == 1:
obj.tzinfo = pytz.FixedOffset(out_tzoffset)
Expand Down Expand Up @@ -1984,7 +1981,7 @@ def format_array_from_datetime(ndarray[int64_t] values, object tz=None,
result[i] = na_rep
elif basic_format:

pandas_datetime_to_datetimestruct(val, PANDAS_FR_ns, &dts)
dt64_to_dtstruct(val, &dts)
res = '%d-%.2d-%.2d %.2d:%.2d:%.2d' % (dts.year,
dts.month,
dts.day,
Expand Down Expand Up @@ -3792,7 +3789,7 @@ cdef inline int64_t _normalized_stamp(pandas_datetimestruct *dts) nogil:
dts.sec = 0
dts.us = 0
dts.ps = 0
return pandas_datetimestruct_to_datetime(PANDAS_FR_ns, dts)
return dtstruct_to_dt64(dts)


def dates_normalized(ndarray[int64_t] stamps, tz=None):
Expand Down Expand Up @@ -3910,13 +3907,12 @@ def shift_months(int64_t[:] dtindex, int months, object day=None):
out[i] = NPY_NAT
continue

pandas_datetime_to_datetimestruct(dtindex[i],
PANDAS_FR_ns, &dts)
dt64_to_dtstruct(dtindex[i], &dts)
dts.year = _year_add_months(dts, months)
dts.month = _month_add_months(dts, months)

dts.day = min(dts.day, days_in_month(dts))
out[i] = pandas_datetimestruct_to_datetime(PANDAS_FR_ns, &dts)
out[i] = dtstruct_to_dt64(&dts)
elif day == 'start':
roll_check = False
if months <= 0:
Expand All @@ -3928,8 +3924,7 @@ def shift_months(int64_t[:] dtindex, int months, object day=None):
out[i] = NPY_NAT
continue

pandas_datetime_to_datetimestruct(dtindex[i],
PANDAS_FR_ns, &dts)
dt64_to_dtstruct(dtindex[i], &dts)
months_to_roll = months

# offset semantics - if on the anchor point and going backwards
Expand All @@ -3941,7 +3936,7 @@ def shift_months(int64_t[:] dtindex, int months, object day=None):
dts.month = _month_add_months(dts, months_to_roll)
dts.day = 1

out[i] = pandas_datetimestruct_to_datetime(PANDAS_FR_ns, &dts)
out[i] = dtstruct_to_dt64(&dts)
elif day == 'end':
roll_check = False
if months > 0:
Expand All @@ -3953,8 +3948,7 @@ def shift_months(int64_t[:] dtindex, int months, object day=None):
out[i] = NPY_NAT
continue

pandas_datetime_to_datetimestruct(dtindex[i],
PANDAS_FR_ns, &dts)
dt64_to_dtstruct(dtindex[i], &dts)
months_to_roll = months

# similar semantics - when adding shift forward by one
Expand All @@ -3966,7 +3960,7 @@ def shift_months(int64_t[:] dtindex, int months, object day=None):
dts.month = _month_add_months(dts, months_to_roll)

dts.day = days_in_month(dts)
out[i] = pandas_datetimestruct_to_datetime(PANDAS_FR_ns, &dts)
out[i] = dtstruct_to_dt64(&dts)
else:
raise ValueError("day must be None, 'start' or 'end'")

Expand Down
Loading