Skip to content

Commit ef2cf61

Browse files
committed
BUG: resampling with NaT in TimedeltaIndex (pandas-dev#13223)
1 parent c26e5bb commit ef2cf61

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

pandas/tseries/resample.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,8 +1224,10 @@ def _get_time_delta_bins(self, ax):
12241224
data=[], freq=self.freq, name=ax.name)
12251225
return binner, [], labels
12261226

1227-
start = ax[0]
1228-
end = ax[-1]
1227+
# Addresses GH #13223
1228+
start = ax.min()
1229+
end = ax.max()
1230+
12291231
labels = binner = TimedeltaIndex(start=start,
12301232
end=end,
12311233
freq=self.freq,
@@ -1234,6 +1236,13 @@ def _get_time_delta_bins(self, ax):
12341236
end_stamps = labels + 1
12351237
bins = ax.searchsorted(end_stamps, side='left')
12361238

1239+
if ax.hasnans:
1240+
binner = binner.insert(0, tslib.NaT)
1241+
labels = labels.insert(0, tslib.NaT)
1242+
1243+
n_NaT = sum([ax_i is tslib.NaT for ax_i in ax])
1244+
bins = np.insert(bins, 0, n_NaT)
1245+
12371246
# Addresses GH #10530
12381247
if self.base > 0:
12391248
labels += type(self.freq)(self.base)

pandas/tseries/tests/test_resample.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,15 @@ def test_resample_timedelta_idempotency(self):
10251025
expected = series
10261026
assert_series_equal(result, expected)
10271027

1028+
def test_resample_timedelta_missing_values(self):
1029+
# GH 13223
1030+
index = pd.to_timedelta(['0s', pd.NaT, '2s'])
1031+
series = pd.Series([2, 3, 5], index=index)
1032+
result = series.resample('1s').mean()
1033+
expected = pd.Series([2, np.nan, 5], index=pd.timedelta_range(
1034+
start='0s', end='2s', freq='1s'))
1035+
assert_series_equal(result, expected)
1036+
10281037
def test_resample_rounding(self):
10291038
# GH 8371
10301039
# odd results when rounding is needed

0 commit comments

Comments
 (0)