Skip to content

BUG: resample with TimedeltaIndex, fenceposts are off #22488

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 2 commits into from
Sep 5, 2018
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ Groupby/Resample/Rolling
datetime-like index leading to incorrect results and also segfault. (:issue:`21704`)
- Bug in :meth:`Resampler.apply` when passing postiional arguments to applied func (:issue:`14615`).
- Bug in :meth:`Series.resample` when passing ``numpy.timedelta64`` to `loffset` kwarg (:issue:`7687`).
- Bug in :meth:`Resampler.asfreq` when frequency of ``TimedeltaIndex`` is a subperiod of a new frequency (:issue:`13022`).

Sparse
^^^^^^
Expand Down
21 changes: 9 additions & 12 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,10 @@ def _downsample(self, how, **kwargs):
return self._wrap_result(result)

def _adjust_binner_for_upsample(self, binner):
""" adjust our binner when upsampling """
"""
Adjust our binner when upsampling.
The range of a new index should not be outside specified range
"""
if self.closed == 'right':
binner = binner[1:]
else:
Expand Down Expand Up @@ -1156,17 +1159,11 @@ def _get_binner_for_time(self):
return self.groupby._get_time_delta_bins(self.ax)

def _adjust_binner_for_upsample(self, binner):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a comment here (and maybe refresh the comment in same method for DTI resampling)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jreback

Could you please clarify how I should refresh the comment for DTI? What should be instead of adjust our binner when upsampling?

Copy link
Contributor

Choose a reason for hiding this comment

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

add a comment on why we are NOT adjusting anything here for TDI (e.g. just returning it directly). basically explain why DTI but not TDI needs adjustment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just a reminder: here is why we adjust a binner #10885

""" adjust our binner when upsampling """
ax = self.ax

if is_subperiod(ax.freq, self.freq):
# We are actually downsampling
# but are in the asfreq path
# GH 12926
if self.closed == 'right':
binner = binner[1:]
else:
binner = binner[:-1]
"""
Adjust our binner when upsampling.
The range of a new index is allowed to be greater than original range
so we don't need to change the length of a binner, GH 13022
"""
return binner


Expand Down
19 changes: 12 additions & 7 deletions pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from pandas.compat import range, lrange, zip, OrderedDict
from pandas.errors import UnsupportedFunctionCall
import pandas.tseries.offsets as offsets
from pandas.tseries.frequencies import to_offset
from pandas.tseries.offsets import Minute, BDay

from pandas.core.groupby.groupby import DataError
Expand Down Expand Up @@ -626,12 +625,7 @@ def test_asfreq(self, series_and_frame, freq):
obj = series_and_frame

result = obj.resample(freq).asfreq()
if freq == '2D':
new_index = obj.index.take(np.arange(0, len(obj.index), 2))
new_index.freq = to_offset('2D')
else:
new_index = self.create_index(obj.index[0], obj.index[-1],
freq=freq)
new_index = self.create_index(obj.index[0], obj.index[-1], freq=freq)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mroeschke

The case noted here is covered by this test. However, it used to produce the incorrect index so tests were passing (which is incorrect). Here is the old expected index for TDI:

In [1]: obj.index.take(np.arange(0, len(obj.index), 2))
Out[1]: TimedeltaIndex(['1 days', '3 days', '5 days', '7 days', '9 days'], dtype='timedelta64[ns]', freq='2D')

expected = obj.reindex(new_index)
assert_almost_equal(result, expected)

Expand Down Expand Up @@ -2932,6 +2926,17 @@ def test_resample_with_nat(self):
freq='1S'))
assert_frame_equal(result, expected)

def test_resample_as_freq_with_subperiod(self):
# GH 13022
index = timedelta_range('00:00:00', '00:10:00', freq='5T')
df = DataFrame(data={'value': [1, 5, 10]}, index=index)
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add the other example in the OP here as well (or is it too duplicative)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jreback

The other example is duplicative of TestTimedeltaIndex.test_asfreq test. Should I add it?

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, no that is fine

result = df.resample('2T').asfreq()
expected_data = {'value': [1, np.nan, np.nan, np.nan, np.nan, 10]}
expected = DataFrame(data=expected_data,
index=timedelta_range('00:00:00',
'00:10:00', freq='2T'))
tm.assert_frame_equal(result, expected)


class TestResamplerGrouper(object):

Expand Down