Skip to content

BUG: Set frequency for empty Series #14458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,10 @@ Bug Fixes


- Bug in the display of ``.info()`` where a qualifier (+) would always be displayed with a ``MultiIndex`` that contains only non-strings (:issue:`15245`)

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

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

Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,14 @@ def test_asfreq_fillvalue(self):
actual_series = ts.asfreq(freq='1S', fill_value=9.0)
assert_series_equal(expected_series, actual_series)

def test_asfreq_datetimeindex_empty_series(self):
# GH 14340
expected = Series(index=pd.DatetimeIndex(
["2016-09-29 11:00"])).asfreq('H')
result = Series(index=pd.DatetimeIndex(["2016-09-29 11:00"]),
data=[3]).asfreq('H')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you create one of the two without using asfreq.

Further, it is not fully clear what is what you actually testing, as the result of the empty series is called expected (you typically call the constructed expected value expected to compare the result of what you want to test)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jorisvandenbossche Actually the issue was that we didn't set frequency properly for empty series. Hence I'm testing this particular thing by comparing with frequency of a non-empty Series that we make using similar parameters with an empty one.

self.assert_index_equal(expected.index, result.index)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compare the series, e.g. assert_series_equal, and construct the expected directly.

you have two results to compare here, what you currently have labeled as expected and as result.

make another tests where you construct the expected directly (iow, dont' use .asfreq) . IOW you actually should use a direc

def test_first_last_valid(self):
N = len(self.frame.index)
mat = randn(N)
Expand Down
5 changes: 4 additions & 1 deletion pandas/tseries/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,10 @@ def asfreq(obj, freq, method=None, how=None, normalize=False, fill_value=None):
return new_obj
else:
if len(obj.index) == 0:
return obj.copy()
new_index = obj.index._shallow_copy(freq=to_offset(freq))
new_obj = obj.copy()
new_obj.index = new_index
return new_obj
dti = date_range(obj.index[0], obj.index[-1], freq=freq)
dti.name = obj.index.name
rs = obj.reindex(dti, method=method, fill_value=fill_value)
Expand Down