Skip to content

REF/TST: share asfreq tests #44219

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

Merged
merged 1 commit into from
Oct 29, 2021
Merged
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
126 changes: 111 additions & 15 deletions pandas/tests/frame/methods/test_asfreq.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from datetime import datetime

import numpy as np
import pytest

from pandas import (
DataFrame,
DatetimeIndex,
Series,
date_range,
period_range,
to_datetime,
)
import pandas._testing as tm
Expand All @@ -15,41 +17,135 @@


class TestAsFreq:
def test_asfreq_resample_set_correct_freq(self):
def test_asfreq2(self, frame_or_series):
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 avoid the "2" here and be more descriptive

Copy link
Member Author

Choose a reason for hiding this comment

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

im open to suggestions. there are a lot of places with e.g. "# TODO: more informative test name"

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah let's do in a followup is fine.

ts = frame_or_series(
[0.0, 1.0, 2.0],
index=DatetimeIndex(
[
datetime(2009, 10, 30),
datetime(2009, 11, 30),
datetime(2009, 12, 31),
],
freq="BM",
),
)

daily_ts = ts.asfreq("B")
monthly_ts = daily_ts.asfreq("BM")
tm.assert_equal(monthly_ts, ts)

daily_ts = ts.asfreq("B", method="pad")
monthly_ts = daily_ts.asfreq("BM")
tm.assert_equal(monthly_ts, ts)

daily_ts = ts.asfreq(offsets.BDay())
monthly_ts = daily_ts.asfreq(offsets.BMonthEnd())
tm.assert_equal(monthly_ts, ts)

result = ts[:0].asfreq("M")
assert len(result) == 0
assert result is not ts

if frame_or_series is Series:
daily_ts = ts.asfreq("D", fill_value=-1)
result = daily_ts.value_counts().sort_index()
expected = Series([60, 1, 1, 1], index=[-1.0, 2.0, 1.0, 0.0]).sort_index()
tm.assert_series_equal(result, expected)

def test_asfreq_datetimeindex_empty(self, frame_or_series):
# GH#14320
index = DatetimeIndex(["2016-09-29 11:00"])
expected = frame_or_series(index=index, dtype=object).asfreq("H")
result = frame_or_series([3], index=index.copy()).asfreq("H")
tm.assert_index_equal(expected.index, result.index)

@pytest.mark.parametrize("tz", ["US/Eastern", "dateutil/US/Eastern"])
def test_tz_aware_asfreq_smoke(self, tz, frame_or_series):
dr = date_range("2011-12-01", "2012-07-20", freq="D", tz=tz)

obj = frame_or_series(np.random.randn(len(dr)), index=dr)

# it works!
obj.asfreq("T")

def test_asfreq_normalize(self, frame_or_series):
rng = date_range("1/1/2000 09:30", periods=20)
norm = date_range("1/1/2000", periods=20)

vals = np.random.randn(20, 3)

obj = DataFrame(vals, index=rng)
expected = DataFrame(vals, index=norm)
if frame_or_series is Series:
obj = obj[0]
expected = expected[0]

result = obj.asfreq("D", normalize=True)
tm.assert_equal(result, expected)

def test_asfreq_keep_index_name(self, frame_or_series):
# GH#9854
index_name = "bar"
index = date_range("20130101", periods=20, name=index_name)
obj = DataFrame(list(range(20)), columns=["foo"], index=index)
if frame_or_series is Series:
obj = obj["foo"]

assert index_name == obj.index.name
assert index_name == obj.asfreq("10D").index.name

def test_asfreq_ts(self, frame_or_series):
index = period_range(freq="A", start="1/1/2001", end="12/31/2010")
obj = DataFrame(np.random.randn(len(index), 3), index=index)
if frame_or_series is Series:
obj = obj[0]

result = obj.asfreq("D", how="end")
exp_index = index.asfreq("D", how="end")
assert len(result) == len(obj)
tm.assert_index_equal(result.index, exp_index)

result = obj.asfreq("D", how="start")
exp_index = index.asfreq("D", how="start")
assert len(result) == len(obj)
tm.assert_index_equal(result.index, exp_index)

def test_asfreq_resample_set_correct_freq(self, frame_or_series):
# GH#5613
# we test if .asfreq() and .resample() set the correct value for .freq
df = DataFrame(
{"date": ["2012-01-01", "2012-01-02", "2012-01-03"], "col": [1, 2, 3]}
)
df = df.set_index(to_datetime(df.date))
dti = to_datetime(["2012-01-01", "2012-01-02", "2012-01-03"])
obj = DataFrame({"col": [1, 2, 3]}, index=dti)
if frame_or_series is Series:
obj = obj["col"]

# testing the settings before calling .asfreq() and .resample()
assert df.index.freq is None
assert df.index.inferred_freq == "D"
assert obj.index.freq is None
assert obj.index.inferred_freq == "D"

# does .asfreq() set .freq correctly?
assert df.asfreq("D").index.freq == "D"
assert obj.asfreq("D").index.freq == "D"

# does .resample() set .freq correctly?
assert df.resample("D").asfreq().index.freq == "D"
assert obj.resample("D").asfreq().index.freq == "D"

def test_asfreq_empty(self, datetime_frame):
# test does not blow up on length-0 DataFrame
zero_length = datetime_frame.reindex([])
result = zero_length.asfreq("BM")
assert result is not zero_length

def test_asfreq(self, datetime_frame):
offset_monthly = datetime_frame.asfreq(offsets.BMonthEnd())
rule_monthly = datetime_frame.asfreq("BM")

tm.assert_almost_equal(offset_monthly["A"], rule_monthly["A"])
tm.assert_frame_equal(offset_monthly, rule_monthly)

filled = rule_monthly.asfreq("B", method="pad") # noqa
# TODO: actually check that this worked.

# don't forget!
filled_dep = rule_monthly.asfreq("B", method="pad") # noqa

# test does not blow up on length-0 DataFrame
zero_length = datetime_frame.reindex([])
result = zero_length.asfreq("BM")
assert result is not zero_length

def test_asfreq_datetimeindex(self):
df = DataFrame(
{"A": [1, 2, 3]},
Expand Down
116 changes: 0 additions & 116 deletions pandas/tests/series/methods/test_asfreq.py

This file was deleted.