Skip to content

Commit 1494d85

Browse files
committed
BUG: Set frequency for empty Series
Add test to verify frequency for empty series Update 0.19.1 whatsnew doc Fix linting; exceeded number of characters on one line Improve tests, add assertion for PeriodIndex as well Refactor tests and move to correct file Move changelog entry from 0.19.1 to 0.20.0
1 parent 098831d commit 1494d85

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ Bug Fixes
300300
- Bug in ``pd.read_csv()`` in which the ``dialect`` parameter was not being verified before processing (:issue:`14898`)
301301

302302
- Bug in ``DataFrame.loc`` with indexing a ``MultiIndex`` with a ``Series`` indexer (:issue:`14730`)
303+
- Bug in ``asfreq``, where frequency wasn't set for empty Series (:issue:`14320`)
303304

304305
- Bug in ``pd.read_msgpack()`` in which ``Series`` categoricals were being improperly processed (:issue:`14901`)
305306
- Bug in ``Series.ffill()`` with mixed dtypes containing tz-aware datetimes. (:issue:`14956`)

pandas/tests/frame/test_timeseries.py

+8
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,14 @@ def test_asfreq_datetimeindex(self):
323323
ts = df['A'].asfreq('B')
324324
tm.assertIsInstance(ts.index, DatetimeIndex)
325325

326+
def test_asfreq_datetimeindex_empty_series(self):
327+
# GH 14340
328+
expected = Series(index=pd.DatetimeIndex(
329+
["2016-09-29 11:00"])).asfreq('H')
330+
result = Series(index=pd.DatetimeIndex(["2016-09-29 11:00"]),
331+
data=[3]).asfreq('H')
332+
self.assert_index_equal(expected.index, result.index)
333+
326334
def test_first_last_valid(self):
327335
N = len(self.frame.index)
328336
mat = randn(N)

pandas/tseries/resample.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,10 @@ def asfreq(obj, freq, method=None, how=None, normalize=False):
13631363
return new_obj
13641364
else:
13651365
if len(obj.index) == 0:
1366-
return obj.copy()
1366+
new_index = obj.index._shallow_copy(freq=to_offset(freq))
1367+
new_obj = obj.copy()
1368+
new_obj.index = new_index
1369+
return new_obj
13671370
dti = date_range(obj.index[0], obj.index[-1], freq=freq)
13681371
dti.name = obj.index.name
13691372
rs = obj.reindex(dti, method=method)

pandas/tseries/tests/test_period.py

+8
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,14 @@ def test_asfreq_mult(self):
15961596
self.assertEqual(result.ordinal, expected.ordinal)
15971597
self.assertEqual(result.freq, expected.freq)
15981598

1599+
def test_asfreq_period_empty_series(self):
1600+
# GH 14340
1601+
expected = Series(index=pd.PeriodIndex(['2011-1', '2011-2', '2011-3'],
1602+
freq='M')).asfreq('M')
1603+
result = Series(index=pd.PeriodIndex(['2011-1', '2011-2', '2011-3'],
1604+
freq='M'), data=[3]).asfreq('M')
1605+
self.assert_index_equal(expected.index, result.index)
1606+
15991607
def test_asfreq_combined(self):
16001608
# normal freq to combined freq
16011609
p = Period('2007', freq='H')

0 commit comments

Comments
 (0)