Skip to content

Commit 5918295

Browse files
author
Matt Roeschke
committed
Add more tests
1 parent af599da commit 5918295

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

pandas/tests/indexes/datetimes/test_date_range.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ def test_cdaterange_weekmask_and_holidays(self):
781781

782782
@pytest.mark.parametrize('freq', [freq for freq in prefix_mapping
783783
if freq.startswith('C')
784-
and freq != 'CD'])
784+
and freq != 'CD']) # CalendarDay
785785
def test_all_custom_freq(self, freq):
786786
# should not raise
787787
bdate_range(START, END, freq=freq, weekmask='Mon Wed Fri',

pandas/tests/indexes/timedeltas/test_timedelta_range.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def test_timedelta_range(self):
4848
result = df.loc['0s':, :]
4949
tm.assert_frame_equal(expected, result)
5050

51+
with pytest.raises(ValueError):
52+
# GH 22274: CalendarDay is a relative time measurement
53+
timedelta_range('1day', freq='CD', periods=2)
54+
5155
@pytest.mark.parametrize('periods, freq', [
5256
(3, '2D'), (5, 'D'), (6, '19H12T'), (7, '16H'), (9, '12H')])
5357
def test_linspace_behavior(self, periods, freq):

pandas/tests/tseries/offsets/test_offsets.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
from pandas.compat.numpy import np_datetime64_compat
1212

1313
from pandas.core.series import Series
14+
from pandas.core.frame import DataFrame
1415
from pandas._libs.tslibs import conversion
1516
from pandas._libs.tslibs.frequencies import (get_freq_code, get_freq_str,
1617
INVALID_FREQ_ERR_MSG)
1718
from pandas.tseries.frequencies import _offset_map, get_offset
1819
from pandas.core.indexes.datetimes import (
1920
_to_m8, DatetimeIndex, _daterange_cache)
21+
from pandas.core.indexes.timedeltas import TimedeltaIndex
2022
import pandas._libs.tslibs.offsets as liboffsets
2123
from pandas._libs.tslibs.offsets import CacheableOffset
2224
from pandas.tseries.offsets import (BDay, CDay, BQuarterEnd, BMonthEnd,
@@ -3180,16 +3182,28 @@ def test_last_week_of_month_on_offset():
31803182
fast = offset.onOffset(ts)
31813183
assert fast == slow
31823184

3183-
3184-
def test_CalendarDay_with_timezone():
3185+
@pytest.mark.parametrize('box, assert_func', [
3186+
[None, None],
3187+
[DatetimeIndex, 'assert_index_equal'],
3188+
[Series, 'assert_series_equal']])
3189+
def test_CalendarDay_with_timezone(box, assert_func):
31853190
# GH 22274
31863191
ts = Timestamp('2016-10-30 00:00:00+0300', tz='Europe/Helsinki')
3187-
result = ts + CalendarDay(1)
31883192
expected = Timestamp('2016-10-31 00:00:00+0200', tz='Europe/Helsinki')
3189-
assert result == expected
3193+
if box is not None:
3194+
ts = box(([ts]))
3195+
expected = box(([expected]))
3196+
result = ts + CalendarDay(1)
3197+
if assert_func:
3198+
getattr(tm, assert_func)(result, expected)
3199+
else:
3200+
assert result == expected
31903201

31913202
result = result - CalendarDay(1)
3192-
assert result == ts
3203+
if assert_func:
3204+
getattr(tm, assert_func)(result, ts)
3205+
else:
3206+
assert result == ts
31933207

31943208
# CalendarDay applied to a Timestamp that leads to ambiguous time
31953209
with pytest.raises(pytz.AmbiguousTimeError):
@@ -3199,6 +3213,7 @@ def test_CalendarDay_with_timezone():
31993213
with pytest.raises(pytz.NonExistentTimeError):
32003214
Timestamp("2019-03-09 02:00:00", tz='US/Pacific') + CalendarDay(1)
32013215

3216+
32023217
@pytest.mark.parametrize('arg, exp', [
32033218
[1, 2],
32043219
[-1, 0],
@@ -3211,8 +3226,14 @@ def test_CalendarDay_arithmetic_with_self(arg, exp):
32113226
assert result == expected
32123227

32133228

3214-
@pytest.mark.parametrize('arg', [timedelta(1), Day(1), Timedelta(1)])
3229+
@pytest.mark.parametrize('arg', [
3230+
timedelta(1),
3231+
Day(1),
3232+
Timedelta(1),
3233+
TimedeltaIndex([timedelta(1)])])
32153234
def test_CalendarDay_invalid_arithmetic(arg):
32163235
# GH 22274
3236+
# CalendarDay (relative time) cannot be added to Timedelta-like objects
3237+
# (absolute time)
32173238
with pytest.raises(TypeError):
32183239
CalendarDay(1) + arg

pandas/tseries/offsets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,7 +2140,7 @@ def __init__(self, n=1, normalize=False):
21402140
@apply_wraps
21412141
def apply(self, other):
21422142
"""
2143-
Apply the CalendarDay offset to a datetime object. Incoming datetime
2143+
Apply scalar arithmetic with CalendarDay offset. Incoming datetime
21442144
objects can be tz-aware or naive.
21452145
"""
21462146
if type(other) == type(self):
@@ -2160,7 +2160,7 @@ def apply(self, other):
21602160
return as_timestamp(other)
21612161
except TypeError:
21622162
raise TypeError("Cannot perform arithmetic between {other} and "
2163-
"{offset}".format(other=type(other), offset=self))
2163+
"CalendarDay".format(other=type(other)))
21642164

21652165
@apply_index_wraps
21662166
def apply_index(self, i):

0 commit comments

Comments
 (0)