Skip to content

CLN: Use C-API for datetime.date #33222

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
Apr 2, 2020
Merged
Changes from 1 commit
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
21 changes: 11 additions & 10 deletions pandas/_libs/tslibs/strptime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import time
import locale
import calendar
import re
import datetime
Copy link
Member

Choose a reason for hiding this comment

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

I think can just cimport cpython.datetime as datetime to minimize changes no? Didn't we move away from the import pattern here already in #32459

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch!

Somehow slipped away from me


from _thread import allocate_lock as _thread_allocate_lock

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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


Expand Down