Skip to content
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

BUG: fix infer frequency for business daily #16683

Merged
merged 7 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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
16 changes: 13 additions & 3 deletions asv_bench/benchmarks/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ def setup(self):
self.rng6 = date_range(start='1/1/1', periods=self.N, freq='B')

self.rng7 = date_range(start='1/1/1700', freq='D', periods=100000)
self.a = self.rng7[:50000].append(self.rng7[50002:])
self.no_freq = self.rng7[:50000].append(self.rng7[50002:])
self.d_freq = self.rng7[:50000].append(self.rng7[50000:])

self.rng8 = date_range(start='1/1/1700', freq='B', periods=100000)
self.b_freq = self.rng8[:50000].append(self.rng8[50000:])

def time_add_timedelta(self):
(self.rng + dt.timedelta(minutes=2))
Expand Down Expand Up @@ -94,8 +98,14 @@ def time_infer_dst(self):
def time_timeseries_is_month_start(self):
self.rng6.is_month_start

def time_infer_freq(self):
infer_freq(self.a)
def time_infer_freq_none(self):
infer_freq(self.no_freq)

def time_infer_freq_daily(self):
infer_freq(self.d_freq)

def time_infer_freq_business(self):
infer_freq(self.b_freq)


class TimeDatetimeConverter(object):
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.20.3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Plotting
Groupby/Resample/Rolling
^^^^^^^^^^^^^^^^^^^^^^^^


- Bug in ``infer_freq`` causing indices with 2-day gaps during the working week to be wrongly inferred as business daily (:issue:`16624`)
Copy link
Contributor

Choose a reason for hiding this comment

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

move to 0.21.0


Sparse
^^^^^^
Expand Down
48 changes: 28 additions & 20 deletions pandas/tests/indexes/timedeltas/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,26 +567,34 @@ class TestSlicing(object):

def test_timedelta(self):
# this is valid too
index = date_range('1/1/2000', periods=50, freq='B')
shifted = index + timedelta(1)
back = shifted + timedelta(-1)
assert tm.equalContents(index, back)
assert shifted.freq == index.freq
assert shifted.freq == back.freq

result = index - timedelta(1)
expected = index + timedelta(-1)
tm.assert_index_equal(result, expected)

# GH4134, buggy with timedeltas
rng = date_range('2013', '2014')
s = Series(rng)
result1 = rng - pd.offsets.Hour(1)
result2 = DatetimeIndex(s - np.timedelta64(100000000))
result3 = rng - np.timedelta64(100000000)
result4 = DatetimeIndex(s - pd.offsets.Hour(1))
tm.assert_index_equal(result1, result4)
tm.assert_index_equal(result2, result3)
for freq in ['D', 'B']:
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of doing a loop, use parametrize

index = date_range('1/1/2000', periods=50, freq=freq)
shifted = index + timedelta(1)
back = shifted + timedelta(-1)
assert tm.equalContents(index, back)
if freq == 'D':
expected = pd.tseries.offsets.Day(1)
assert index.freq == expected
assert shifted.freq == expected
assert back.freq == expected
else: # freq == 'B'
assert index.freq == pd.tseries.offsets.BusinessDay(1)
assert shifted.freq == None
assert back.freq == pd.tseries.offsets.BusinessDay(1)

result = index - timedelta(1)
expected = index + timedelta(-1)
tm.assert_index_equal(result, expected)

# GH4134, buggy with timedeltas
rng = date_range('2013', '2014')
s = Series(rng)
result1 = rng - pd.offsets.Hour(1)
result2 = DatetimeIndex(s - np.timedelta64(100000000))
result3 = rng - np.timedelta64(100000000)
result4 = DatetimeIndex(s - pd.offsets.Hour(1))
tm.assert_index_equal(result1, result4)
tm.assert_index_equal(result2, result3)


class TestTimeSeries(object):
Expand Down
7 changes: 6 additions & 1 deletion pandas/tests/tseries/test_frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,14 @@ def test_raise_if_too_few(self):
pytest.raises(ValueError, frequencies.infer_freq, index)

def test_business_daily(self):
index = _dti(['12/31/1998', '1/3/1999', '1/4/1999'])
index = _dti(['01/01/1999', '1/4/1999', '1/5/1999'])
assert frequencies.infer_freq(index) == 'B'

def test_business_daily_look_alike(self):
# 'weekend' (2-day gap) in wrong place
Copy link
Contributor

Choose a reason for hiding this comment

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

add the issue number as a comment

index = _dti(['12/31/1998', '1/3/1999', '1/4/1999'])
assert frequencies.infer_freq(index) is None

def test_day(self):
self._check_tick(timedelta(1), 'D')

Expand Down
14 changes: 12 additions & 2 deletions pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,8 +975,7 @@ def _infer_daily_rule(self):
else:
return _maybe_add_count('D', days)

# Business daily. Maybe
if self.day_deltas == [1, 3]:
if self._is_business_daily():
return 'B'

wom_rule = self._get_wom_rule()
Expand Down Expand Up @@ -1012,6 +1011,17 @@ def _get_monthly_rule(self):
return {'cs': 'MS', 'bs': 'BMS',
'ce': 'M', 'be': 'BM'}.get(pos_check)

def _is_business_daily(self):
if self.day_deltas != [1, 3]: # quick check: cannot be business daily
Copy link
Contributor

Choose a reason for hiding this comment

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

put the comment on the line above (causes lint failure as line too long)

return False
# probably business daily, but need to confirm
Copy link
Contributor

Choose a reason for hiding this comment

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

add blank line before comment

first_weekday = self.index[0].weekday()
shifts = np.diff(np.asarray(self.index).view('i8'))
Copy link
Contributor

Choose a reason for hiding this comment

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

use np.diff(self.index.asi8)

shifts = np.floor_divide(shifts, _ONE_DAY)
weekdays = np.mod(first_weekday + np.cumsum(shifts), 7)
return np.all(((weekdays == 0) & (shifts == 3)) |
((weekdays > 0) & (weekdays <= 4) & (shifts == 1)))

def _get_wom_rule(self):
# wdiffs = unique(np.diff(self.index.week))
# We also need -47, -49, -48 to catch index spanning year boundary
Expand Down