Skip to content

Commit 51018f6

Browse files
authored
TST: check freq on series.index in assert_series_equal (pandas-dev#33815)
1 parent bcd81e9 commit 51018f6

20 files changed

+142
-54
lines changed

pandas/_testing.py

+4
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,10 @@ def assert_series_equal(
11711171
check_categorical=check_categorical,
11721172
obj=f"{obj}.index",
11731173
)
1174+
if isinstance(left.index, (pd.DatetimeIndex, pd.TimedeltaIndex)):
1175+
lidx = left.index
1176+
ridx = right.index
1177+
assert lidx.freq == ridx.freq, (lidx.freq, ridx.freq)
11741178

11751179
if check_dtype:
11761180
# We want to skip exact dtype checking when `check_categorical`

pandas/tests/arithmetic/test_numeric.py

+2
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,8 @@ def _check_op(series, other, op, pos_only=False):
919919

920920
cython_or_numpy = op(left, right)
921921
python = left.combine(right, op)
922+
if isinstance(other, Series) and not other.index.equals(series.index):
923+
python.index = python.index._with_freq(None)
922924
tm.assert_series_equal(cython_or_numpy, python)
923925

924926
def check(series, other):

pandas/tests/frame/methods/test_at_time.py

+4
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,8 @@ def test_at_time_axis(self, axis):
8383
expected = ts.loc[:, indices]
8484

8585
result = ts.at_time("9:30", axis=axis)
86+
87+
# Without clearing freq, result has freq 1440T and expected 5T
88+
result.index = result.index._with_freq(None)
89+
expected.index = expected.index._with_freq(None)
8690
tm.assert_frame_equal(result, expected)

pandas/tests/frame/methods/test_shift.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,12 @@ def test_tshift(self, datetime_frame):
177177
columns=datetime_frame.columns,
178178
)
179179
shifted = inferred_ts.tshift(1)
180+
181+
expected = datetime_frame.tshift(1)
182+
expected.index = expected.index._with_freq(None)
183+
tm.assert_frame_equal(shifted, expected)
184+
180185
unshifted = shifted.tshift(-1)
181-
tm.assert_frame_equal(shifted, datetime_frame.tshift(1))
182186
tm.assert_frame_equal(unshifted, inferred_ts)
183187

184188
no_freq = datetime_frame.iloc[[0, 5, 7], :]

pandas/tests/groupby/test_timegrouper.py

+1
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@ def test_nunique_with_timegrouper_and_nat(self):
747747
grouper = pd.Grouper(key="time", freq="h")
748748
result = test.groupby(grouper)["data"].nunique()
749749
expected = test[test.time.notnull()].groupby(grouper)["data"].nunique()
750+
expected.index = expected.index._with_freq(None)
750751
tm.assert_series_equal(result, expected)
751752

752753
def test_scalar_call_versus_list_call(self):

pandas/tests/indexes/timedeltas/test_ops.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ def test_value_counts_unique(self):
1818
idx = TimedeltaIndex(np.repeat(idx.values, range(1, len(idx) + 1)))
1919

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

23-
for obj in [idx, Series(idx)]:
24-
tm.assert_series_equal(obj.value_counts(), expected)
24+
obj = idx
25+
tm.assert_series_equal(obj.value_counts(), expected)
26+
27+
obj = Series(idx)
28+
tm.assert_series_equal(obj.value_counts(), expected)
2529

2630
expected = timedelta_range("1 days 09:00:00", freq="H", periods=10)
2731
tm.assert_index_equal(idx.unique(), expected)

pandas/tests/indexing/test_datetime.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def test_indexing_with_datetimeindex_tz(self):
146146
for sel in (index, list(index)):
147147
# getitem
148148
result = ser[sel]
149-
expected = ser
149+
expected = ser.copy()
150150
if sel is not index:
151151
expected.index = expected.index._with_freq(None)
152152
tm.assert_series_equal(result, expected)
@@ -159,7 +159,10 @@ def test_indexing_with_datetimeindex_tz(self):
159159

160160
# .loc getitem
161161
result = ser.loc[sel]
162-
tm.assert_series_equal(result, ser)
162+
expected = ser.copy()
163+
if sel is not index:
164+
expected.index = expected.index._with_freq(None)
165+
tm.assert_series_equal(result, expected)
163166

164167
# .loc setitem
165168
result = ser.copy()

pandas/tests/resample/test_datetime_index.py

+48-22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pytest
77
import pytz
88

9+
from pandas._libs import lib
910
from pandas.errors import UnsupportedFunctionCall
1011

1112
import pandas as pd
@@ -62,6 +63,7 @@ def test_custom_grouper(index):
6263
arr = [1] + [5] * 2592
6364
idx = dti[0:-1:5]
6465
idx = idx.append(dti[-1:])
66+
idx = pd.DatetimeIndex(idx, freq="5T")
6567
expect = Series(arr, index=idx)
6668

6769
# GH2763 - return in put dtype if we can
@@ -502,15 +504,18 @@ def test_resample_how_method():
502504
)
503505
expected = Series(
504506
[11, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, 22],
505-
index=[
506-
Timestamp("2015-03-31 21:48:50"),
507-
Timestamp("2015-03-31 21:49:00"),
508-
Timestamp("2015-03-31 21:49:10"),
509-
Timestamp("2015-03-31 21:49:20"),
510-
Timestamp("2015-03-31 21:49:30"),
511-
Timestamp("2015-03-31 21:49:40"),
512-
Timestamp("2015-03-31 21:49:50"),
513-
],
507+
index=pd.DatetimeIndex(
508+
[
509+
Timestamp("2015-03-31 21:48:50"),
510+
Timestamp("2015-03-31 21:49:00"),
511+
Timestamp("2015-03-31 21:49:10"),
512+
Timestamp("2015-03-31 21:49:20"),
513+
Timestamp("2015-03-31 21:49:30"),
514+
Timestamp("2015-03-31 21:49:40"),
515+
Timestamp("2015-03-31 21:49:50"),
516+
],
517+
freq="10s",
518+
),
514519
)
515520
tm.assert_series_equal(s.resample("10S").mean(), expected)
516521

@@ -778,7 +783,7 @@ def test_resample_single_group():
778783
[30.1, 31.6],
779784
index=[Timestamp("20070915 15:30:00"), Timestamp("20070915 15:40:00")],
780785
)
781-
expected = Series([0.75], index=[Timestamp("20070915")])
786+
expected = Series([0.75], index=pd.DatetimeIndex([Timestamp("20070915")], freq="D"))
782787
result = s.resample("D").apply(lambda x: np.std(x))
783788
tm.assert_series_equal(result, expected)
784789

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

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

807814

@@ -938,13 +945,17 @@ def test_resample_anchored_intraday(simple_date_range_series):
938945
result = df.resample("M").mean()
939946
expected = df.resample("M", kind="period").mean().to_timestamp(how="end")
940947
expected.index += Timedelta(1, "ns") - Timedelta(1, "D")
948+
expected.index = expected.index._with_freq("infer")
949+
assert expected.index.freq == "M"
941950
tm.assert_frame_equal(result, expected)
942951

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

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

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

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

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

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

11681187
result = df.groupby(pd.Grouper(freq="M", key="A")).count()
@@ -1291,7 +1310,8 @@ def test_resample_across_dst():
12911310
dti2 = DatetimeIndex(
12921311
pd.to_datetime(df2.ts, unit="s")
12931312
.dt.tz_localize("UTC")
1294-
.dt.tz_convert("Europe/Madrid")
1313+
.dt.tz_convert("Europe/Madrid"),
1314+
freq="H",
12951315
)
12961316
df = DataFrame([5, 5], index=dti1)
12971317

@@ -1322,13 +1342,17 @@ def test_resample_dst_anchor():
13221342
# 5172
13231343
dti = DatetimeIndex([datetime(2012, 11, 4, 23)], tz="US/Eastern")
13241344
df = DataFrame([5], index=dti)
1325-
tm.assert_frame_equal(
1326-
df.resample(rule="D").sum(), DataFrame([5], index=df.index.normalize())
1327-
)
1345+
1346+
dti = DatetimeIndex(df.index.normalize(), freq="D")
1347+
expected = DataFrame([5], index=dti)
1348+
tm.assert_frame_equal(df.resample(rule="D").sum(), expected)
13281349
df.resample(rule="MS").sum()
13291350
tm.assert_frame_equal(
13301351
df.resample(rule="MS").sum(),
1331-
DataFrame([5], index=DatetimeIndex([datetime(2012, 11, 1)], tz="US/Eastern")),
1352+
DataFrame(
1353+
[5],
1354+
index=DatetimeIndex([datetime(2012, 11, 1)], tz="US/Eastern", freq="MS"),
1355+
),
13321356
)
13331357

13341358
dti = date_range("2013-09-30", "2013-11-02", freq="30Min", tz="Europe/Paris")
@@ -1424,7 +1448,9 @@ def test_downsample_across_dst_weekly():
14241448
result = df.resample("1W").sum()
14251449
expected = DataFrame(
14261450
[23, 42],
1427-
index=pd.DatetimeIndex(["2017-03-26", "2017-04-02"], tz="Europe/Amsterdam"),
1451+
index=pd.DatetimeIndex(
1452+
["2017-03-26", "2017-04-02"], tz="Europe/Amsterdam", freq="W"
1453+
),
14281454
)
14291455
tm.assert_frame_equal(result, expected)
14301456

@@ -1447,12 +1473,12 @@ def test_downsample_dst_at_midnight():
14471473
data = list(range(len(index)))
14481474
dataframe = pd.DataFrame(data, index=index)
14491475
result = dataframe.groupby(pd.Grouper(freq="1D")).mean()
1450-
expected = DataFrame(
1451-
[7.5, 28.0, 44.5],
1452-
index=date_range("2018-11-03", periods=3).tz_localize(
1453-
"America/Havana", ambiguous=True
1454-
),
1476+
1477+
dti = date_range("2018-11-03", periods=3).tz_localize(
1478+
"America/Havana", ambiguous=True
14551479
)
1480+
dti = pd.DatetimeIndex(dti, freq="D")
1481+
expected = DataFrame([7.5, 28.0, 44.5], index=dti,)
14561482
tm.assert_frame_equal(result, expected)
14571483

14581484

pandas/tests/resample/test_period_index.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,10 @@ def test_resample_with_pytz(self):
270270
)
271271
result = s.resample("D").mean()
272272
expected = Series(
273-
2, index=pd.DatetimeIndex(["2017-01-01", "2017-01-02"], tz="US/Eastern")
273+
2,
274+
index=pd.DatetimeIndex(
275+
["2017-01-01", "2017-01-02"], tz="US/Eastern", freq="D"
276+
),
274277
)
275278
tm.assert_series_equal(result, expected)
276279
# Especially assert that the timezone is LMT for pytz
@@ -308,6 +311,7 @@ def test_resample_nonexistent_time_bin_edge(self):
308311
index = date_range("2017-03-12", "2017-03-12 1:45:00", freq="15T")
309312
s = Series(np.zeros(len(index)), index=index)
310313
expected = s.tz_localize("US/Pacific")
314+
expected.index = pd.DatetimeIndex(expected.index, freq="900S")
311315
result = expected.resample("900S").mean()
312316
tm.assert_series_equal(result, expected)
313317

@@ -471,6 +475,7 @@ def test_resample_tz_localized(self):
471475
]
472476

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

475480
tm.assert_series_equal(result, exp)
476481

@@ -582,6 +587,7 @@ def test_resample_with_dst_time_change(self):
582587
index = pd.to_datetime(expected_index_values, utc=True).tz_convert(
583588
"America/Chicago"
584589
)
590+
index = pd.DatetimeIndex(index, freq="12h")
585591
expected = pd.DataFrame(
586592
[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],
587593
index=index,
@@ -650,7 +656,9 @@ def test_evenly_divisible_with_no_extra_bins(self):
650656
df = DataFrame(np.random.randn(9, 3), index=date_range("2000-1-1", periods=9))
651657
result = df.resample("5D").mean()
652658
expected = pd.concat([df.iloc[0:5].mean(), df.iloc[5:].mean()], axis=1).T
653-
expected.index = [Timestamp("2000-1-1"), Timestamp("2000-1-6")]
659+
expected.index = pd.DatetimeIndex(
660+
[Timestamp("2000-1-1"), Timestamp("2000-1-6")], freq="5D"
661+
)
654662
tm.assert_frame_equal(result, expected)
655663

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

841852
@pytest.mark.parametrize(

pandas/tests/resample/test_time_grouper.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def test_resample_entirly_nat_window(method, method_args, unit):
170170
s = pd.Series([0] * 2 + [np.nan] * 2, index=pd.date_range("2017", periods=4))
171171
result = methodcaller(method, **method_args)(s.resample("2d"))
172172
expected = pd.Series(
173-
[0.0, unit], index=pd.to_datetime(["2017-01-01", "2017-01-03"])
173+
[0.0, unit], index=pd.DatetimeIndex(["2017-01-01", "2017-01-03"], freq="2D")
174174
)
175175
tm.assert_series_equal(result, expected)
176176

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

@@ -237,7 +238,9 @@ def test_aggregate_with_nat_size():
237238
pad = Series([0], index=[3])
238239
expected = normal_result.append(pad)
239240
expected = expected.sort_index()
240-
expected.index = date_range(start="2013-01-01", freq="D", periods=5, name="key")
241+
expected.index = date_range(
242+
start="2013-01-01", freq="D", periods=5, name="key"
243+
)._with_freq(None)
241244
tm.assert_series_equal(expected, dt_result)
242245
assert dt_result.index.name == "key"
243246

@@ -269,8 +272,9 @@ def test_repr():
269272
def test_upsample_sum(method, method_args, expected_values):
270273
s = pd.Series(1, index=pd.date_range("2017", periods=2, freq="H"))
271274
resampled = s.resample("30T")
272-
index = pd.to_datetime(
273-
["2017-01-01T00:00:00", "2017-01-01T00:30:00", "2017-01-01T01:00:00"]
275+
index = pd.DatetimeIndex(
276+
["2017-01-01T00:00:00", "2017-01-01T00:30:00", "2017-01-01T01:00:00"],
277+
freq="30T",
274278
)
275279
result = methodcaller(method, **method_args)(resampled)
276280
expected = pd.Series(expected_values, index=index)

pandas/tests/resample/test_timedelta.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def test_resample_categorical_data_with_timedeltaindex():
102102
result = df.resample("10s").agg(lambda x: (x.value_counts().index[0]))
103103
expected = DataFrame(
104104
{"Group_obj": ["A", "A"], "Group": ["A", "A"]},
105-
index=pd.to_timedelta([0, 10], unit="s"),
105+
index=pd.TimedeltaIndex([0, 10], unit="s", freq="10s"),
106106
)
107107
expected = expected.reindex(["Group_obj", "Group"], axis=1)
108108
expected["Group"] = expected["Group_obj"]

0 commit comments

Comments
 (0)