Skip to content

Commit

Permalink
Fix #12037 Error when Resampling using pd.tseries.offsets.Nano as period
Browse files Browse the repository at this point in the history
Closes #12037

Author: Bran Yang <snowolfy@163.com>

Closes #12270 from BranYang/nanosec and squashes the following commits:

bff0c85 [Bran Yang] Add to whatsnew and some comments
fd0b307 [Bran Yang] Fix #12037 Error when Resampling using pd.tseries.offsets.Nano as period
  • Loading branch information
BranYang authored and jreback committed Feb 10, 2016
1 parent e9558d3 commit ab29f93
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,7 @@ Bug Fixes
- Bug in ``Series.str.get_dummies`` when one of the variables was 'name' (:issue:`12180`)
- Bug in ``pd.concat`` while concatenating tz-aware NaT series. (:issue:`11693`, :issue:`11755`)
- Bug in ``pd.read_stata`` with version <= 108 files (:issue:`12232`)
- Bug in ``Series.resample`` using a frequency of ``Nano`` when the index is a ``DatetimeIndex`` and contains non-zero nanosecond parts (:issue:`12037`)


- Bug in ``Timedelta.round`` with negative values (:issue:`11690`)
Expand All @@ -845,7 +846,7 @@ Bug Fixes
- Bug in ``DataFrame.query`` containing an assignment (:issue:`8664`)

- Bug in ``from_msgpack`` where ``__contains__()`` fails for columns of the unpacked ``DataFrame``, if the ``DataFrame`` has object columns. (:issue:`11880`)
- Bug in ``df.resample()`` on categorical data with ``TimedeltaIndex`` (:issue:`12169`)
- Bug in ``.resample`` on categorical data with ``TimedeltaIndex`` (:issue:`12169`)


- Bug in timezone info lost when broadcasting scalar datetime to ``DataFrame`` (:issue:`11682`)
Expand Down
9 changes: 7 additions & 2 deletions pandas/tseries/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,9 +854,14 @@ def _get_time_bins(self, ax):
closed=self.closed,
base=self.base)
tz = ax.tz
# GH #12037
# use first/last directly instead of call replace() on them
# because replace() will swallow the nanosecond part
# thus last bin maybe slightly before the end if the end contains
# nanosecond part and lead to `Values falls after last bin` error
binner = labels = DatetimeIndex(freq=self.freq,
start=first.replace(tzinfo=None),
end=last.replace(tzinfo=None),
start=first,
end=last,
tz=tz,
name=ax.name)

Expand Down
23 changes: 23 additions & 0 deletions pandas/tseries/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,29 @@ def test_monthly_resample_error(self):
# it works!
ts.resample('M')

def test_nanosecond_resample_error(self):
# GH 12307 - Values falls after last bin when
# Resampling using pd.tseries.offsets.Nano as period
start = 1443707890427
exp_start = 1443707890400
indx = pd.date_range(
start=pd.to_datetime(start),
periods=10,
freq='100n'
)
ts = pd.Series(range(len(indx)), index=indx)
r = ts.resample(pd.tseries.offsets.Nano(100))
result = r.agg('mean')

exp_indx = pd.date_range(
start=pd.to_datetime(exp_start),
periods=10,
freq='100n'
)
exp = pd.Series(range(len(exp_indx)), index=exp_indx)

assert_series_equal(result, exp)

def test_resample_anchored_intraday(self):
# #1471, #1458

Expand Down

0 comments on commit ab29f93

Please sign in to comment.