Skip to content

Commit d13c281

Browse files
committed
Normalized index type and name of empty series
1 parent 611d296 commit d13c281

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ Groupby/Resample/Rolling
298298
- Bug in :func:`infer_freq` causing indices with 2-day gaps during the working week to be wrongly inferred as business daily (:issue:`16624`)
299299
- Bug in ``.rolling(...).quantile()`` which incorrectly used different defaults than :func:`Series.quantile()` and :func:`DataFrame.quantile()` (:issue:`9413`, :issue:`16211`)
300300
- Bug in ``groupby.transform()`` that would coerce boolean dtypes back to float (:issue:`16875`)
301+
- Bug in ``Series.resample(...).apply()`` where an empty ``Series`` modified the source index and did not return the name of a ``Series`` (:issue:`14313`)
301302

302303
Sparse
303304
^^^^^^

pandas/core/resample.py

+5
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,11 @@ def _wrap_result(self, result):
778778
# convert if needed
779779
if self.kind == 'period' and not isinstance(result.index, PeriodIndex):
780780
result.index = result.index.to_period(self.freq)
781+
782+
if isinstance(result, com.ABCSeries) and result.empty:
783+
obj = self.obj
784+
result.index = obj.index._shallow_copy(freq=to_offset(self.freq))
785+
result.name = getattr(obj, 'name', None)
781786
return result
782787

783788

pandas/tests/test_resample.py

+11
Original file line numberDiff line numberDiff line change
@@ -2192,6 +2192,17 @@ def test_resample_datetime_values(self):
21922192
res = df['timestamp'].resample('2D').first()
21932193
tm.assert_series_equal(res, exp)
21942194

2195+
def test_apply_to_empty_series(self):
2196+
# GH 14313
2197+
series = self.create_series()[:0]
2198+
2199+
for freq in ['M', 'D', 'H']:
2200+
result = series.resample(freq).apply(lambda x: 1)
2201+
expected = series.resample(freq).apply(np.sum)
2202+
2203+
assert result.name == expected.name
2204+
assert_series_equal(result, expected, check_dtype=False)
2205+
21952206

21962207
class TestPeriodIndex(Base):
21972208
_index_factory = lambda x: period_range

0 commit comments

Comments
 (0)