Skip to content

Commit 12aba35

Browse files
committed
Normalized index type and name of empty series
1 parent f433061 commit 12aba35

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
@@ -299,6 +299,7 @@ Groupby/Resample/Rolling
299299
- Bug in :func:`infer_freq` causing indices with 2-day gaps during the working week to be wrongly inferred as business daily (:issue:`16624`)
300300
- Bug in ``.rolling(...).quantile()`` which incorrectly used different defaults than :func:`Series.quantile()` and :func:`DataFrame.quantile()` (:issue:`9413`, :issue:`16211`)
301301
- Bug in ``groupby.transform()`` that would coerce boolean dtypes back to float (:issue:`16875`)
302+
- Bug in ``Series.resample(...).apply()`` where an empty ``Series`` modified the source index and did not return the name of a ``Series`` (:issue:`14313`)
302303

303304
Sparse
304305
^^^^^^

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)