Skip to content

TST: check freq on series.index in assert_series_equal #33815

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
Apr 27, 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
4 changes: 4 additions & 0 deletions pandas/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,10 @@ def assert_series_equal(
check_categorical=check_categorical,
obj=f"{obj}.index",
)
if isinstance(left.index, (pd.DatetimeIndex, pd.TimedeltaIndex)):
lidx = left.index
ridx = right.index
assert lidx.freq == ridx.freq, (lidx.freq, ridx.freq)

if check_dtype:
# We want to skip exact dtype checking when `check_categorical`
Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,8 @@ def _check_op(series, other, op, pos_only=False):

cython_or_numpy = op(left, right)
python = left.combine(right, op)
if isinstance(other, Series) and not other.index.equals(series.index):
python.index = python.index._with_freq(None)
tm.assert_series_equal(cython_or_numpy, python)

def check(series, other):
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/frame/methods/test_at_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,8 @@ def test_at_time_axis(self, axis):
expected = ts.loc[:, indices]

result = ts.at_time("9:30", axis=axis)

# Without clearing freq, result has freq 1440T and expected 5T
result.index = result.index._with_freq(None)
expected.index = expected.index._with_freq(None)
tm.assert_frame_equal(result, expected)
6 changes: 5 additions & 1 deletion pandas/tests/frame/methods/test_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,12 @@ def test_tshift(self, datetime_frame):
columns=datetime_frame.columns,
)
shifted = inferred_ts.tshift(1)

expected = datetime_frame.tshift(1)
expected.index = expected.index._with_freq(None)
tm.assert_frame_equal(shifted, expected)

unshifted = shifted.tshift(-1)
tm.assert_frame_equal(shifted, datetime_frame.tshift(1))
tm.assert_frame_equal(unshifted, inferred_ts)

no_freq = datetime_frame.iloc[[0, 5, 7], :]
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/groupby/test_timegrouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ def test_nunique_with_timegrouper_and_nat(self):
grouper = pd.Grouper(key="time", freq="h")
result = test.groupby(grouper)["data"].nunique()
expected = test[test.time.notnull()].groupby(grouper)["data"].nunique()
expected.index = expected.index._with_freq(None)
tm.assert_series_equal(result, expected)

def test_scalar_call_versus_list_call(self):
Expand Down
8 changes: 6 additions & 2 deletions pandas/tests/indexes/timedeltas/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ def test_value_counts_unique(self):
idx = TimedeltaIndex(np.repeat(idx.values, range(1, len(idx) + 1)))

exp_idx = timedelta_range("1 days 18:00:00", freq="-1H", periods=10)
exp_idx = exp_idx._with_freq(None)
expected = Series(range(10, 0, -1), index=exp_idx, dtype="int64")

for obj in [idx, Series(idx)]:
tm.assert_series_equal(obj.value_counts(), expected)
obj = idx
tm.assert_series_equal(obj.value_counts(), expected)

obj = Series(idx)
tm.assert_series_equal(obj.value_counts(), expected)

expected = timedelta_range("1 days 09:00:00", freq="H", periods=10)
tm.assert_index_equal(idx.unique(), expected)
Expand Down
7 changes: 5 additions & 2 deletions pandas/tests/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def test_indexing_with_datetimeindex_tz(self):
for sel in (index, list(index)):
# getitem
result = ser[sel]
expected = ser
expected = ser.copy()
if sel is not index:
expected.index = expected.index._with_freq(None)
tm.assert_series_equal(result, expected)
Expand All @@ -159,7 +159,10 @@ def test_indexing_with_datetimeindex_tz(self):

# .loc getitem
result = ser.loc[sel]
tm.assert_series_equal(result, ser)
expected = ser.copy()
if sel is not index:
expected.index = expected.index._with_freq(None)
tm.assert_series_equal(result, expected)

# .loc setitem
result = ser.copy()
Expand Down
70 changes: 48 additions & 22 deletions pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest
import pytz

from pandas._libs import lib
from pandas.errors import UnsupportedFunctionCall

import pandas as pd
Expand Down Expand Up @@ -62,6 +63,7 @@ def test_custom_grouper(index):
arr = [1] + [5] * 2592
idx = dti[0:-1:5]
idx = idx.append(dti[-1:])
idx = pd.DatetimeIndex(idx, freq="5T")
expect = Series(arr, index=idx)

# GH2763 - return in put dtype if we can
Expand Down Expand Up @@ -502,15 +504,18 @@ def test_resample_how_method():
)
expected = Series(
[11, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, 22],
index=[
Timestamp("2015-03-31 21:48:50"),
Timestamp("2015-03-31 21:49:00"),
Timestamp("2015-03-31 21:49:10"),
Timestamp("2015-03-31 21:49:20"),
Timestamp("2015-03-31 21:49:30"),
Timestamp("2015-03-31 21:49:40"),
Timestamp("2015-03-31 21:49:50"),
],
index=pd.DatetimeIndex(
[
Timestamp("2015-03-31 21:48:50"),
Timestamp("2015-03-31 21:49:00"),
Timestamp("2015-03-31 21:49:10"),
Timestamp("2015-03-31 21:49:20"),
Timestamp("2015-03-31 21:49:30"),
Timestamp("2015-03-31 21:49:40"),
Timestamp("2015-03-31 21:49:50"),
],
freq="10s",
),
)
tm.assert_series_equal(s.resample("10S").mean(), expected)

Expand Down Expand Up @@ -778,7 +783,7 @@ def test_resample_single_group():
[30.1, 31.6],
index=[Timestamp("20070915 15:30:00"), Timestamp("20070915 15:40:00")],
)
expected = Series([0.75], index=[Timestamp("20070915")])
expected = Series([0.75], index=pd.DatetimeIndex([Timestamp("20070915")], freq="D"))
result = s.resample("D").apply(lambda x: np.std(x))
tm.assert_series_equal(result, expected)

Expand All @@ -801,7 +806,9 @@ def test_resample_float_base():

base = 17 + 43.51 / 60
result = s.resample("3min", base=base).size()
expected = Series(3, index=pd.DatetimeIndex(["2018-11-26 16:17:43.51"]))
expected = Series(
3, index=pd.DatetimeIndex(["2018-11-26 16:17:43.51"], freq="3min")
)
tm.assert_series_equal(result, expected)


Expand Down Expand Up @@ -938,13 +945,17 @@ def test_resample_anchored_intraday(simple_date_range_series):
result = df.resample("M").mean()
expected = df.resample("M", kind="period").mean().to_timestamp(how="end")
expected.index += Timedelta(1, "ns") - Timedelta(1, "D")
expected.index = expected.index._with_freq("infer")
assert expected.index.freq == "M"
tm.assert_frame_equal(result, expected)

result = df.resample("M", closed="left").mean()
exp = df.tshift(1, freq="D").resample("M", kind="period").mean()
exp = exp.to_timestamp(how="end")

exp.index = exp.index + Timedelta(1, "ns") - Timedelta(1, "D")
exp.index = exp.index._with_freq("infer")
assert exp.index.freq == "M"
tm.assert_frame_equal(result, exp)

rng = date_range("1/1/2012", "4/1/2012", freq="100min")
Expand All @@ -953,12 +964,16 @@ def test_resample_anchored_intraday(simple_date_range_series):
result = df.resample("Q").mean()
expected = df.resample("Q", kind="period").mean().to_timestamp(how="end")
expected.index += Timedelta(1, "ns") - Timedelta(1, "D")
expected.index._data.freq = "Q"
expected.index._freq = lib.no_default
tm.assert_frame_equal(result, expected)

result = df.resample("Q", closed="left").mean()
expected = df.tshift(1, freq="D").resample("Q", kind="period", closed="left").mean()
expected = expected.to_timestamp(how="end")
expected.index += Timedelta(1, "ns") - Timedelta(1, "D")
expected.index._data.freq = "Q"
expected.index._freq = lib.no_default
tm.assert_frame_equal(result, expected)

ts = simple_date_range_series("2012-04-29 23:00", "2012-04-30 5:00", freq="h")
Expand Down Expand Up @@ -1151,6 +1166,8 @@ def test_resample_timegrouper():
name="A",
)
expected = DataFrame({"B": [1, 0, 2, 2, 1]}, index=exp_idx)
if df["A"].isna().any():
expected.index = expected.index._with_freq(None)
tm.assert_frame_equal(result, expected)

result = df.groupby(pd.Grouper(freq="M", key="A")).count()
Expand All @@ -1163,6 +1180,8 @@ def test_resample_timegrouper():
index=exp_idx,
columns=["B", "C"],
)
if df["A"].isna().any():
expected.index = expected.index._with_freq(None)
tm.assert_frame_equal(result, expected)

result = df.groupby(pd.Grouper(freq="M", key="A")).count()
Expand Down Expand Up @@ -1291,7 +1310,8 @@ def test_resample_across_dst():
dti2 = DatetimeIndex(
pd.to_datetime(df2.ts, unit="s")
.dt.tz_localize("UTC")
.dt.tz_convert("Europe/Madrid")
.dt.tz_convert("Europe/Madrid"),
freq="H",
)
df = DataFrame([5, 5], index=dti1)

Expand Down Expand Up @@ -1322,13 +1342,17 @@ def test_resample_dst_anchor():
# 5172
dti = DatetimeIndex([datetime(2012, 11, 4, 23)], tz="US/Eastern")
df = DataFrame([5], index=dti)
tm.assert_frame_equal(
df.resample(rule="D").sum(), DataFrame([5], index=df.index.normalize())
)

dti = DatetimeIndex(df.index.normalize(), freq="D")
expected = DataFrame([5], index=dti)
tm.assert_frame_equal(df.resample(rule="D").sum(), expected)
df.resample(rule="MS").sum()
tm.assert_frame_equal(
df.resample(rule="MS").sum(),
DataFrame([5], index=DatetimeIndex([datetime(2012, 11, 1)], tz="US/Eastern")),
DataFrame(
[5],
index=DatetimeIndex([datetime(2012, 11, 1)], tz="US/Eastern", freq="MS"),
),
)

dti = date_range("2013-09-30", "2013-11-02", freq="30Min", tz="Europe/Paris")
Expand Down Expand Up @@ -1424,7 +1448,9 @@ def test_downsample_across_dst_weekly():
result = df.resample("1W").sum()
expected = DataFrame(
[23, 42],
index=pd.DatetimeIndex(["2017-03-26", "2017-04-02"], tz="Europe/Amsterdam"),
index=pd.DatetimeIndex(
["2017-03-26", "2017-04-02"], tz="Europe/Amsterdam", freq="W"
),
)
tm.assert_frame_equal(result, expected)

Expand All @@ -1447,12 +1473,12 @@ def test_downsample_dst_at_midnight():
data = list(range(len(index)))
dataframe = pd.DataFrame(data, index=index)
result = dataframe.groupby(pd.Grouper(freq="1D")).mean()
expected = DataFrame(
[7.5, 28.0, 44.5],
index=date_range("2018-11-03", periods=3).tz_localize(
"America/Havana", ambiguous=True
),

dti = date_range("2018-11-03", periods=3).tz_localize(
"America/Havana", ambiguous=True
)
dti = pd.DatetimeIndex(dti, freq="D")
expected = DataFrame([7.5, 28.0, 44.5], index=dti,)
tm.assert_frame_equal(result, expected)


Expand Down
15 changes: 13 additions & 2 deletions pandas/tests/resample/test_period_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,10 @@ def test_resample_with_pytz(self):
)
result = s.resample("D").mean()
expected = Series(
2, index=pd.DatetimeIndex(["2017-01-01", "2017-01-02"], tz="US/Eastern")
2,
index=pd.DatetimeIndex(
["2017-01-01", "2017-01-02"], tz="US/Eastern", freq="D"
),
)
tm.assert_series_equal(result, expected)
# Especially assert that the timezone is LMT for pytz
Expand Down Expand Up @@ -308,6 +311,7 @@ def test_resample_nonexistent_time_bin_edge(self):
index = date_range("2017-03-12", "2017-03-12 1:45:00", freq="15T")
s = Series(np.zeros(len(index)), index=index)
expected = s.tz_localize("US/Pacific")
expected.index = pd.DatetimeIndex(expected.index, freq="900S")
result = expected.resample("900S").mean()
tm.assert_series_equal(result, expected)

Expand Down Expand Up @@ -471,6 +475,7 @@ def test_resample_tz_localized(self):
]

exp = ts_local_naive.resample("W").mean().tz_localize("America/Los_Angeles")
exp.index = pd.DatetimeIndex(exp.index, freq="W")

tm.assert_series_equal(result, exp)

Expand Down Expand Up @@ -582,6 +587,7 @@ def test_resample_with_dst_time_change(self):
index = pd.to_datetime(expected_index_values, utc=True).tz_convert(
"America/Chicago"
)
index = pd.DatetimeIndex(index, freq="12h")
expected = pd.DataFrame(
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0],
index=index,
Expand Down Expand Up @@ -650,7 +656,9 @@ def test_evenly_divisible_with_no_extra_bins(self):
df = DataFrame(np.random.randn(9, 3), index=date_range("2000-1-1", periods=9))
result = df.resample("5D").mean()
expected = pd.concat([df.iloc[0:5].mean(), df.iloc[5:].mean()], axis=1).T
expected.index = [Timestamp("2000-1-1"), Timestamp("2000-1-6")]
expected.index = pd.DatetimeIndex(
[Timestamp("2000-1-1"), Timestamp("2000-1-6")], freq="5D"
)
tm.assert_frame_equal(result, expected)

index = date_range(start="2001-5-4", periods=28)
Expand Down Expand Up @@ -836,6 +844,9 @@ def test_resample_with_non_zero_base(self, start, end, start_freq, end_freq, bas
# to_timestamp casts 24H -> D
result = result.asfreq(end_freq) if end_freq == "24H" else result
expected = s.to_timestamp().resample(end_freq, base=base).mean()
if end_freq == "M":
# TODO: is non-tick the relevant characteristic?
expected.index = expected.index._with_freq(None)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
Expand Down
14 changes: 9 additions & 5 deletions pandas/tests/resample/test_time_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def test_resample_entirly_nat_window(method, method_args, unit):
s = pd.Series([0] * 2 + [np.nan] * 2, index=pd.date_range("2017", periods=4))
result = methodcaller(method, **method_args)(s.resample("2d"))
expected = pd.Series(
[0.0, unit], index=pd.to_datetime(["2017-01-01", "2017-01-03"])
[0.0, unit], index=pd.DatetimeIndex(["2017-01-01", "2017-01-03"], freq="2D")
)
tm.assert_series_equal(result, expected)

Expand Down Expand Up @@ -207,7 +207,8 @@ def test_aggregate_with_nat(func, fill_value):
pad = DataFrame([[fill_value] * 4], index=[3], columns=["A", "B", "C", "D"])
expected = normal_result.append(pad)
expected = expected.sort_index()
expected.index = date_range(start="2013-01-01", freq="D", periods=5, name="key")
dti = date_range(start="2013-01-01", freq="D", periods=5, name="key")
expected.index = dti._with_freq(None) # TODO: is this desired?
tm.assert_frame_equal(expected, dt_result)
assert dt_result.index.name == "key"

Expand Down Expand Up @@ -237,7 +238,9 @@ def test_aggregate_with_nat_size():
pad = Series([0], index=[3])
expected = normal_result.append(pad)
expected = expected.sort_index()
expected.index = date_range(start="2013-01-01", freq="D", periods=5, name="key")
expected.index = date_range(
start="2013-01-01", freq="D", periods=5, name="key"
)._with_freq(None)
tm.assert_series_equal(expected, dt_result)
assert dt_result.index.name == "key"

Expand Down Expand Up @@ -269,8 +272,9 @@ def test_repr():
def test_upsample_sum(method, method_args, expected_values):
s = pd.Series(1, index=pd.date_range("2017", periods=2, freq="H"))
resampled = s.resample("30T")
index = pd.to_datetime(
["2017-01-01T00:00:00", "2017-01-01T00:30:00", "2017-01-01T01:00:00"]
index = pd.DatetimeIndex(
["2017-01-01T00:00:00", "2017-01-01T00:30:00", "2017-01-01T01:00:00"],
freq="30T",
)
result = methodcaller(method, **method_args)(resampled)
expected = pd.Series(expected_values, index=index)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/resample/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_resample_categorical_data_with_timedeltaindex():
result = df.resample("10s").agg(lambda x: (x.value_counts().index[0]))
expected = DataFrame(
{"Group_obj": ["A", "A"], "Group": ["A", "A"]},
index=pd.to_timedelta([0, 10], unit="s"),
index=pd.TimedeltaIndex([0, 10], unit="s", freq="10s"),
)
expected = expected.reindex(["Group_obj", "Group"], axis=1)
expected["Group"] = expected["Group_obj"]
Expand Down
Loading