From 30bf86fe5463b07a7749044beb404000996fb39c Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 23 Apr 2020 15:34:50 -0700 Subject: [PATCH 1/3] TST: more accurate freq attributes --- pandas/tests/arithmetic/test_datetime64.py | 4 ++++ pandas/tests/arithmetic/test_numeric.py | 4 ++-- pandas/tests/arithmetic/test_timedelta64.py | 10 +++++++--- pandas/tests/indexes/timedeltas/test_ops.py | 3 ++- pandas/tests/indexing/test_partial.py | 2 +- pandas/tests/series/methods/test_sort_index.py | 10 ++++++---- pandas/tests/series/methods/test_to_dict.py | 8 +++++--- 7 files changed, 27 insertions(+), 14 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index a8e9ad9ff7cc9..79fcb5e9478c3 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -2052,6 +2052,7 @@ def test_dti_add_tdi(self, tz_naive_fixture): dti = DatetimeIndex([Timestamp("2017-01-01", tz=tz)] * 10) tdi = pd.timedelta_range("0 days", periods=10) expected = pd.date_range("2017-01-01", periods=10, tz=tz) + expected._set_freq(None) # add with TimdeltaIndex result = dti + tdi @@ -2073,6 +2074,7 @@ def test_dti_iadd_tdi(self, tz_naive_fixture): dti = DatetimeIndex([Timestamp("2017-01-01", tz=tz)] * 10) tdi = pd.timedelta_range("0 days", periods=10) expected = pd.date_range("2017-01-01", periods=10, tz=tz) + expected._set_freq(None) # iadd with TimdeltaIndex result = DatetimeIndex([Timestamp("2017-01-01", tz=tz)] * 10) @@ -2098,6 +2100,7 @@ def test_dti_sub_tdi(self, tz_naive_fixture): dti = DatetimeIndex([Timestamp("2017-01-01", tz=tz)] * 10) tdi = pd.timedelta_range("0 days", periods=10) expected = pd.date_range("2017-01-01", periods=10, tz=tz, freq="-1D") + expected = expected._with_freq(None) # sub with TimedeltaIndex result = dti - tdi @@ -2121,6 +2124,7 @@ def test_dti_isub_tdi(self, tz_naive_fixture): dti = DatetimeIndex([Timestamp("2017-01-01", tz=tz)] * 10) tdi = pd.timedelta_range("0 days", periods=10) expected = pd.date_range("2017-01-01", periods=10, tz=tz, freq="-1D") + expected = expected._with_freq(None) # isub with TimedeltaIndex result = DatetimeIndex([Timestamp("2017-01-01", tz=tz)] * 10) diff --git a/pandas/tests/arithmetic/test_numeric.py b/pandas/tests/arithmetic/test_numeric.py index 202e30287881f..0675ba874846b 100644 --- a/pandas/tests/arithmetic/test_numeric.py +++ b/pandas/tests/arithmetic/test_numeric.py @@ -165,7 +165,7 @@ def test_numeric_arr_mul_tdscalar(self, scalar_td, numeric_idx, box): # GH#19333 index = numeric_idx - expected = pd.timedelta_range("0 days", "4 days") + expected = pd.TimedeltaIndex([pd.Timedelta(days=n) for n in range(5)]) index = tm.box_expected(index, box) expected = tm.box_expected(expected, box) @@ -974,7 +974,7 @@ def check(series, other): tm.assert_almost_equal(np.asarray(result), expected) assert result.name == series.name - tm.assert_index_equal(result.index, series.index) + tm.assert_index_equal(result.index, series.index._with_freq(None)) tser = tm.makeTimeSeries().rename("ts") check(tser, tser * 2) diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index 8387e4d708662..3ffdc87ff84c8 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -403,9 +403,7 @@ def _check(result, expected): _check(result, expected) result = dti_tz - td - expected = DatetimeIndex( - ["20121231", "20130101", "20130102"], tz="US/Eastern", freq="D" - ) + expected = DatetimeIndex(["20121231", "20130101", "20130102"], tz="US/Eastern") tm.assert_index_equal(result, expected) def test_dti_tdi_numeric_ops(self): @@ -515,7 +513,13 @@ def test_timedelta(self, freq): result2 = DatetimeIndex(s - np.timedelta64(100000000)) result3 = rng - np.timedelta64(100000000) result4 = DatetimeIndex(s - pd.offsets.Hour(1)) + + assert result1.freq == rng.freq + result1 = result1._with_freq(None) tm.assert_index_equal(result1, result4) + + assert result3.freq == rng.freq + result3 = result3._with_freq(None) tm.assert_index_equal(result2, result3) def test_tda_add_sub_index(self): diff --git a/pandas/tests/indexes/timedeltas/test_ops.py b/pandas/tests/indexes/timedeltas/test_ops.py index aa1bf997fc66b..0e5abe2f5ccd1 100644 --- a/pandas/tests/indexes/timedeltas/test_ops.py +++ b/pandas/tests/indexes/timedeltas/test_ops.py @@ -144,7 +144,8 @@ def test_drop_duplicates_metadata(self, freq_sample): idx_dup = idx.append(idx) assert idx_dup.freq is None # freq is reset result = idx_dup.drop_duplicates() - tm.assert_index_equal(idx, result) + expected = idx._with_freq(None) + tm.assert_index_equal(expected, result) assert result.freq is None @pytest.mark.parametrize( diff --git a/pandas/tests/indexing/test_partial.py b/pandas/tests/indexing/test_partial.py index 2e691c6fd76d8..6c259db33cf7c 100644 --- a/pandas/tests/indexing/test_partial.py +++ b/pandas/tests/indexing/test_partial.py @@ -119,7 +119,7 @@ def test_partial_setting(self): ) expected = pd.concat( - [df_orig, DataFrame({"A": 7}, index=[dates[-1] + dates.freq])], sort=True + [df_orig, DataFrame({"A": 7}, index=dates[-1:] + dates.freq)], sort=True ) df = df_orig.copy() df.loc[dates[-1] + dates.freq, "A"] = 7 diff --git a/pandas/tests/series/methods/test_sort_index.py b/pandas/tests/series/methods/test_sort_index.py index 2d4fdfd5a3950..47aa72e298685 100644 --- a/pandas/tests/series/methods/test_sort_index.py +++ b/pandas/tests/series/methods/test_sort_index.py @@ -55,16 +55,18 @@ def test_sort_index_inplace(self, datetime_series): result = random_order.sort_index(ascending=False, inplace=True) assert result is None - tm.assert_series_equal( - random_order, datetime_series.reindex(datetime_series.index[::-1]) - ) + expected = datetime_series.reindex(datetime_series.index[::-1]) + expected.index = expected.index._with_freq(None) + tm.assert_series_equal(random_order, expected) # ascending random_order = datetime_series.reindex(rindex) result = random_order.sort_index(ascending=True, inplace=True) assert result is None - tm.assert_series_equal(random_order, datetime_series) + expected = datetime_series.copy() + expected.index = expected.index._with_freq(None) + tm.assert_series_equal(random_order, expected) def test_sort_index_level(self): mi = MultiIndex.from_tuples([[1, 1, 3], [1, 1, 1]], names=list("ABC")) diff --git a/pandas/tests/series/methods/test_to_dict.py b/pandas/tests/series/methods/test_to_dict.py index 2fbf3e8d39cf3..47badb0a1bb52 100644 --- a/pandas/tests/series/methods/test_to_dict.py +++ b/pandas/tests/series/methods/test_to_dict.py @@ -12,9 +12,11 @@ class TestSeriesToDict: ) def test_to_dict(self, mapping, datetime_series): # GH#16122 - tm.assert_series_equal( - Series(datetime_series.to_dict(mapping), name="ts"), datetime_series - ) + result = Series(datetime_series.to_dict(mapping), name="ts") + expected = datetime_series.copy() + expected.index = expected.index._with_freq(None) + tm.assert_series_equal(result, expected) + from_method = Series(datetime_series.to_dict(collections.Counter)) from_constructor = Series(collections.Counter(datetime_series.items())) tm.assert_series_equal(from_method, from_constructor) From 549fe587e121ea576d5f1a775c2e8f2e0af4f5d4 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 23 Apr 2020 16:05:29 -0700 Subject: [PATCH 2/3] more precise test --- pandas/tests/indexing/test_datetime.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexing/test_datetime.py b/pandas/tests/indexing/test_datetime.py index c8c2d1ed587cf..17ca23055f6e0 100644 --- a/pandas/tests/indexing/test_datetime.py +++ b/pandas/tests/indexing/test_datetime.py @@ -145,7 +145,11 @@ def test_indexing_with_datetimeindex_tz(self): for sel in (index, list(index)): # getitem - tm.assert_series_equal(ser[sel], ser) + result = ser[sel] + expected = ser + if sel is not index: + expected.index = expected.index._with_freq(None) + tm.assert_series_equal(result, expected) # setitem result = ser.copy() @@ -154,7 +158,8 @@ def test_indexing_with_datetimeindex_tz(self): tm.assert_series_equal(result, expected) # .loc getitem - tm.assert_series_equal(ser.loc[sel], ser) + result = ser.loc[sel] + tm.assert_series_equal(result, ser) # .loc setitem result = ser.copy() @@ -226,6 +231,7 @@ def test_series_partial_set_datetime(self): result = ser.loc[[Timestamp("2011-01-01"), Timestamp("2011-01-02")]] exp = Series([0.1, 0.2], index=idx, name="s") + exp.index = exp.index._with_freq(None) tm.assert_series_equal(result, exp, check_index_type=True) keys = [ From 16772ea847118d0851097c3e8cd54070d3171b7a Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 24 Apr 2020 10:57:32 -0700 Subject: [PATCH 3/3] TST: more accurate freq attrs --- pandas/tests/indexes/common.py | 2 ++ .../tests/indexes/datetimes/test_datetime.py | 3 +- pandas/tests/indexes/datetimes/test_ops.py | 6 +++- pandas/tests/indexes/datetimes/test_setops.py | 5 +-- .../tests/indexes/datetimes/test_timezones.py | 36 +++++++++++-------- pandas/tests/test_multilevel.py | 1 + 6 files changed, 34 insertions(+), 19 deletions(-) diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index c0e1eeb8f4eec..957ca138498d9 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -266,6 +266,8 @@ def test_ensure_copied_data(self, indices): result = index_type(indices.values, copy=True, **init_kwargs) if is_datetime64tz_dtype(indices.dtype): result = result.tz_localize("UTC").tz_convert(indices.tz) + if isinstance(indices, (DatetimeIndex, TimedeltaIndex)): + indices._set_freq(None) tm.assert_index_equal(indices, result) diff --git a/pandas/tests/indexes/datetimes/test_datetime.py b/pandas/tests/indexes/datetimes/test_datetime.py index e109c7a4f1c8d..08706dce7e1e0 100644 --- a/pandas/tests/indexes/datetimes/test_datetime.py +++ b/pandas/tests/indexes/datetimes/test_datetime.py @@ -368,7 +368,8 @@ def test_factorize_tz(self, tz_naive_fixture): for obj in [idx, pd.Series(idx)]: arr, res = obj.factorize() tm.assert_numpy_array_equal(arr, exp_arr) - tm.assert_index_equal(res, base) + expected = base._with_freq(None) + tm.assert_index_equal(res, expected) def test_factorize_dst(self): # GH 13750 diff --git a/pandas/tests/indexes/datetimes/test_ops.py b/pandas/tests/indexes/datetimes/test_ops.py index c55b0481c1041..f0fe5e9b293fc 100644 --- a/pandas/tests/indexes/datetimes/test_ops.py +++ b/pandas/tests/indexes/datetimes/test_ops.py @@ -134,11 +134,14 @@ def test_value_counts_unique(self, tz_naive_fixture): exp_idx = pd.date_range("2011-01-01 18:00", freq="-1H", periods=10, tz=tz) expected = Series(range(10, 0, -1), index=exp_idx, dtype="int64") + expected.index._set_freq(None) for obj in [idx, Series(idx)]: + tm.assert_series_equal(obj.value_counts(), expected) expected = pd.date_range("2011-01-01 09:00", freq="H", periods=10, tz=tz) + expected = expected._with_freq(None) tm.assert_index_equal(idx.unique(), expected) idx = DatetimeIndex( @@ -274,7 +277,8 @@ def test_drop_duplicates_metadata(self, freq_sample): idx_dup = idx.append(idx) assert idx_dup.freq is None # freq is reset result = idx_dup.drop_duplicates() - tm.assert_index_equal(idx, result) + expected = idx._with_freq(None) + tm.assert_index_equal(result, expected) assert result.freq is None @pytest.mark.parametrize( diff --git a/pandas/tests/indexes/datetimes/test_setops.py b/pandas/tests/indexes/datetimes/test_setops.py index 7182d05b77be3..df6e2dac72f95 100644 --- a/pandas/tests/indexes/datetimes/test_setops.py +++ b/pandas/tests/indexes/datetimes/test_setops.py @@ -285,16 +285,17 @@ def test_intersection_empty(self, tz_aware_fixture, freq): assert result.freq == rng.freq # no overlap GH#33604 + check_freq = freq != "T" # We don't preserve freq on non-anchored offsets result = rng[:3].intersection(rng[-3:]) tm.assert_index_equal(result, rng[:0]) - if freq != "T": + if check_freq: # We don't preserve freq on non-anchored offsets assert result.freq == rng.freq # swapped left and right result = rng[-3:].intersection(rng[:3]) tm.assert_index_equal(result, rng[:0]) - if freq != "T": + if check_freq: # We don't preserve freq on non-anchored offsets assert result.freq == rng.freq diff --git a/pandas/tests/indexes/datetimes/test_timezones.py b/pandas/tests/indexes/datetimes/test_timezones.py index 8628ce7ade212..ea68e8759c123 100644 --- a/pandas/tests/indexes/datetimes/test_timezones.py +++ b/pandas/tests/indexes/datetimes/test_timezones.py @@ -271,6 +271,7 @@ def test_tz_convert_roundtrip(self, tz_aware_fixture): tm.assert_index_equal(reset, expected) assert reset.tzinfo is None expected = converted.tz_convert("UTC").tz_localize(None) + expected = expected._with_freq("infer") tm.assert_index_equal(reset, expected) def test_dti_tz_convert_tzlocal(self): @@ -352,8 +353,9 @@ def test_dti_tz_localize_ambiguous_infer(self, tz): ] di = DatetimeIndex(times) localized = di.tz_localize(tz, ambiguous="infer") - tm.assert_index_equal(dr, localized) - tm.assert_index_equal(dr, DatetimeIndex(times, tz=tz, ambiguous="infer")) + expected = dr._with_freq(None) + tm.assert_index_equal(expected, localized) + tm.assert_index_equal(expected, DatetimeIndex(times, tz=tz, ambiguous="infer")) # When there is no dst transition, nothing special happens dr = date_range(datetime(2011, 6, 1, 0), periods=10, freq=pd.offsets.Hour()) @@ -458,7 +460,8 @@ def test_dti_tz_localize_roundtrip(self, tz_aware_fixture): localized.tz_localize(tz) reset = localized.tz_localize(None) assert reset.tzinfo is None - tm.assert_index_equal(reset, idx) + expected = idx._with_freq(None) + tm.assert_index_equal(reset, expected) def test_dti_tz_localize_naive(self): rng = date_range("1/1/2011", periods=100, freq="H") @@ -466,7 +469,7 @@ def test_dti_tz_localize_naive(self): conv = rng.tz_localize("US/Pacific") exp = date_range("1/1/2011", periods=100, freq="H", tz="US/Pacific") - tm.assert_index_equal(conv, exp) + tm.assert_index_equal(conv, exp._with_freq(None)) def test_dti_tz_localize_tzlocal(self): # GH#13583 @@ -526,8 +529,9 @@ def test_dti_tz_localize_ambiguous_flags(self, tz): di = DatetimeIndex(times) is_dst = [1, 1, 0, 0, 0] localized = di.tz_localize(tz, ambiguous=is_dst) - tm.assert_index_equal(dr, localized) - tm.assert_index_equal(dr, DatetimeIndex(times, tz=tz, ambiguous=is_dst)) + expected = dr._with_freq(None) + tm.assert_index_equal(expected, localized) + tm.assert_index_equal(expected, DatetimeIndex(times, tz=tz, ambiguous=is_dst)) localized = di.tz_localize(tz, ambiguous=np.array(is_dst)) tm.assert_index_equal(dr, localized) @@ -703,9 +707,9 @@ def test_dti_tz_localize_nonexistent_shift_invalid(self, offset, tz_type): def test_normalize_tz(self): rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz="US/Eastern") - result = rng.normalize() + result = rng.normalize() # does not preserve freq expected = date_range("1/1/2000", periods=10, freq="D", tz="US/Eastern") - tm.assert_index_equal(result, expected) + tm.assert_index_equal(result, expected._with_freq(None)) assert result.is_normalized assert not rng.is_normalized @@ -720,9 +724,9 @@ def test_normalize_tz(self): assert not rng.is_normalized rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz=tzlocal()) - result = rng.normalize() + result = rng.normalize() # does not preserve freq expected = date_range("1/1/2000", periods=10, freq="D", tz=tzlocal()) - tm.assert_index_equal(result, expected) + tm.assert_index_equal(result, expected._with_freq(None)) assert result.is_normalized assert not rng.is_normalized @@ -746,6 +750,7 @@ def test_normalize_tz_local(self, timezone): result = rng.normalize() expected = date_range("1/1/2000", periods=10, freq="D", tz=tzlocal()) + expected = expected._with_freq(None) tm.assert_index_equal(result, expected) assert result.is_normalized @@ -777,10 +782,8 @@ def test_dti_constructor_with_fixed_tz(self): @pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"]) def test_dti_convert_datetime_list(self, tzstr): dr = date_range("2012-06-02", periods=10, tz=tzstr, name="foo") - dr2 = DatetimeIndex(list(dr), name="foo") + dr2 = DatetimeIndex(list(dr), name="foo", freq="D") tm.assert_index_equal(dr, dr2) - assert dr.tz == dr2.tz - assert dr2.name == "foo" def test_dti_construction_univalent(self): rng = date_range("03/12/2012 00:00", periods=10, freq="W-FRI", tz="US/Eastern") @@ -803,6 +806,7 @@ def test_dti_tz_constructors(self, tzstr): idx1 = to_datetime(arr).tz_localize(tzstr) idx2 = pd.date_range(start="2005-11-10 08:00:00", freq="H", periods=2, tz=tzstr) + idx2 = idx2._with_freq(None) # the others all have freq=None idx3 = DatetimeIndex(arr, tz=tzstr) idx4 = DatetimeIndex(np.array(arr), tz=tzstr) @@ -913,7 +917,7 @@ def test_date_range_localize(self): rng3 = date_range("3/11/2012 03:00", periods=15, freq="H") rng3 = rng3.tz_localize("US/Eastern") - tm.assert_index_equal(rng, rng3) + tm.assert_index_equal(rng._with_freq(None), rng3) # DST transition time val = rng[0] @@ -926,7 +930,9 @@ def test_date_range_localize(self): # Right before the DST transition rng = date_range("3/11/2012 00:00", periods=2, freq="H", tz="US/Eastern") - rng2 = DatetimeIndex(["3/11/2012 00:00", "3/11/2012 01:00"], tz="US/Eastern") + rng2 = DatetimeIndex( + ["3/11/2012 00:00", "3/11/2012 01:00"], tz="US/Eastern", freq="H" + ) tm.assert_index_equal(rng, rng2) exp = Timestamp("3/11/2012 00:00", tz="US/Eastern") assert exp.hour == 0 diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py index dd0bac683c35c..f113d47ec7340 100644 --- a/pandas/tests/test_multilevel.py +++ b/pandas/tests/test_multilevel.py @@ -1505,6 +1505,7 @@ def test_set_index_datetime(self): tz="US/Eastern", ) idx3 = pd.date_range("2011-01-01 09:00", periods=6, tz="Asia/Tokyo") + idx3._set_freq(None) df = df.set_index(idx1) df = df.set_index(idx2, append=True)