Skip to content

Commit f39a1bc

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 8e630b6 commit f39a1bc

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
@@ -246,6 +246,7 @@ Bug Fixes
246246
- Bug in ``DataFrame`` construction in which unsigned 64-bit integer elements were being converted to objects (:issue:`14881`)
247247
- Bug in ``astype()`` where ``inf`` values were incorrectly converted to integers. Now raises error now with ``astype()`` for Series and DataFrames (:issue:`14265`)
248248
- Bug in ``describe()`` when passing a numpy array which does not contain the median to the ``percentiles`` keyword argument (:issue:`14908`)
249+
- Bug in ``asfreq``, where frequency wasn't set for empty Series (:issue:`14320`)
249250

250251

251252

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
@@ -1350,7 +1350,10 @@ def asfreq(obj, freq, method=None, how=None, normalize=False):
13501350
return new_obj
13511351
else:
13521352
if len(obj.index) == 0:
1353-
return obj.copy()
1353+
new_index = obj.index._shallow_copy(freq=to_offset(freq))
1354+
new_obj = obj.copy()
1355+
new_obj.index = new_index
1356+
return new_obj
13541357
dti = date_range(obj.index[0], obj.index[-1], freq=freq)
13551358
dti.name = obj.index.name
13561359
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)