From 270a5e5442fc416d8d392edb39a3964cb8686b8f Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 24 Apr 2020 17:58:31 -0700 Subject: [PATCH 1/6] TST: more specific freq attrs --- .../arrays/categorical/test_constructors.py | 2 ++ pandas/tests/arrays/test_datetimelike.py | 6 +++++ pandas/tests/frame/methods/test_tz_convert.py | 6 +++++ .../tests/frame/test_axis_select_reindex.py | 3 ++- pandas/tests/frame/test_to_csv.py | 3 +++ pandas/tests/groupby/test_timegrouper.py | 26 ++++++++++++++----- 6 files changed, 38 insertions(+), 8 deletions(-) diff --git a/pandas/tests/arrays/categorical/test_constructors.py b/pandas/tests/arrays/categorical/test_constructors.py index 3e31c1acbe09d..485bb6ca72a3e 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._set_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._set_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 5b703cfe8fae5..b19f414859579 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._set_freq(None) tm.assert_index_equal(result, expected) + dta = dti._data + result = dta.round(freq="2T") + expected = expected._data + 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..5933ee3730738 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._set_freq(None) + l0_expected._set_freq(None) + l1._set_freq(None) + l0._set_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..5563df6b6e8ce 100644 --- a/pandas/tests/frame/test_to_csv.py +++ b/pandas/tests/frame/test_to_csv.py @@ -54,6 +54,7 @@ def test_to_csv_from_csv1(self, float_frame, datetime_frame): float_frame.to_csv(path, index=False) # test roundtrip + datetime_frame.index._set_freq(None) # freq does not roundtrip datetime_frame.to_csv(path) recons = self.read_csv(path) tm.assert_frame_equal(datetime_frame, recons) @@ -1157,6 +1158,7 @@ def test_to_csv_with_dst_transitions(self): ) for i in [times, times + pd.Timedelta("10s")]: + i._set_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 +1172,7 @@ 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._set_freq(None) # freq does not round-trip 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) From 6da3496f53669a38ebe917358528064c1deea49e Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 24 Apr 2020 18:07:15 -0700 Subject: [PATCH 2/6] TST: more specific freq attrs --- pandas/tests/series/indexing/test_alter_index.py | 2 ++ pandas/tests/series/methods/test_sort_index.py | 3 +++ pandas/tests/series/test_datetime_values.py | 1 + pandas/tests/series/test_io.py | 1 + 4 files changed, 7 insertions(+) diff --git a/pandas/tests/series/indexing/test_alter_index.py b/pandas/tests/series/indexing/test_alter_index.py index 558f10d967df6..6a6d7b233131b 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._set_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._set_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..3e3fccff43e6d 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._set_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._set_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..2081ac64dc7e4 100644 --- a/pandas/tests/series/test_datetime_values.py +++ b/pandas/tests/series/test_datetime_values.py @@ -233,6 +233,7 @@ def get_dir(s): exp_values = pd.date_range( "2015-01-01", "2016-01-01", freq="T", tz="UTC" ).tz_convert("America/Chicago") + exp_values._set_freq(None) # freq not preserved by tz_localize above 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..da8c35929ce43 100644 --- a/pandas/tests/series/test_io.py +++ b/pandas/tests/series/test_io.py @@ -25,6 +25,7 @@ def read_csv(self, path, **kwargs): return out def test_from_csv(self, datetime_series, string_series): + datetime_series.index._set_freq(None) # freq doesnt round-trip with tm.ensure_clean() as path: datetime_series.to_csv(path, header=False) From cb2d2416b8b34c6f1496234576b53d78bb90c78f Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 24 Apr 2020 18:14:21 -0700 Subject: [PATCH 3/6] TST: more specific freqs --- pandas/tests/indexes/datetimes/test_astype.py | 2 ++ pandas/tests/indexes/test_base.py | 1 + 2 files changed, 3 insertions(+) diff --git a/pandas/tests/indexes/datetimes/test_astype.py b/pandas/tests/indexes/datetimes/test_astype.py index a299f03c5ebad..48f3e90f4edee 100644 --- a/pandas/tests/indexes/datetimes/test_astype.py +++ b/pandas/tests/indexes/datetimes/test_astype.py @@ -92,8 +92,10 @@ def test_astype_with_tz(self): # GH 18951: tz-naive to tz-aware idx = date_range("20170101", periods=4) + idx._set_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._set_freq(None) tm.assert_index_equal(result, expected) def test_astype_str_compat(self): diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 1083f1c332705..63af509f34ef7 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._set_freq(None) # wont be preserved by constructors dtype = index.dtype values = getattr(index, attr) From 97a941fd19a5f371e9d8cd2a27c2c3e3e027004a Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 25 Apr 2020 14:08:09 -0700 Subject: [PATCH 4/6] update usage --- pandas/tests/arrays/categorical/test_constructors.py | 4 ++-- pandas/tests/arrays/test_datetimelike.py | 2 +- pandas/tests/frame/methods/test_tz_convert.py | 8 ++++---- pandas/tests/frame/test_to_csv.py | 7 ++++--- pandas/tests/indexes/datetimes/test_astype.py | 4 ++-- pandas/tests/indexes/test_base.py | 2 +- pandas/tests/series/indexing/test_alter_index.py | 4 ++-- pandas/tests/series/methods/test_sort_index.py | 4 ++-- pandas/tests/series/test_datetime_values.py | 3 ++- pandas/tests/series/test_io.py | 3 ++- 10 files changed, 22 insertions(+), 19 deletions(-) diff --git a/pandas/tests/arrays/categorical/test_constructors.py b/pandas/tests/arrays/categorical/test_constructors.py index 485bb6ca72a3e..9be741274c15a 100644 --- a/pandas/tests/arrays/categorical/test_constructors.py +++ b/pandas/tests/arrays/categorical/test_constructors.py @@ -331,7 +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._set_freq(None) # freq not preserved in result.categories + idx = idx._with_freq(None) # freq not preserved in result.categories result = Categorical(idx) tm.assert_index_equal(result.categories, idx) @@ -340,7 +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._set_freq(None) # freq not preserved in result.categories + 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 b19f414859579..f48d394b20a47 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -302,7 +302,7 @@ def test_round(self, tz_naive_fixture): result = dti.round(freq="2T") expected = dti - pd.Timedelta(minutes=1) - expected._set_freq(None) + expected = expected._with_freq(None) tm.assert_index_equal(result, expected) dta = dti._data diff --git a/pandas/tests/frame/methods/test_tz_convert.py b/pandas/tests/frame/methods/test_tz_convert.py index 5933ee3730738..d2ab7a386a92d 100644 --- a/pandas/tests/frame/methods/test_tz_convert.py +++ b/pandas/tests/frame/methods/test_tz_convert.py @@ -45,10 +45,10 @@ def test_tz_convert_and_localize(self, fn): df2 = DataFrame(np.ones(5), MultiIndex.from_arrays([l0, l1])) # freq is not preserved in MultiIndex construction - l1_expected._set_freq(None) - l0_expected._set_freq(None) - l1._set_freq(None) - l0._set_freq(None) + 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) diff --git a/pandas/tests/frame/test_to_csv.py b/pandas/tests/frame/test_to_csv.py index 5563df6b6e8ce..c064b2ccebd1c 100644 --- a/pandas/tests/frame/test_to_csv.py +++ b/pandas/tests/frame/test_to_csv.py @@ -54,7 +54,8 @@ def test_to_csv_from_csv1(self, float_frame, datetime_frame): float_frame.to_csv(path, index=False) # test roundtrip - datetime_frame.index._set_freq(None) # freq does not 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) @@ -1158,7 +1159,7 @@ def test_to_csv_with_dst_transitions(self): ) for i in [times, times + pd.Timedelta("10s")]: - i._set_freq(None) # freq is not preserved by read_csv + 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) @@ -1172,7 +1173,7 @@ 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._set_freq(None) # freq does not round-trip + idx = idx._with_freq(None) # freq does not round-trip 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/indexes/datetimes/test_astype.py b/pandas/tests/indexes/datetimes/test_astype.py index 179dd175ddc3d..3e7e76bba0dde 100644 --- a/pandas/tests/indexes/datetimes/test_astype.py +++ b/pandas/tests/indexes/datetimes/test_astype.py @@ -75,10 +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._set_freq(None) # tz_localize does not preserve freq + 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._set_freq(None) + 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 63af509f34ef7..7b42e9646918e 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -383,7 +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._set_freq(None) # wont be preserved by constructors + 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 6a6d7b233131b..0415434f01fcf 100644 --- a/pandas/tests/series/indexing/test_alter_index.py +++ b/pandas/tests/series/indexing/test_alter_index.py @@ -75,7 +75,7 @@ def test_reindex_with_datetimes(): result = ts.reindex(list(ts.index[5:10])) expected = ts[5:10] - expected.index._set_freq(None) + expected.index = expected.index._with_freq(None) tm.assert_series_equal(result, expected) result = ts[list(ts.index[5:10])] @@ -92,7 +92,7 @@ def test_reindex_corner(datetime_series): # pass non-Index reindexed = datetime_series.reindex(list(datetime_series.index)) - datetime_series.index._set_freq(None) + 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 3e3fccff43e6d..39f872394d16b 100644 --- a/pandas/tests/series/methods/test_sort_index.py +++ b/pandas/tests/series/methods/test_sort_index.py @@ -13,7 +13,7 @@ def test_sort_index_name(self, datetime_series): assert result.name == datetime_series.name def test_sort_index(self, datetime_series): - datetime_series.index._set_freq(None) + datetime_series.index = datetime_series.index._with_freq(None) rindex = list(datetime_series.index) random.shuffle(rindex) @@ -47,7 +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._set_freq(None) + 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 2081ac64dc7e4..0d7fd0529dc1f 100644 --- a/pandas/tests/series/test_datetime_values.py +++ b/pandas/tests/series/test_datetime_values.py @@ -233,7 +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") - exp_values._set_freq(None) # freq not preserved by tz_localize above + # 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 da8c35929ce43..708118e950686 100644 --- a/pandas/tests/series/test_io.py +++ b/pandas/tests/series/test_io.py @@ -25,7 +25,8 @@ def read_csv(self, path, **kwargs): return out def test_from_csv(self, datetime_series, string_series): - datetime_series.index._set_freq(None) # freq doesnt round-trip + # 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) From 44cb276372601f83977f7f7b1d3be4aabc353c47 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 25 Apr 2020 16:40:02 -0700 Subject: [PATCH 5/6] freq update --- pandas/tests/arrays/test_datetimelike.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index c36a2fe9d37fe..580c0c7a8cab5 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -307,7 +307,7 @@ def test_round(self, tz_naive_fixture): dta = dti._data result = dta.round(freq="2T") - expected = expected._data + expected = expected._data._with_freq(None) tm.assert_datetime_array_equal(result, expected) def test_array_interface(self, datetime_index): From 42cdcd2ab1f645e957c84514d778e982fff0bdd8 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 25 Apr 2020 16:42:13 -0700 Subject: [PATCH 6/6] post-merge compat --- pandas/tests/frame/test_to_csv.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/frame/test_to_csv.py b/pandas/tests/frame/test_to_csv.py index c064b2ccebd1c..9c656dd69abe2 100644 --- a/pandas/tests/frame/test_to_csv.py +++ b/pandas/tests/frame/test_to_csv.py @@ -1174,6 +1174,7 @@ 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)