@@ -5,9 +5,13 @@ cimport cython
5
5
from cython cimport Py_ssize_t
6
6
7
7
import time
8
- from cpython.datetime cimport datetime, timedelta, time as dt_time
8
+ from cpython.datetime cimport (PyDateTime_IMPORT, PyDateTime_CheckExact,
9
+ datetime, timedelta,
10
+ time as dt_time)
11
+ PyDateTime_IMPORT
9
12
10
13
from dateutil.relativedelta import relativedelta
14
+ from pytz import UTC
11
15
12
16
import numpy as np
13
17
cimport numpy as cnp
@@ -19,7 +23,7 @@ from util cimport is_string_object, is_integer_object
19
23
20
24
from ccalendar import MONTHS, DAYS
21
25
from ccalendar cimport get_days_in_month, dayofweek
22
- from conversion cimport tz_convert_single, pydt_to_i8
26
+ from conversion cimport tz_convert_single, pydt_to_i8, localize_pydatetime
23
27
from frequencies cimport get_freq_code
24
28
from nattype cimport NPY_NAT
25
29
from np_datetime cimport (pandas_datetimestruct,
@@ -494,6 +498,31 @@ class BaseOffset(_BaseOffset):
494
498
# ----------------------------------------------------------------------
495
499
# RelativeDelta Arithmetic
496
500
501
+ cpdef datetime shift_day(datetime other, int days):
502
+ """
503
+ Increment the datetime `other` by the given number of days, retaining
504
+ the time-portion of the datetime. For tz-naive datetimes this is
505
+ equivalent to adding a timedelta. For tz-aware datetimes it is similar to
506
+ dateutil's relativedelta.__add__, but handles pytz tzinfo objects.
507
+
508
+ Parameters
509
+ ----------
510
+ other : datetime or Timestamp
511
+ days : int
512
+
513
+ Returns
514
+ -------
515
+ shifted: datetime or Timestamp
516
+ """
517
+ if other.tzinfo is None :
518
+ return other + timedelta(days = days)
519
+
520
+ tz = other.tzinfo
521
+ naive = other.replace(tzinfo = None )
522
+ shifted = naive + timedelta(days = days)
523
+ return localize_pydatetime(shifted, tz)
524
+
525
+
497
526
cdef inline int year_add_months(pandas_datetimestruct dts, int months) nogil:
498
527
""" new year number after shifting pandas_datetimestruct number of months"""
499
528
return dts.year + (dts.month + months - 1 ) / 12
0 commit comments