Skip to content

Commit

Permalink
BUG: fix downsampling bug with intraday data when end.time() < start.…
Browse files Browse the repository at this point in the history
…time(). close #1772
  • Loading branch information
wesm committed Sep 11, 2012
1 parent 3151662 commit 54b54f8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
4 changes: 4 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ pandas 0.9.0
- Fixes to Period.start_time for non-daily frequencies (#1857)
- Fix failure when converter used on index_col in read_csv (#1835)
- Implement PeriodIndex.append so that pandas.concat works correctly (#1815)
- Avoid Cython out-of-bounds access causing segfault sometimes in pad_2d,
backfill_2d
- Fix resampling error with intraday times and anchored target time (like
AS-DEC) (#1772)

pandas 0.8.1
============
Expand Down
13 changes: 10 additions & 3 deletions pandas/tseries/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pandas.tseries.index import DatetimeIndex, date_range
from pandas.tseries.offsets import DateOffset, Tick, _delta_to_nanoseconds
from pandas.tseries.period import PeriodIndex, period_range
import pandas.tseries.tools as tools
import pandas.core.common as com

from pandas.lib import Timestamp
Expand Down Expand Up @@ -276,12 +277,18 @@ def _get_range_edges(axis, offset, closed='left', base=0):
return _adjust_dates_anchored(axis[0], axis[-1], offset,
closed=closed, base=base)

first, last = axis[0], axis[-1]
if not isinstance(offset, Tick):# and first.time() != last.time():
# hack!
first = tools.normalize_date(first)
last = tools.normalize_date(last)

if closed == 'left':
first = Timestamp(offset.rollback(axis[0]))
first = Timestamp(offset.rollback(first))
else:
first = Timestamp(axis[0] - offset)
first = Timestamp(first - offset)

last = Timestamp(axis[-1] + offset)
last = Timestamp(last + offset)

return first, last

Expand Down
12 changes: 12 additions & 0 deletions pandas/tseries/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,18 @@ def test_how_lambda_functions(self):
tm.assert_series_equal(result['foo'], foo_exp)
tm.assert_series_equal(result['bar'], bar_exp)

def test_resample_unequal_times(self):
# #1772
start = datetime(1999, 3, 1, 5)
# end hour is less than start
end = datetime(2012, 7, 31, 4)
bad_ind = date_range(start, end, freq="30min")
df = DataFrame({'close':1}, index=bad_ind)

# it works!
df.resample('AS', 'sum')


def _simple_ts(start, end, freq='D'):
rng = date_range(start, end, freq=freq)
return Series(np.random.randn(len(rng)), index=rng)
Expand Down

0 comments on commit 54b54f8

Please sign in to comment.