diff --git a/pandas/tests/arrays/categorical/test_constructors.py b/pandas/tests/arrays/categorical/test_constructors.py index 3e31c1acbe09d..9be741274c15a 100644 --- a/pandas/tests/arrays/categorical/test_constructors.py +++ b/pandas/tests/arrays/categorical/test_constructors.py @@ -331,6 +331,7 @@ def test_constructor_with_datetimelike(self, dtl): def test_constructor_from_index_series_datetimetz(self): idx = date_range("2015-01-01 10:00", freq="D", periods=3, tz="US/Eastern") + idx = idx._with_freq(None) # freq not preserved in result.categories result = Categorical(idx) tm.assert_index_equal(result.categories, idx) @@ -339,6 +340,7 @@ def test_constructor_from_index_series_datetimetz(self): def test_constructor_from_index_series_timedelta(self): idx = timedelta_range("1 days", freq="D", periods=3) + idx = idx._with_freq(None) # freq not preserved in result.categories result = Categorical(idx) tm.assert_index_equal(result.categories, idx) diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index 80739b9512953..580c0c7a8cab5 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -302,8 +302,14 @@ def test_round(self, tz_naive_fixture): result = dti.round(freq="2T") expected = dti - pd.Timedelta(minutes=1) + expected = expected._with_freq(None) tm.assert_index_equal(result, expected) + dta = dti._data + result = dta.round(freq="2T") + expected = expected._data._with_freq(None) + tm.assert_datetime_array_equal(result, expected) + def test_array_interface(self, datetime_index): arr = DatetimeArray(datetime_index) diff --git a/pandas/tests/frame/methods/test_tz_convert.py b/pandas/tests/frame/methods/test_tz_convert.py index ea8c4b88538d4..d2ab7a386a92d 100644 --- a/pandas/tests/frame/methods/test_tz_convert.py +++ b/pandas/tests/frame/methods/test_tz_convert.py @@ -44,6 +44,12 @@ def test_tz_convert_and_localize(self, fn): # GH7846 df2 = DataFrame(np.ones(5), MultiIndex.from_arrays([l0, l1])) + # freq is not preserved in MultiIndex construction + l1_expected = l1_expected._with_freq(None) + l0_expected = l0_expected._with_freq(None) + l1 = l1._with_freq(None) + l0 = l0._with_freq(None) + df3 = getattr(df2, fn)("US/Pacific", level=0) assert not df3.index.levels[0].equals(l0) tm.assert_index_equal(df3.index.levels[0], l0_expected) diff --git a/pandas/tests/frame/test_axis_select_reindex.py b/pandas/tests/frame/test_axis_select_reindex.py index f61512b1a62d9..b68e20bee63fc 100644 --- a/pandas/tests/frame/test_axis_select_reindex.py +++ b/pandas/tests/frame/test_axis_select_reindex.py @@ -91,7 +91,8 @@ def test_reindex(self, float_frame): # pass non-Index newFrame = float_frame.reindex(list(datetime_series.index)) - tm.assert_index_equal(newFrame.index, datetime_series.index) + expected = datetime_series.index._with_freq(None) + tm.assert_index_equal(newFrame.index, expected) # copy with no axes result = float_frame.reindex() diff --git a/pandas/tests/frame/test_to_csv.py b/pandas/tests/frame/test_to_csv.py index a9d9d0ace8701..9c656dd69abe2 100644 --- a/pandas/tests/frame/test_to_csv.py +++ b/pandas/tests/frame/test_to_csv.py @@ -54,6 +54,8 @@ def test_to_csv_from_csv1(self, float_frame, datetime_frame): float_frame.to_csv(path, index=False) # test roundtrip + # freq does not roundtrip + datetime_frame.index = datetime_frame.index._with_freq(None) datetime_frame.to_csv(path) recons = self.read_csv(path) tm.assert_frame_equal(datetime_frame, recons) @@ -1157,6 +1159,7 @@ def test_to_csv_with_dst_transitions(self): ) for i in [times, times + pd.Timedelta("10s")]: + i = i._with_freq(None) # freq is not preserved by read_csv time_range = np.array(range(len(i)), dtype="int64") df = DataFrame({"A": time_range}, index=i) df.to_csv(path, index=True) @@ -1170,6 +1173,8 @@ def test_to_csv_with_dst_transitions(self): # GH11619 idx = pd.date_range("2015-01-01", "2015-12-31", freq="H", tz="Europe/Paris") + idx = idx._with_freq(None) # freq does not round-trip + idx._data._freq = None # otherwise there is trouble on unpickle df = DataFrame({"values": 1, "idx": idx}, index=idx) with tm.ensure_clean("csv_date_format_with_dst") as path: df.to_csv(path, index=True) diff --git a/pandas/tests/groupby/test_timegrouper.py b/pandas/tests/groupby/test_timegrouper.py index 7cac13efb71f3..6d29ebd7ba795 100644 --- a/pandas/tests/groupby/test_timegrouper.py +++ b/pandas/tests/groupby/test_timegrouper.py @@ -8,7 +8,16 @@ import pytz import pandas as pd -from pandas import DataFrame, Index, MultiIndex, Series, Timestamp, date_range +from pandas import ( + DataFrame, + DatetimeIndex, + Index, + MultiIndex, + Series, + Timestamp, + date_range, + offsets, +) import pandas._testing as tm from pandas.core.groupby.grouper import Grouper from pandas.core.groupby.ops import BinGrouper @@ -243,17 +252,20 @@ def test_timegrouper_with_reg_groups(self): # single groupers expected = DataFrame( - {"Quantity": [31], "Date": [datetime(2013, 10, 31, 0, 0)]} - ).set_index("Date") + [[31]], + columns=["Quantity"], + index=DatetimeIndex( + [datetime(2013, 10, 31, 0, 0)], freq=offsets.MonthEnd(), name="Date" + ), + ) result = df.groupby(pd.Grouper(freq="1M")).sum() tm.assert_frame_equal(result, expected) result = df.groupby([pd.Grouper(freq="1M")]).sum() tm.assert_frame_equal(result, expected) - expected = DataFrame( - {"Quantity": [31], "Date": [datetime(2013, 11, 30, 0, 0)]} - ).set_index("Date") + expected.index = expected.index.shift(1) + assert expected.index.freq == offsets.MonthEnd() result = df.groupby(pd.Grouper(freq="1M", key="Date")).sum() tm.assert_frame_equal(result, expected) @@ -448,7 +460,7 @@ def test_groupby_groups_datetimeindex(self): for date in dates: result = grouped.get_group(date) data = [[df.loc[date, "A"], df.loc[date, "B"]]] - expected_index = pd.DatetimeIndex([date], name="date") + expected_index = pd.DatetimeIndex([date], name="date", freq="D") expected = pd.DataFrame(data, columns=list("AB"), index=expected_index) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/indexes/datetimes/test_astype.py b/pandas/tests/indexes/datetimes/test_astype.py index 6ef831a9daaaf..3e7e76bba0dde 100644 --- a/pandas/tests/indexes/datetimes/test_astype.py +++ b/pandas/tests/indexes/datetimes/test_astype.py @@ -75,8 +75,10 @@ def test_astype_tzaware_to_tzaware(self): def test_astype_tznaive_to_tzaware(self): # GH 18951: tz-naive to tz-aware idx = date_range("20170101", periods=4) + idx = idx._with_freq(None) # tz_localize does not preserve freq result = idx.astype("datetime64[ns, US/Eastern]") expected = date_range("20170101", periods=4, tz="US/Eastern") + expected = expected._with_freq(None) tm.assert_index_equal(result, expected) def test_astype_str_nat(self): diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 1083f1c332705..7b42e9646918e 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -383,6 +383,7 @@ def test_constructor_dtypes_datetime(self, tz_naive_fixture, attr, klass): @pytest.mark.parametrize("klass", [pd.Index, pd.TimedeltaIndex]) def test_constructor_dtypes_timedelta(self, attr, klass): index = pd.timedelta_range("1 days", periods=5) + index = index._with_freq(None) # wont be preserved by constructors dtype = index.dtype values = getattr(index, attr) diff --git a/pandas/tests/series/indexing/test_alter_index.py b/pandas/tests/series/indexing/test_alter_index.py index 558f10d967df6..0415434f01fcf 100644 --- a/pandas/tests/series/indexing/test_alter_index.py +++ b/pandas/tests/series/indexing/test_alter_index.py @@ -75,6 +75,7 @@ def test_reindex_with_datetimes(): result = ts.reindex(list(ts.index[5:10])) expected = ts[5:10] + expected.index = expected.index._with_freq(None) tm.assert_series_equal(result, expected) result = ts[list(ts.index[5:10])] @@ -91,6 +92,7 @@ def test_reindex_corner(datetime_series): # pass non-Index reindexed = datetime_series.reindex(list(datetime_series.index)) + datetime_series.index = datetime_series.index._with_freq(None) tm.assert_series_equal(datetime_series, reindexed) # bad fill method diff --git a/pandas/tests/series/methods/test_sort_index.py b/pandas/tests/series/methods/test_sort_index.py index d43b7efc779b0..39f872394d16b 100644 --- a/pandas/tests/series/methods/test_sort_index.py +++ b/pandas/tests/series/methods/test_sort_index.py @@ -13,6 +13,8 @@ def test_sort_index_name(self, datetime_series): assert result.name == datetime_series.name def test_sort_index(self, datetime_series): + datetime_series.index = datetime_series.index._with_freq(None) + rindex = list(datetime_series.index) random.shuffle(rindex) @@ -45,6 +47,7 @@ def test_sort_index(self, datetime_series): random_order.sort_index(level=0, axis=1) def test_sort_index_inplace(self, datetime_series): + datetime_series.index = datetime_series.index._with_freq(None) # For GH#11402 rindex = list(datetime_series.index) diff --git a/pandas/tests/series/test_datetime_values.py b/pandas/tests/series/test_datetime_values.py index e903e850ec36c..0d7fd0529dc1f 100644 --- a/pandas/tests/series/test_datetime_values.py +++ b/pandas/tests/series/test_datetime_values.py @@ -233,6 +233,8 @@ def get_dir(s): exp_values = pd.date_range( "2015-01-01", "2016-01-01", freq="T", tz="UTC" ).tz_convert("America/Chicago") + # freq not preserved by tz_localize above + exp_values = exp_values._with_freq(None) expected = Series(exp_values, name="xxx") tm.assert_series_equal(s, expected) diff --git a/pandas/tests/series/test_io.py b/pandas/tests/series/test_io.py index 510c11a51ca38..708118e950686 100644 --- a/pandas/tests/series/test_io.py +++ b/pandas/tests/series/test_io.py @@ -25,6 +25,8 @@ def read_csv(self, path, **kwargs): return out def test_from_csv(self, datetime_series, string_series): + # freq doesnt round-trip + datetime_series.index = datetime_series.index._with_freq(None) with tm.ensure_clean() as path: datetime_series.to_csv(path, header=False)