From f4e64ec450a1427d851c77ec19d891cf57abdc1f Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 31 Mar 2020 20:45:54 +0300 Subject: [PATCH 1/2] CLN: Use C-API for datetime.date --- pandas/_libs/tslibs/strptime.pyx | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/pandas/_libs/tslibs/strptime.pyx b/pandas/_libs/tslibs/strptime.pyx index a48c3365947dc..275ccc467f837 100644 --- a/pandas/_libs/tslibs/strptime.pyx +++ b/pandas/_libs/tslibs/strptime.pyx @@ -4,7 +4,6 @@ import time import locale import calendar import re -import datetime from _thread import allocate_lock as _thread_allocate_lock @@ -13,6 +12,8 @@ import pytz import numpy as np from numpy cimport int64_t +from cpython.datetime cimport date + from pandas._libs.tslibs.np_datetime cimport ( check_dts_bounds, dtstruct_to_dt64, npy_datetimestruct) @@ -295,13 +296,13 @@ def array_strptime(object[:] values, object fmt, bint exact=True, errors='raise' if julian == -1: # Need to add 1 to result since first day of the year is 1, not # 0. - ordinal = datetime.date(year, month, day).toordinal() - julian = ordinal - datetime.date(year, 1, 1).toordinal() + 1 + ordinal = date(year, month, day).toordinal() + julian = ordinal - date(year, 1, 1).toordinal() + 1 else: # Assume that if they bothered to include Julian day it will # be accurate. - datetime_result = datetime.date.fromordinal( - (julian - 1) + datetime.date(year, 1, 1).toordinal()) + datetime_result = date.fromordinal( + (julian - 1) + date(year, 1, 1).toordinal()) year = datetime_result.year month = datetime_result.month day = datetime_result.day @@ -311,7 +312,7 @@ def array_strptime(object[:] values, object fmt, bint exact=True, errors='raise' continue raise if weekday == -1: - weekday = datetime.date(year, month, day).weekday() + weekday = date(year, month, day).weekday() dts.year = year dts.month = month @@ -649,7 +650,7 @@ cdef int _calc_julian_from_U_or_W(int year, int week_of_year, cdef: int first_weekday, week_0_length, days_to_week - first_weekday = datetime.date(year, 1, 1).weekday() + first_weekday = date(year, 1, 1).weekday() # If we are dealing with the %U directive (week starts on Sunday), it's # easier to just shift the view to Sunday being the first day of the # week. @@ -692,14 +693,14 @@ cdef (int, int) _calc_julian_from_V(int iso_year, int iso_week, int iso_weekday) cdef: int correction, ordinal - correction = datetime.date(iso_year, 1, 4).isoweekday() + 3 + correction = date(iso_year, 1, 4).isoweekday() + 3 ordinal = (iso_week * 7) + iso_weekday - correction # ordinal may be negative or 0 now, which means the date is in the previous # calendar year if ordinal < 1: - ordinal += datetime.date(iso_year, 1, 1).toordinal() + ordinal += date(iso_year, 1, 1).toordinal() iso_year -= 1 - ordinal -= datetime.date(iso_year, 1, 1).toordinal() + ordinal -= date(iso_year, 1, 1).toordinal() return iso_year, ordinal From ba0562691b7797ec557dc828b564f2dff3a1e088 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Thu, 2 Apr 2020 01:32:14 +0300 Subject: [PATCH 2/2] Cimporting cpython.datetime as datetime XREF: https://github.com/pandas-dev/pandas/pull/33222#discussion_r401941065 --- pandas/_libs/tslibs/strptime.pyx | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/pandas/_libs/tslibs/strptime.pyx b/pandas/_libs/tslibs/strptime.pyx index 275ccc467f837..ce4d3a4ef8e02 100644 --- a/pandas/_libs/tslibs/strptime.pyx +++ b/pandas/_libs/tslibs/strptime.pyx @@ -12,8 +12,7 @@ import pytz import numpy as np from numpy cimport int64_t -from cpython.datetime cimport date - +cimport cpython.datetime as datetime from pandas._libs.tslibs.np_datetime cimport ( check_dts_bounds, dtstruct_to_dt64, npy_datetimestruct) @@ -296,13 +295,13 @@ def array_strptime(object[:] values, object fmt, bint exact=True, errors='raise' if julian == -1: # Need to add 1 to result since first day of the year is 1, not # 0. - ordinal = date(year, month, day).toordinal() - julian = ordinal - date(year, 1, 1).toordinal() + 1 + ordinal = datetime.date(year, month, day).toordinal() + julian = ordinal - datetime.date(year, 1, 1).toordinal() + 1 else: # Assume that if they bothered to include Julian day it will # be accurate. - datetime_result = date.fromordinal( - (julian - 1) + date(year, 1, 1).toordinal()) + datetime_result = datetime.date.fromordinal( + (julian - 1) + datetime.date(year, 1, 1).toordinal()) year = datetime_result.year month = datetime_result.month day = datetime_result.day @@ -312,7 +311,7 @@ def array_strptime(object[:] values, object fmt, bint exact=True, errors='raise' continue raise if weekday == -1: - weekday = date(year, month, day).weekday() + weekday = datetime.date(year, month, day).weekday() dts.year = year dts.month = month @@ -650,7 +649,7 @@ cdef int _calc_julian_from_U_or_W(int year, int week_of_year, cdef: int first_weekday, week_0_length, days_to_week - first_weekday = date(year, 1, 1).weekday() + first_weekday = datetime.date(year, 1, 1).weekday() # If we are dealing with the %U directive (week starts on Sunday), it's # easier to just shift the view to Sunday being the first day of the # week. @@ -693,14 +692,14 @@ cdef (int, int) _calc_julian_from_V(int iso_year, int iso_week, int iso_weekday) cdef: int correction, ordinal - correction = date(iso_year, 1, 4).isoweekday() + 3 + correction = datetime.date(iso_year, 1, 4).isoweekday() + 3 ordinal = (iso_week * 7) + iso_weekday - correction # ordinal may be negative or 0 now, which means the date is in the previous # calendar year if ordinal < 1: - ordinal += date(iso_year, 1, 1).toordinal() + ordinal += datetime.date(iso_year, 1, 1).toordinal() iso_year -= 1 - ordinal -= date(iso_year, 1, 1).toordinal() + ordinal -= datetime.date(iso_year, 1, 1).toordinal() return iso_year, ordinal