Skip to content

TST: fix tests for asserting matching freq #33737

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 2 commits into from
Apr 23, 2020
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
3 changes: 3 additions & 0 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ def test_numpy_repeat(self):
@pytest.mark.parametrize("klass", [list, tuple, np.array, Series])
def test_where(self, klass):
i = self.create_index()
if isinstance(i, (pd.DatetimeIndex, pd.TimedeltaIndex)):
# where does not preserve freq
i._set_freq(None)

cond = [True] * len(i)
result = i.where(klass(cond))
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ def test_map_dictlike(self, mapper):
expected = index + index.freq

# don't compare the freqs
if isinstance(expected, pd.DatetimeIndex):
expected._data.freq = None
if isinstance(expected, (pd.DatetimeIndex, pd.TimedeltaIndex)):
expected._set_freq(None)

result = index.map(mapper(expected, index))
tm.assert_index_equal(result, expected)
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/indexes/datetimes/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def test_astype_with_tz(self):
result = idx.astype("datetime64[ns, US/Eastern]")
expected = date_range("20170101 03:00:00", periods=4, tz="US/Eastern")
tm.assert_index_equal(result, expected)
assert result.freq == expected.freq

# GH 18951: tz-naive to tz-aware
idx = date_range("20170101", periods=4)
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/indexes/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def test_construction_with_alt(self, kwargs, tz_aware_fixture):
def test_construction_with_alt_tz_localize(self, kwargs, tz_aware_fixture):
tz = tz_aware_fixture
i = pd.date_range("20130101", periods=5, freq="H", tz=tz)
i._set_freq(None)
kwargs = {key: attrgetter(val)(i) for key, val in kwargs.items()}

if "tz" in kwargs:
Expand Down Expand Up @@ -703,7 +704,9 @@ def test_constructor_start_end_with_tz(self, tz):
end = Timestamp("2013-01-02 06:00:00", tz="America/Los_Angeles")
result = date_range(freq="D", start=start, end=end, tz=tz)
expected = DatetimeIndex(
["2013-01-01 06:00:00", "2013-01-02 06:00:00"], tz="America/Los_Angeles"
["2013-01-01 06:00:00", "2013-01-02 06:00:00"],
tz="America/Los_Angeles",
freq="D",
)
tm.assert_index_equal(result, expected)
# Especially assert that the timezone is consistent for pytz
Expand Down
28 changes: 20 additions & 8 deletions pandas/tests/indexes/datetimes/test_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def test_date_range_normalize(self):
rng = date_range(snap, periods=n, normalize=False, freq="2D")

offset = timedelta(2)
values = DatetimeIndex([snap + i * offset for i in range(n)])
values = DatetimeIndex([snap + i * offset for i in range(n)], freq=offset)

tm.assert_index_equal(rng, values)

Expand Down Expand Up @@ -413,7 +413,7 @@ def test_construct_over_dst(self):
pre_dst,
pst_dst,
]
expected = DatetimeIndex(expect_data)
expected = DatetimeIndex(expect_data, freq="H")
result = date_range(start="2010-11-7", periods=3, freq="H", tz="US/Pacific")
tm.assert_index_equal(result, expected)

Expand All @@ -427,7 +427,8 @@ def test_construct_with_different_start_end_string_format(self):
Timestamp("2013-01-01 00:00:00+09:00"),
Timestamp("2013-01-01 01:00:00+09:00"),
Timestamp("2013-01-01 02:00:00+09:00"),
]
],
freq="H",
)
tm.assert_index_equal(result, expected)

Expand All @@ -442,7 +443,7 @@ def test_range_bug(self):
result = date_range("2011-1-1", "2012-1-31", freq=offset)

start = datetime(2011, 1, 1)
expected = DatetimeIndex([start + i * offset for i in range(5)])
expected = DatetimeIndex([start + i * offset for i in range(5)], freq=offset)
tm.assert_index_equal(result, expected)

def test_range_tz_pytz(self):
Expand Down Expand Up @@ -861,6 +862,7 @@ def test_bdays_and_open_boundaries(self, closed):
bday_end = "2018-07-27" # Friday
expected = pd.date_range(bday_start, bday_end, freq="D")
tm.assert_index_equal(result, expected)
# Note: we do _not_ expect the freqs to match here

def test_bday_near_overflow(self):
# GH#24252 avoid doing unnecessary addition that _would_ overflow
Expand Down Expand Up @@ -910,15 +912,19 @@ def test_daterange_bug_456(self):

def test_cdaterange(self):
result = bdate_range("2013-05-01", periods=3, freq="C")
expected = DatetimeIndex(["2013-05-01", "2013-05-02", "2013-05-03"])
expected = DatetimeIndex(["2013-05-01", "2013-05-02", "2013-05-03"], freq="C")
tm.assert_index_equal(result, expected)
assert result.freq == expected.freq

def test_cdaterange_weekmask(self):
result = bdate_range(
"2013-05-01", periods=3, freq="C", weekmask="Sun Mon Tue Wed Thu"
)
expected = DatetimeIndex(["2013-05-01", "2013-05-02", "2013-05-05"])
expected = DatetimeIndex(
["2013-05-01", "2013-05-02", "2013-05-05"], freq=result.freq
)
tm.assert_index_equal(result, expected)
assert result.freq == expected.freq

# raise with non-custom freq
msg = (
Expand All @@ -930,8 +936,11 @@ def test_cdaterange_weekmask(self):

def test_cdaterange_holidays(self):
result = bdate_range("2013-05-01", periods=3, freq="C", holidays=["2013-05-01"])
expected = DatetimeIndex(["2013-05-02", "2013-05-03", "2013-05-06"])
expected = DatetimeIndex(
["2013-05-02", "2013-05-03", "2013-05-06"], freq=result.freq
)
tm.assert_index_equal(result, expected)
assert result.freq == expected.freq

# raise with non-custom freq
msg = (
Expand All @@ -949,8 +958,11 @@ def test_cdaterange_weekmask_and_holidays(self):
weekmask="Sun Mon Tue Wed Thu",
holidays=["2013-05-01"],
)
expected = DatetimeIndex(["2013-05-02", "2013-05-05", "2013-05-06"])
expected = DatetimeIndex(
["2013-05-02", "2013-05-05", "2013-05-06"], freq=result.freq
)
tm.assert_index_equal(result, expected)
assert result.freq == expected.freq

# raise with non-custom freq
msg = (
Expand Down
14 changes: 10 additions & 4 deletions pandas/tests/indexes/period/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,24 @@ def test_astype_array_fallback(self):
def test_period_astype_to_timestamp(self):
pi = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="M")

exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"])
tm.assert_index_equal(pi.astype("datetime64[ns]"), exp)
exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], freq="MS")
res = pi.astype("datetime64[ns]")
tm.assert_index_equal(res, exp)
assert res.freq == exp.freq

exp = DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"])
exp = exp + Timedelta(1, "D") - Timedelta(1, "ns")
tm.assert_index_equal(pi.astype("datetime64[ns]", how="end"), exp)
res = pi.astype("datetime64[ns]", how="end")
tm.assert_index_equal(res, exp)
assert res.freq == exp.freq

exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], tz="US/Eastern")
res = pi.astype("datetime64[ns, US/Eastern]")
tm.assert_index_equal(pi.astype("datetime64[ns, US/Eastern]"), exp)
tm.assert_index_equal(res, exp)
assert res.freq == exp.freq

exp = DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"], tz="US/Eastern")
exp = exp + Timedelta(1, "D") - Timedelta(1, "ns")
res = pi.astype("datetime64[ns, US/Eastern]", how="end")
tm.assert_index_equal(res, exp)
assert res.freq == exp.freq
3 changes: 2 additions & 1 deletion pandas/tests/indexes/period/test_to_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ def test_to_timestamp_quarterly_bug(self):
pindex = PeriodIndex(year=years, quarter=quarters)

stamps = pindex.to_timestamp("D", "end")
expected = DatetimeIndex([x.to_timestamp("D", "end") for x in pindex], freq="Q")
expected = DatetimeIndex([x.to_timestamp("D", "end") for x in pindex])
tm.assert_index_equal(stamps, expected)
assert stamps.freq == expected.freq

def test_to_timestamp_pi_mult(self):
idx = PeriodIndex(["2011-01", "NaT", "2011-02"], freq="2M", name="idx")
Expand Down