Skip to content

Commit ab29f93

Browse files
BranYangjreback
authored andcommitted
Fix #12037 Error when Resampling using pd.tseries.offsets.Nano as period
Closes #12037 Author: Bran Yang <[email protected]> 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
1 parent e9558d3 commit ab29f93

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

doc/source/whatsnew/v0.18.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,7 @@ Bug Fixes
828828
- Bug in ``Series.str.get_dummies`` when one of the variables was 'name' (:issue:`12180`)
829829
- Bug in ``pd.concat`` while concatenating tz-aware NaT series. (:issue:`11693`, :issue:`11755`)
830830
- Bug in ``pd.read_stata`` with version <= 108 files (:issue:`12232`)
831+
- Bug in ``Series.resample`` using a frequency of ``Nano`` when the index is a ``DatetimeIndex`` and contains non-zero nanosecond parts (:issue:`12037`)
831832

832833

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

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

850851

851852
- Bug in timezone info lost when broadcasting scalar datetime to ``DataFrame`` (:issue:`11682`)

pandas/tseries/resample.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -854,9 +854,14 @@ def _get_time_bins(self, ax):
854854
closed=self.closed,
855855
base=self.base)
856856
tz = ax.tz
857+
# GH #12037
858+
# use first/last directly instead of call replace() on them
859+
# because replace() will swallow the nanosecond part
860+
# thus last bin maybe slightly before the end if the end contains
861+
# nanosecond part and lead to `Values falls after last bin` error
857862
binner = labels = DatetimeIndex(freq=self.freq,
858-
start=first.replace(tzinfo=None),
859-
end=last.replace(tzinfo=None),
863+
start=first,
864+
end=last,
860865
tz=tz,
861866
name=ax.name)
862867

pandas/tseries/tests/test_resample.py

+23
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,29 @@ def test_monthly_resample_error(self):
12361236
# it works!
12371237
ts.resample('M')
12381238

1239+
def test_nanosecond_resample_error(self):
1240+
# GH 12307 - Values falls after last bin when
1241+
# Resampling using pd.tseries.offsets.Nano as period
1242+
start = 1443707890427
1243+
exp_start = 1443707890400
1244+
indx = pd.date_range(
1245+
start=pd.to_datetime(start),
1246+
periods=10,
1247+
freq='100n'
1248+
)
1249+
ts = pd.Series(range(len(indx)), index=indx)
1250+
r = ts.resample(pd.tseries.offsets.Nano(100))
1251+
result = r.agg('mean')
1252+
1253+
exp_indx = pd.date_range(
1254+
start=pd.to_datetime(exp_start),
1255+
periods=10,
1256+
freq='100n'
1257+
)
1258+
exp = pd.Series(range(len(exp_indx)), index=exp_indx)
1259+
1260+
assert_series_equal(result, exp)
1261+
12391262
def test_resample_anchored_intraday(self):
12401263
# #1471, #1458
12411264

0 commit comments

Comments
 (0)