-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
move and de-privatize _localize_pydatetime, move shift_day to liboffsets #21691
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
Changes from 1 commit
170ee93
226ad30
8e669ea
12c85a3
3e81f57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,13 @@ cimport cython | |
from cython cimport Py_ssize_t | ||
|
||
import time | ||
from cpython.datetime cimport datetime, timedelta, time as dt_time | ||
from cpython.datetime cimport (PyDateTime_IMPORT, PyDateTime_CheckExact, | ||
datetime, timedelta, | ||
time as dt_time) | ||
PyDateTime_IMPORT | ||
|
||
from dateutil.relativedelta import relativedelta | ||
from pytz import UTC | ||
|
||
import numpy as np | ||
cimport numpy as cnp | ||
|
@@ -494,6 +498,58 @@ class BaseOffset(_BaseOffset): | |
# ---------------------------------------------------------------------- | ||
# RelativeDelta Arithmetic | ||
|
||
cpdef inline datetime localize_pydatetime(datetime dt, object tz): | ||
""" | ||
Take a datetime/Timestamp in UTC and localizes to timezone tz. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be in timezones.pyx? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to keeping There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, let's do that |
||
|
||
Parameters | ||
---------- | ||
dt : datetime or Timestamp | ||
tz : tzinfo, "UTC", or None | ||
|
||
Returns | ||
------- | ||
localized : datetime or Timestamp | ||
""" | ||
if tz is None: | ||
return dt | ||
elif not PyDateTime_CheckExact(dt): | ||
# i.e. is a Timestamp | ||
return dt.tz_localize(tz) | ||
elif tz == 'UTC' or tz is UTC: | ||
return UTC.localize(dt) | ||
try: | ||
# datetime.replace with pytz may be incorrect result | ||
return tz.localize(dt) | ||
except AttributeError: | ||
return dt.replace(tzinfo=tz) | ||
|
||
|
||
cpdef datetime shift_day(datetime other, int days): | ||
""" | ||
Increment the datetime `other` by the given number of days, retaining | ||
the time-portion of the datetime. For tz-naive datetimes this is | ||
equivalent to adding a timedelta. For tz-aware datetimes it is similar to | ||
dateutil's relativedelta.__add__, but handles pytz tzinfo objects. | ||
|
||
Parameters | ||
---------- | ||
other : datetime or Timestamp | ||
days : int | ||
|
||
Returns | ||
------- | ||
shifted: datetime or Timestamp | ||
""" | ||
if other.tzinfo is None: | ||
return other + timedelta(days=days) | ||
|
||
tz = other.tzinfo | ||
naive = other.replace(tzinfo=None) | ||
shifted = naive + timedelta(days=days) | ||
return localize_pydatetime(shifted, tz) | ||
|
||
|
||
cdef inline int year_add_months(pandas_datetimestruct dts, int months) nogil: | ||
"""new year number after shifting pandas_datetimestruct number of months""" | ||
return dts.year + (dts.month + months - 1) / 12 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be ok with not doing this import and changing the tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After the current set of PRs I'm going to pitch an idea to 1) finish off
tslib
and 2) expose selected functions/classes intslibs.__init__
. I'd rather avoid futzing with imports until then.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok that's fine, though in this case its simple enough to remove, so should just do it, 1 less thing to do later