Skip to content

ENH/API: offsets funcs now accepts datetime64 #7452

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
Jun 14, 2014
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
2 changes: 1 addition & 1 deletion doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Enhancements




- All offsets ``apply``, ``rollforward`` and ``rollback`` can now handle ``np.datetime64``, previously results in ``ApplyTypeError`` (:issue:`7452`)



Expand Down
16 changes: 2 additions & 14 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def wrapper(self, other):
return tslib.NaT
if type(other) == date:
other = datetime(other.year, other.month, other.day)
elif isinstance(other, np.datetime64):
other = as_timestamp(other)

result = func(self, other)

Expand Down Expand Up @@ -555,20 +557,6 @@ def apply(self, other):

return as_timestamp(result)

elif isinstance(other, np.datetime64):
date_in = other
np_day = date_in.astype('datetime64[D]')
np_time = date_in - np_day

np_incr_dt = np.busday_offset(np_day, self.n, roll=roll,
busdaycal=self.busdaycalendar)
result = np_incr_dt + np_time

if self.offset:
result = result + self.offset

return as_timestamp(result)

elif isinstance(other, (timedelta, Tick)):
return BDay(self.n, offset=self.offset + other,
normalize=self.normalize)
Expand Down
65 changes: 35 additions & 30 deletions pandas/tseries/tests/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,19 +222,20 @@ def _check_nanofunc_works(self, offset, funcname, dt, expected):
self.assertEqual(func(t1), expected)

def test_apply(self):
dt = datetime(2011, 1, 1, 9, 0)
sdt = datetime(2011, 1, 1, 9, 0)
ndt = np.datetime64('2011-01-01 09:00Z')

for offset in self.offset_types:
expected = self.expecteds[offset.__name__]

if offset == Nano:
self._check_nanofunc_works(offset, 'apply', dt, expected)
else:
self._check_offsetfunc_works(offset, 'apply', dt, expected)
for dt in [sdt, ndt]:
expected = self.expecteds[offset.__name__]
if offset == Nano:
self._check_nanofunc_works(offset, 'apply', dt, expected)
else:
self._check_offsetfunc_works(offset, 'apply', dt, expected)

expected = Timestamp(expected.date())
self._check_offsetfunc_works(offset, 'apply', dt, expected,
normalize=True)
expected = Timestamp(expected.date())
self._check_offsetfunc_works(offset, 'apply', dt, expected,
normalize=True)

def test_rollforward(self):
expecteds = self.expecteds.copy()
Expand All @@ -261,17 +262,19 @@ def test_rollforward(self):
'Micro': Timestamp('2011-01-01 00:00:00')}
norm_expected.update(normalized)

dt = datetime(2011, 1, 1, 9, 0)
for offset in self.offset_types:
expected = expecteds[offset.__name__]
sdt = datetime(2011, 1, 1, 9, 0)
ndt = np.datetime64('2011-01-01 09:00Z')

if offset == Nano:
self._check_nanofunc_works(offset, 'rollforward', dt, expected)
else:
self._check_offsetfunc_works(offset, 'rollforward', dt, expected)
expected = norm_expected[offset.__name__]
self._check_offsetfunc_works(offset, 'rollforward', dt, expected,
normalize=True)
for offset in self.offset_types:
for dt in [sdt, ndt]:
expected = expecteds[offset.__name__]
if offset == Nano:
self._check_nanofunc_works(offset, 'rollforward', dt, expected)
else:
self._check_offsetfunc_works(offset, 'rollforward', dt, expected)
expected = norm_expected[offset.__name__]
self._check_offsetfunc_works(offset, 'rollforward', dt, expected,
normalize=True)

def test_rollback(self):
expecteds = {'BusinessDay': Timestamp('2010-12-31 09:00:00'),
Expand Down Expand Up @@ -315,18 +318,20 @@ def test_rollback(self):
'Micro': Timestamp('2011-01-01 00:00:00')}
norm_expected.update(normalized)

dt = datetime(2011, 1, 1, 9, 0)
for offset in self.offset_types:
expected = expecteds[offset.__name__]
sdt = datetime(2011, 1, 1, 9, 0)
ndt = np.datetime64('2011-01-01 09:00Z')

if offset == Nano:
self._check_nanofunc_works(offset, 'rollback', dt, expected)
else:
self._check_offsetfunc_works(offset, 'rollback', dt, expected)
for offset in self.offset_types:
for dt in [sdt, ndt]:
expected = expecteds[offset.__name__]
if offset == Nano:
self._check_nanofunc_works(offset, 'rollback', dt, expected)
else:
self._check_offsetfunc_works(offset, 'rollback', dt, expected)

expected = norm_expected[offset.__name__]
self._check_offsetfunc_works(offset, 'rollback',
dt, expected, normalize=True)
expected = norm_expected[offset.__name__]
self._check_offsetfunc_works(offset, 'rollback',
dt, expected, normalize=True)

def test_onOffset(self):

Expand Down
19 changes: 19 additions & 0 deletions vb_suite/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,33 @@ def date_range(start=None, end=None, periods=None, freq=None):
setup = common_setup + """
import datetime as dt
import pandas as pd
import numpy as np

date = dt.datetime(2011,1,1)
dt64 = np.datetime64('2011-01-01 09:00Z')

day = pd.offsets.Day()
year = pd.offsets.YearBegin()
cday = pd.offsets.CustomBusinessDay()
cme = pd.offsets.CustomBusinessMonthEnd()
"""
timeseries_day_incr = Benchmark("date + day",setup)

timeseries_day_apply = Benchmark("day.apply(date)",setup)

timeseries_year_incr = Benchmark("date + year",setup)

timeseries_year_apply = Benchmark("year.apply(date)",setup)

timeseries_custom_bday_incr = \
Benchmark("date + cday",setup)

timeseries_custom_bday_apply = \
Benchmark("cday.apply(date)",setup)

timeseries_custom_bday_apply_dt64 = \
Benchmark("cday.apply(dt64)",setup)

# Increment by n
timeseries_custom_bday_incr_n = \
Benchmark("date + 10 * cday",setup)
Expand Down