diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index 5a5abcc4aa85d..5996d8797d143 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -853,10 +853,14 @@ def test_concat_same_type_invalid(self, arr1d): with pytest.raises(ValueError, match="to_concat must have the same"): arr._concat_same_type([arr, other]) - def test_concat_same_type_different_freq(self): + def test_concat_same_type_different_freq(self, unit): # we *can* concatenate DTI with different freqs. - a = DatetimeArray(pd.date_range("2000", periods=2, freq="D", tz="US/Central")) - b = DatetimeArray(pd.date_range("2000", periods=2, freq="h", tz="US/Central")) + a = DatetimeArray( + pd.date_range("2000", periods=2, freq="D", tz="US/Central", unit=unit) + ) + b = DatetimeArray( + pd.date_range("2000", periods=2, freq="h", tz="US/Central", unit=unit) + ) result = DatetimeArray._concat_same_type([a, b]) expected = DatetimeArray( pd.to_datetime( @@ -866,7 +870,9 @@ def test_concat_same_type_different_freq(self): "2000-01-01 00:00:00", "2000-01-01 01:00:00", ] - ).tz_localize("US/Central") + ) + .tz_localize("US/Central") + .as_unit(unit) ) tm.assert_datetime_array_equal(result, expected) diff --git a/pandas/tests/groupby/methods/test_nth.py b/pandas/tests/groupby/methods/test_nth.py index b21060e7f1247..e3005c9b971ec 100644 --- a/pandas/tests/groupby/methods/test_nth.py +++ b/pandas/tests/groupby/methods/test_nth.py @@ -286,8 +286,10 @@ def test_nth5(): tm.assert_frame_equal(gb.nth([3, 4]), df.loc[[]]) -def test_nth_bdays(): - business_dates = pd.date_range(start="4/1/2014", end="6/30/2014", freq="B") +def test_nth_bdays(unit): + business_dates = pd.date_range( + start="4/1/2014", end="6/30/2014", freq="B", unit=unit + ) df = DataFrame(1, index=business_dates, columns=["a", "b"]) # get the first, fourth and last two business days for each month key = [df.index.year, df.index.month] @@ -307,7 +309,7 @@ def test_nth_bdays(): "2014/6/27", "2014/6/30", ] - ) + ).as_unit(unit) expected = DataFrame(1, columns=["a", "b"], index=expected_dates) tm.assert_frame_equal(result, expected) @@ -401,14 +403,15 @@ def test_first_last_tz(data, expected_first, expected_last): ["last", Timestamp("2013-01-02", tz="US/Eastern"), "b"], ], ) -def test_first_last_tz_multi_column(method, ts, alpha): +def test_first_last_tz_multi_column(method, ts, alpha, unit): # GH 21603 category_string = Series(list("abc")).astype("category") + dti = pd.date_range("20130101", periods=3, tz="US/Eastern", unit=unit) df = DataFrame( { "group": [1, 1, 2], "category_string": category_string, - "datetimetz": pd.date_range("20130101", periods=3, tz="US/Eastern"), + "datetimetz": dti, } ) result = getattr(df.groupby("group"), method)() @@ -421,6 +424,7 @@ def test_first_last_tz_multi_column(method, ts, alpha): }, index=Index([1, 2], name="group"), ) + expected["datetimetz"] = expected["datetimetz"].dt.as_unit(unit) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/groupby/methods/test_quantile.py b/pandas/tests/groupby/methods/test_quantile.py index fcb9701e9881b..d949e5e36fa81 100644 --- a/pandas/tests/groupby/methods/test_quantile.py +++ b/pandas/tests/groupby/methods/test_quantile.py @@ -415,13 +415,14 @@ def test_columns_groupby_quantile(): tm.assert_frame_equal(result, expected) -def test_timestamp_groupby_quantile(): +def test_timestamp_groupby_quantile(unit): # GH 33168 + dti = pd.date_range( + start="2020-04-19 00:00:00", freq="1min", periods=100, tz="UTC", unit=unit + ).floor("1h") df = DataFrame( { - "timestamp": pd.date_range( - start="2020-04-19 00:00:00", freq="1min", periods=100, tz="UTC" - ).floor("1h"), + "timestamp": dti, "category": list(range(1, 101)), "value": list(range(101, 201)), } @@ -429,6 +430,7 @@ def test_timestamp_groupby_quantile(): result = df.groupby("timestamp").quantile([0.2, 0.8]) + mi = pd.MultiIndex.from_product([dti[::99], [0.2, 0.8]], names=("timestamp", None)) expected = DataFrame( [ {"category": 12.8, "value": 112.8}, @@ -436,15 +438,7 @@ def test_timestamp_groupby_quantile(): {"category": 68.8, "value": 168.8}, {"category": 92.2, "value": 192.2}, ], - index=pd.MultiIndex.from_tuples( - [ - (pd.Timestamp("2020-04-19 00:00:00+00:00"), 0.2), - (pd.Timestamp("2020-04-19 00:00:00+00:00"), 0.8), - (pd.Timestamp("2020-04-19 01:00:00+00:00"), 0.2), - (pd.Timestamp("2020-04-19 01:00:00+00:00"), 0.8), - ], - names=("timestamp", None), - ), + index=mi, ) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/groupby/methods/test_value_counts.py b/pandas/tests/groupby/methods/test_value_counts.py index b82908ef2aa21..200fa5fd4367d 100644 --- a/pandas/tests/groupby/methods/test_value_counts.py +++ b/pandas/tests/groupby/methods/test_value_counts.py @@ -1135,7 +1135,7 @@ def test_subset_duplicate_columns(): @pytest.mark.parametrize("utc", [True, False]) -def test_value_counts_time_grouper(utc): +def test_value_counts_time_grouper(utc, unit): # GH#50486 df = DataFrame( { @@ -1152,12 +1152,12 @@ def test_value_counts_time_grouper(utc): } ).drop([3]) - df["Datetime"] = to_datetime(df["Timestamp"], utc=utc, unit="s") + df["Datetime"] = to_datetime(df["Timestamp"], utc=utc, unit="s").dt.as_unit(unit) gb = df.groupby(Grouper(freq="1D", key="Datetime")) result = gb.value_counts() dates = to_datetime( ["2019-08-06", "2019-08-07", "2019-08-09", "2019-08-10"], utc=utc - ) + ).as_unit(unit) timestamps = df["Timestamp"].unique() index = MultiIndex( levels=[dates, timestamps, ["apple", "banana", "orange", "pear"]], diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index de8ceb30b565b..5eb894f4eabdf 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -3172,28 +3172,32 @@ def test_groupby_selection_other_methods(df): ) -def test_groupby_with_Time_Grouper(): - idx2 = [ - to_datetime("2016-08-31 22:08:12.000"), - to_datetime("2016-08-31 22:09:12.200"), - to_datetime("2016-08-31 22:20:12.400"), - ] +def test_groupby_with_Time_Grouper(unit): + idx2 = to_datetime( + [ + "2016-08-31 22:08:12.000", + "2016-08-31 22:09:12.200", + "2016-08-31 22:20:12.400", + ] + ).as_unit(unit) test_data = DataFrame( {"quant": [1.0, 1.0, 3.0], "quant2": [1.0, 1.0, 3.0], "time2": idx2} ) + time2 = date_range("2016-08-31 22:08:00", periods=13, freq="1min", unit=unit) expected_output = DataFrame( { - "time2": date_range("2016-08-31 22:08:00", periods=13, freq="1min"), + "time2": time2, "quant": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], "quant2": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], } ) - df = test_data.groupby(Grouper(key="time2", freq="1min")).count().reset_index() + gb = test_data.groupby(Grouper(key="time2", freq="1min")) + result = gb.count().reset_index() - tm.assert_frame_equal(df, expected_output) + tm.assert_frame_equal(result, expected_output) def test_groupby_series_with_datetimeindex_month_name(): diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index 01768582299eb..3e52476be9dbd 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -276,19 +276,24 @@ def test_grouper_creation_bug2(self): result = g.sum() tm.assert_frame_equal(result, expected) - def test_grouper_creation_bug3(self): + def test_grouper_creation_bug3(self, unit): # GH8866 + dti = date_range("20130101", periods=2, unit=unit) + mi = MultiIndex.from_product( + [list("ab"), range(2), dti], + names=["one", "two", "three"], + ) ser = Series( np.arange(8, dtype="int64"), - index=MultiIndex.from_product( - [list("ab"), range(2), date_range("20130101", periods=2)], - names=["one", "two", "three"], - ), + index=mi, ) result = ser.groupby(Grouper(level="three", freq="ME")).sum() + exp_dti = pd.DatetimeIndex( + [Timestamp("2013-01-31")], freq="ME", name="three" + ).as_unit(unit) expected = Series( [28], - index=pd.DatetimeIndex([Timestamp("2013-01-31")], freq="ME", name="three"), + index=exp_dti, ) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/indexes/datetimes/methods/test_delete.py b/pandas/tests/indexes/datetimes/methods/test_delete.py index 0fdd60c14474e..2341499977f22 100644 --- a/pandas/tests/indexes/datetimes/methods/test_delete.py +++ b/pandas/tests/indexes/datetimes/methods/test_delete.py @@ -116,18 +116,17 @@ def test_delete_slice(self, unit): # TODO: belongs in Series.drop tests? @pytest.mark.parametrize("tz", [None, "Asia/Tokyo", "US/Pacific"]) - def test_delete_slice2(self, tz): + def test_delete_slice2(self, tz, unit): + dti = date_range( + "2000-01-01 09:00", periods=10, freq="h", name="idx", tz=tz, unit=unit + ) ts = Series( 1, - index=date_range( - "2000-01-01 09:00", periods=10, freq="h", name="idx", tz=tz - ), + index=dti, ) # preserve freq result = ts.drop(ts.index[:5]).index - expected = date_range( - "2000-01-01 14:00", periods=5, freq="h", name="idx", tz=tz - ) + expected = dti[5:] tm.assert_index_equal(result, expected) assert result.name == expected.name assert result.freq == expected.freq @@ -135,18 +134,7 @@ def test_delete_slice2(self, tz): # reset freq to None result = ts.drop(ts.index[[1, 3, 5, 7, 9]]).index - expected = DatetimeIndex( - [ - "2000-01-01 09:00", - "2000-01-01 11:00", - "2000-01-01 13:00", - "2000-01-01 15:00", - "2000-01-01 17:00", - ], - freq=None, - name="idx", - tz=tz, - ) + expected = dti[::2]._with_freq(None) tm.assert_index_equal(result, expected) assert result.name == expected.name assert result.freq == expected.freq diff --git a/pandas/tests/indexes/datetimes/methods/test_repeat.py b/pandas/tests/indexes/datetimes/methods/test_repeat.py index cf4749aedd5a7..92501755f8c5b 100644 --- a/pandas/tests/indexes/datetimes/methods/test_repeat.py +++ b/pandas/tests/indexes/datetimes/methods/test_repeat.py @@ -17,29 +17,29 @@ def test_repeat_range(self, tz_naive_fixture): assert result.freq is None assert len(result) == 5 * len(rng) - def test_repeat_range2(self, tz_naive_fixture): + def test_repeat_range2(self, tz_naive_fixture, unit): tz = tz_naive_fixture - index = date_range("2001-01-01", periods=2, freq="D", tz=tz) + index = date_range("2001-01-01", periods=2, freq="D", tz=tz, unit=unit) exp = DatetimeIndex( ["2001-01-01", "2001-01-01", "2001-01-02", "2001-01-02"], tz=tz - ) + ).as_unit(unit) for res in [index.repeat(2), np.repeat(index, 2)]: tm.assert_index_equal(res, exp) assert res.freq is None - def test_repeat_range3(self, tz_naive_fixture): + def test_repeat_range3(self, tz_naive_fixture, unit): tz = tz_naive_fixture - index = date_range("2001-01-01", periods=2, freq="2D", tz=tz) + index = date_range("2001-01-01", periods=2, freq="2D", tz=tz, unit=unit) exp = DatetimeIndex( ["2001-01-01", "2001-01-01", "2001-01-03", "2001-01-03"], tz=tz - ) + ).as_unit(unit) for res in [index.repeat(2), np.repeat(index, 2)]: tm.assert_index_equal(res, exp) assert res.freq is None - def test_repeat_range4(self, tz_naive_fixture): + def test_repeat_range4(self, tz_naive_fixture, unit): tz = tz_naive_fixture - index = DatetimeIndex(["2001-01-01", "NaT", "2003-01-01"], tz=tz) + index = DatetimeIndex(["2001-01-01", "NaT", "2003-01-01"], tz=tz).as_unit(unit) exp = DatetimeIndex( [ "2001-01-01", @@ -53,17 +53,17 @@ def test_repeat_range4(self, tz_naive_fixture): "2003-01-01", ], tz=tz, - ) + ).as_unit(unit) for res in [index.repeat(3), np.repeat(index, 3)]: tm.assert_index_equal(res, exp) assert res.freq is None - def test_repeat(self, tz_naive_fixture): + def test_repeat(self, tz_naive_fixture, unit): tz = tz_naive_fixture reps = 2 msg = "the 'axis' parameter is not supported" - rng = date_range(start="2016-01-01", periods=2, freq="30Min", tz=tz) + rng = date_range(start="2016-01-01", periods=2, freq="30Min", tz=tz, unit=unit) expected_rng = DatetimeIndex( [ @@ -72,7 +72,7 @@ def test_repeat(self, tz_naive_fixture): Timestamp("2016-01-01 00:30:00", tz=tz), Timestamp("2016-01-01 00:30:00", tz=tz), ] - ) + ).as_unit(unit) res = rng.repeat(reps) tm.assert_index_equal(res, expected_rng) diff --git a/pandas/tests/indexes/datetimes/methods/test_tz_localize.py b/pandas/tests/indexes/datetimes/methods/test_tz_localize.py index 9cd0d2df48ac8..6e5349870144a 100644 --- a/pandas/tests/indexes/datetimes/methods/test_tz_localize.py +++ b/pandas/tests/indexes/datetimes/methods/test_tz_localize.py @@ -97,9 +97,11 @@ def test_dti_tz_localize_ambiguous_infer(self, tz): dr.tz_localize(tz) @pytest.mark.parametrize("tz", easts) - def test_dti_tz_localize_ambiguous_infer2(self, tz): + def test_dti_tz_localize_ambiguous_infer2(self, tz, unit): # With repeated hours, we can infer the transition - dr = date_range(datetime(2011, 11, 6, 0), periods=5, freq=offsets.Hour(), tz=tz) + dr = date_range( + datetime(2011, 11, 6, 0), periods=5, freq=offsets.Hour(), tz=tz, unit=unit + ) times = [ "11/06/2011 00:00", "11/06/2011 01:00", @@ -107,11 +109,11 @@ def test_dti_tz_localize_ambiguous_infer2(self, tz): "11/06/2011 02:00", "11/06/2011 03:00", ] - di = DatetimeIndex(times) + di = DatetimeIndex(times).as_unit(unit) result = di.tz_localize(tz, ambiguous="infer") expected = dr._with_freq(None) tm.assert_index_equal(result, expected) - result2 = DatetimeIndex(times, tz=tz, ambiguous="infer") + result2 = DatetimeIndex(times, tz=tz, ambiguous="infer").as_unit(unit) tm.assert_index_equal(result2, expected) @pytest.mark.parametrize("tz", easts) @@ -269,11 +271,13 @@ def test_dti_tz_localize_ambiguous_nat(self, tz): tm.assert_numpy_array_equal(di_test.values, localized.values) @pytest.mark.parametrize("tz", easts) - def test_dti_tz_localize_ambiguous_flags(self, tz): + def test_dti_tz_localize_ambiguous_flags(self, tz, unit): # November 6, 2011, fall back, repeat 2 AM hour # Pass in flags to determine right dst transition - dr = date_range(datetime(2011, 11, 6, 0), periods=5, freq=offsets.Hour(), tz=tz) + dr = date_range( + datetime(2011, 11, 6, 0), periods=5, freq=offsets.Hour(), tz=tz, unit=unit + ) times = [ "11/06/2011 00:00", "11/06/2011 01:00", @@ -283,12 +287,17 @@ def test_dti_tz_localize_ambiguous_flags(self, tz): ] # Test tz_localize - di = DatetimeIndex(times) + di = DatetimeIndex(times).as_unit(unit) is_dst = [1, 1, 0, 0, 0] localized = di.tz_localize(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)) + + result = DatetimeIndex(times, tz=tz, ambiguous=is_dst).as_unit(unit) + tm.assert_index_equal( + result, + expected, + ) localized = di.tz_localize(tz, ambiguous=np.array(is_dst)) tm.assert_index_equal(dr, localized) @@ -297,12 +306,12 @@ def test_dti_tz_localize_ambiguous_flags(self, tz): tm.assert_index_equal(dr, localized) # Test constructor - localized = DatetimeIndex(times, tz=tz, ambiguous=is_dst) + localized = DatetimeIndex(times, tz=tz, ambiguous=is_dst).as_unit(unit) tm.assert_index_equal(dr, localized) # Test duplicate times where inferring the dst fails times += times - di = DatetimeIndex(times) + di = DatetimeIndex(times).as_unit(unit) # When the sizes are incompatible, make sure error is raised msg = "Length of ambiguous bool-array must be the same size as vals" @@ -315,6 +324,8 @@ def test_dti_tz_localize_ambiguous_flags(self, tz): dr = dr.append(dr) tm.assert_index_equal(dr, localized) + @pytest.mark.parametrize("tz", easts) + def test_dti_tz_localize_ambiguous_flags2(self, tz, unit): # When there is no dst transition, nothing special happens dr = date_range(datetime(2011, 6, 1, 0), periods=10, freq=offsets.Hour()) is_dst = np.array([1] * 10) @@ -373,15 +384,15 @@ def test_dti_tz_localize_bdate_range(self): ) @pytest.mark.parametrize("tz_type", ["", "dateutil/"]) def test_dti_tz_localize_nonexistent_shift( - self, start_ts, tz, end_ts, shift, tz_type + self, start_ts, tz, end_ts, shift, tz_type, unit ): # GH#8917 tz = tz_type + tz if isinstance(shift, str): shift = "shift_" + shift - dti = DatetimeIndex([Timestamp(start_ts)]) + dti = DatetimeIndex([Timestamp(start_ts)]).as_unit(unit) result = dti.tz_localize(tz, nonexistent=shift) - expected = DatetimeIndex([Timestamp(end_ts)]).tz_localize(tz) + expected = DatetimeIndex([Timestamp(end_ts)]).tz_localize(tz).as_unit(unit) tm.assert_index_equal(result, expected) @pytest.mark.parametrize("offset", [-1, 1]) diff --git a/pandas/tests/indexes/datetimes/methods/test_unique.py b/pandas/tests/indexes/datetimes/methods/test_unique.py index 5319bf59f8a64..0d2c1e3f03c02 100644 --- a/pandas/tests/indexes/datetimes/methods/test_unique.py +++ b/pandas/tests/indexes/datetimes/methods/test_unique.py @@ -33,7 +33,8 @@ def test_index_unique(rand_series_with_duplicate_datetimeindex): datetime(2000, 1, 3), datetime(2000, 1, 4), datetime(2000, 1, 5), - ] + ], + dtype="M8[ns]", ) assert uniques.dtype == "M8[ns]" # sanity tm.assert_index_equal(uniques, expected) diff --git a/pandas/tests/indexes/datetimes/test_constructors.py b/pandas/tests/indexes/datetimes/test_constructors.py index 2a1fa20dce777..7110a428544d5 100644 --- a/pandas/tests/indexes/datetimes/test_constructors.py +++ b/pandas/tests/indexes/datetimes/test_constructors.py @@ -955,8 +955,10 @@ def test_dti_tz_constructors(self, tzstr): for other in [idx2, idx3, idx4]: tm.assert_index_equal(idx1, other) - def test_dti_construction_univalent(self): - rng = date_range("03/12/2012 00:00", periods=10, freq="W-FRI", tz="US/Eastern") + def test_dti_construction_univalent(self, unit): + rng = date_range( + "03/12/2012 00:00", periods=10, freq="W-FRI", tz="US/Eastern", unit=unit + ) rng2 = DatetimeIndex(data=rng, tz="US/Eastern") tm.assert_index_equal(rng, rng2) diff --git a/pandas/tests/indexes/datetimes/test_date_range.py b/pandas/tests/indexes/datetimes/test_date_range.py index 3a6d1cacd0f81..6e8214dc775ae 100644 --- a/pandas/tests/indexes/datetimes/test_date_range.py +++ b/pandas/tests/indexes/datetimes/test_date_range.py @@ -285,9 +285,11 @@ def test_date_range_normalize(self): rng = date_range(snap, periods=n, normalize=False, freq="2D") offset = timedelta(2) - values = DatetimeIndex([snap + i * offset for i in range(n)], freq=offset) + expected = DatetimeIndex( + [snap + i * offset for i in range(n)], dtype="M8[ns]", freq=offset + ) - tm.assert_index_equal(rng, values) + tm.assert_index_equal(rng, expected) rng = date_range("1/1/2000 08:15", periods=n, normalize=False, freq="B") the_time = time(8, 15) @@ -306,11 +308,12 @@ def test_date_range_ambiguous_arguments(self): with pytest.raises(ValueError, match=msg): date_range(start, end, periods=10, freq="s") - def test_date_range_convenience_periods(self): + def test_date_range_convenience_periods(self, unit): # GH 20808 - result = date_range("2018-04-24", "2018-04-27", periods=3) + result = date_range("2018-04-24", "2018-04-27", periods=3, unit=unit) expected = DatetimeIndex( ["2018-04-24 00:00:00", "2018-04-25 12:00:00", "2018-04-27 00:00:00"], + dtype=f"M8[{unit}]", freq=None, ) @@ -322,6 +325,7 @@ def test_date_range_convenience_periods(self): "2018-04-01 04:00:00", tz="Australia/Sydney", periods=3, + unit=unit, ) expected = DatetimeIndex( [ @@ -329,7 +333,7 @@ def test_date_range_convenience_periods(self): Timestamp("2018-04-01 02:00:00+1000", tz="Australia/Sydney"), Timestamp("2018-04-01 04:00:00+1000", tz="Australia/Sydney"), ] - ) + ).as_unit(unit) tm.assert_index_equal(result, expected) def test_date_range_index_comparison(self): @@ -432,7 +436,7 @@ def test_catch_infinite_loop(self): with pytest.raises(ValueError, match=msg): date_range(datetime(2011, 11, 11), datetime(2011, 11, 12), freq=offset) - def test_construct_over_dst(self): + def test_construct_over_dst(self, unit): # GH 20854 pre_dst = Timestamp("2010-11-07 01:00:00").tz_localize( "US/Pacific", ambiguous=True @@ -445,14 +449,19 @@ def test_construct_over_dst(self): pre_dst, pst_dst, ] - expected = DatetimeIndex(expect_data, freq="h") - result = date_range(start="2010-11-7", periods=3, freq="h", tz="US/Pacific") + expected = DatetimeIndex(expect_data, freq="h").as_unit(unit) + result = date_range( + start="2010-11-7", periods=3, freq="h", tz="US/Pacific", unit=unit + ) tm.assert_index_equal(result, expected) - def test_construct_with_different_start_end_string_format(self): + def test_construct_with_different_start_end_string_format(self, unit): # GH 12064 result = date_range( - "2013-01-01 00:00:00+09:00", "2013/01/01 02:00:00+09:00", freq="h" + "2013-01-01 00:00:00+09:00", + "2013/01/01 02:00:00+09:00", + freq="h", + unit=unit, ) expected = DatetimeIndex( [ @@ -461,7 +470,7 @@ def test_construct_with_different_start_end_string_format(self): Timestamp("2013-01-01 02:00:00+09:00"), ], freq="h", - ) + ).as_unit(unit) tm.assert_index_equal(result, expected) def test_error_with_zero_monthends(self): @@ -469,13 +478,15 @@ def test_error_with_zero_monthends(self): with pytest.raises(ValueError, match=msg): date_range("1/1/2000", "1/1/2001", freq=MonthEnd(0)) - def test_range_bug(self): + def test_range_bug(self, unit): # GH #770 offset = DateOffset(months=3) - result = date_range("2011-1-1", "2012-1-31", freq=offset) + result = date_range("2011-1-1", "2012-1-31", freq=offset, unit=unit) start = datetime(2011, 1, 1) - expected = DatetimeIndex([start + i * offset for i in range(5)], freq=offset) + expected = DatetimeIndex( + [start + i * offset for i in range(5)], dtype=f"M8[{unit}]", freq=offset + ) tm.assert_index_equal(result, expected) def test_range_tz_pytz(self): @@ -1081,7 +1092,7 @@ def test_bday_near_overflow(self): # GH#24252 avoid doing unnecessary addition that _would_ overflow start = Timestamp.max.floor("D").to_pydatetime() rng = date_range(start, end=None, periods=1, freq="B") - expected = DatetimeIndex([start], freq="B") + expected = DatetimeIndex([start], freq="B").as_unit("ns") tm.assert_index_equal(rng, expected) def test_bday_overflow_error(self): @@ -1123,18 +1134,22 @@ def test_daterange_bug_456(self): result = rng1.union(rng2) assert isinstance(result, DatetimeIndex) - def test_cdaterange(self): - result = bdate_range("2013-05-01", periods=3, freq="C") - expected = DatetimeIndex(["2013-05-01", "2013-05-02", "2013-05-03"], freq="C") + def test_cdaterange(self, unit): + result = bdate_range("2013-05-01", periods=3, freq="C", unit=unit) + expected = DatetimeIndex( + ["2013-05-01", "2013-05-02", "2013-05-03"], dtype=f"M8[{unit}]", freq="C" + ) tm.assert_index_equal(result, expected) assert result.freq == expected.freq - def test_cdaterange_weekmask(self): + def test_cdaterange_weekmask(self, unit): result = bdate_range( - "2013-05-01", periods=3, freq="C", weekmask="Sun Mon Tue Wed Thu" + "2013-05-01", periods=3, freq="C", weekmask="Sun Mon Tue Wed Thu", unit=unit ) expected = DatetimeIndex( - ["2013-05-01", "2013-05-02", "2013-05-05"], freq=result.freq + ["2013-05-01", "2013-05-02", "2013-05-05"], + dtype=f"M8[{unit}]", + freq=result.freq, ) tm.assert_index_equal(result, expected) assert result.freq == expected.freq @@ -1147,10 +1162,14 @@ def test_cdaterange_weekmask(self): with pytest.raises(ValueError, match=msg): bdate_range("2013-05-01", periods=3, weekmask="Sun Mon Tue Wed Thu") - def test_cdaterange_holidays(self): - result = bdate_range("2013-05-01", periods=3, freq="C", holidays=["2013-05-01"]) + def test_cdaterange_holidays(self, unit): + result = bdate_range( + "2013-05-01", periods=3, freq="C", holidays=["2013-05-01"], unit=unit + ) expected = DatetimeIndex( - ["2013-05-02", "2013-05-03", "2013-05-06"], freq=result.freq + ["2013-05-02", "2013-05-03", "2013-05-06"], + dtype=f"M8[{unit}]", + freq=result.freq, ) tm.assert_index_equal(result, expected) assert result.freq == expected.freq @@ -1163,20 +1182,24 @@ def test_cdaterange_holidays(self): with pytest.raises(ValueError, match=msg): bdate_range("2013-05-01", periods=3, holidays=["2013-05-01"]) - def test_cdaterange_weekmask_and_holidays(self): + def test_cdaterange_weekmask_and_holidays(self, unit): result = bdate_range( "2013-05-01", periods=3, freq="C", weekmask="Sun Mon Tue Wed Thu", holidays=["2013-05-01"], + unit=unit, ) expected = DatetimeIndex( - ["2013-05-02", "2013-05-05", "2013-05-06"], freq=result.freq + ["2013-05-02", "2013-05-05", "2013-05-06"], + dtype=f"M8[{unit}]", + freq=result.freq, ) tm.assert_index_equal(result, expected) assert result.freq == expected.freq + def test_cdaterange_holidays_weekmask_requires_freqstr(self): # raise with non-custom freq msg = ( "a custom frequency string is required when holidays or " @@ -1216,7 +1239,7 @@ def test_range_with_millisecond_resolution(self, start_end): # https://github.com/pandas-dev/pandas/issues/24110 start, end = start_end result = date_range(start=start, end=end, periods=2, inclusive="left") - expected = DatetimeIndex([start]) + expected = DatetimeIndex([start], dtype="M8[ns, UTC]") tm.assert_index_equal(result, expected) @pytest.mark.parametrize( @@ -1234,7 +1257,7 @@ def test_range_with_millisecond_resolution(self, start_end): def test_range_with_timezone_and_custombusinessday(self, start, period, expected): # GH49441 result = date_range(start=start, periods=period, freq="C") - expected = DatetimeIndex(expected) + expected = DatetimeIndex(expected).as_unit("ns") tm.assert_index_equal(result, expected) @@ -1618,6 +1641,16 @@ def test_date_range_week_of_month(self, unit): ) tm.assert_index_equal(result2, expected2) + def test_date_range_week_of_month2(self, unit): + # GH#5115, GH#5348 + result = date_range("2013-1-1", periods=4, freq="WOM-1SAT", unit=unit) + expected = DatetimeIndex( + ["2013-01-05", "2013-02-02", "2013-03-02", "2013-04-06"], + dtype=f"M8[{unit}]", + freq="WOM-1SAT", + ) + tm.assert_index_equal(result, expected) + def test_date_range_negative_freq_month_end(self, unit): # GH#11018 rng = date_range("2011-01-31", freq="-2ME", periods=3, unit=unit) diff --git a/pandas/tests/indexes/datetimes/test_datetime.py b/pandas/tests/indexes/datetimes/test_datetime.py index a1074a1744c7a..e17afd8c2d8a9 100644 --- a/pandas/tests/indexes/datetimes/test_datetime.py +++ b/pandas/tests/indexes/datetimes/test_datetime.py @@ -58,12 +58,6 @@ def test_week_of_month_frequency(self): expected = DatetimeIndex([d1, d3, d2]) tm.assert_index_equal(result_union, expected) - # GH 5115 - result = date_range("2013-1-1", periods=4, freq="WOM-1SAT") - dates = ["2013-01-05", "2013-02-02", "2013-03-02", "2013-04-06"] - expected = DatetimeIndex(dates, freq="WOM-1SAT") - tm.assert_index_equal(result, expected) - def test_append_nondatetimeindex(self): rng = date_range("1/1/2000", periods=10) idx = Index(["a", "b", "c", "d"]) diff --git a/pandas/tests/resample/test_datetime_index.py b/pandas/tests/resample/test_datetime_index.py index 9cf4e68a9c5ec..9bc1b5e2502ae 100644 --- a/pandas/tests/resample/test_datetime_index.py +++ b/pandas/tests/resample/test_datetime_index.py @@ -1385,7 +1385,9 @@ def test_resample_timegrouper(dates, unit): @pytest.mark.parametrize("dates", [dates1, dates2, dates3]) -def test_resample_timegrouper2(dates): +def test_resample_timegrouper2(dates, unit): + dates = DatetimeIndex(dates).as_unit(unit) + df = DataFrame({"A": dates, "B": np.arange(len(dates)), "C": np.arange(len(dates))}) result = df.set_index("A").resample("ME").count() @@ -1393,7 +1395,7 @@ def test_resample_timegrouper2(dates): ["2014-07-31", "2014-08-31", "2014-09-30", "2014-10-31", "2014-11-30"], freq="ME", name="A", - ) + ).as_unit(unit) expected = DataFrame( {"B": [1, 0, 2, 2, 1], "C": [1, 0, 2, 2, 1]}, index=exp_idx, diff --git a/pandas/tests/resample/test_period_index.py b/pandas/tests/resample/test_period_index.py index f3a7c94fc9eaa..e9871fa4bdc12 100644 --- a/pandas/tests/resample/test_period_index.py +++ b/pandas/tests/resample/test_period_index.py @@ -303,13 +303,17 @@ def test_with_local_timezone(self, tz): dateutil.tz.gettz("America/Los_Angeles"), ], ) - def test_resample_with_tz(self, tz): + def test_resample_with_tz(self, tz, unit): # GH 13238 - ser = Series(2, index=date_range("2017-01-01", periods=48, freq="h", tz=tz)) + dti = date_range("2017-01-01", periods=48, freq="h", tz=tz, unit=unit) + ser = Series(2, index=dti) result = ser.resample("D").mean() + exp_dti = pd.DatetimeIndex( + ["2017-01-01", "2017-01-02"], tz=tz, freq="D" + ).as_unit(unit) expected = Series( 2.0, - index=pd.DatetimeIndex(["2017-01-01", "2017-01-02"], tz=tz, freq="D"), + index=exp_dti, ) tm.assert_series_equal(result, expected) # Especially assert that the timezone is LMT for pytz diff --git a/pandas/tests/tseries/offsets/test_business_hour.py b/pandas/tests/tseries/offsets/test_business_hour.py index 2779100f5355c..227cf6a495c91 100644 --- a/pandas/tests/tseries/offsets/test_business_hour.py +++ b/pandas/tests/tseries/offsets/test_business_hour.py @@ -946,6 +946,38 @@ def test_apply_nanoseconds(self): for base, expected in cases.items(): assert_offset_equal(offset, base, expected) + def test_datetimeindex(self): + idx1 = date_range(start="2014-07-04 15:00", end="2014-07-08 10:00", freq="bh") + idx2 = date_range(start="2014-07-04 15:00", periods=12, freq="bh") + idx3 = date_range(end="2014-07-08 10:00", periods=12, freq="bh") + expected = DatetimeIndex( + [ + "2014-07-04 15:00", + "2014-07-04 16:00", + "2014-07-07 09:00", + "2014-07-07 10:00", + "2014-07-07 11:00", + "2014-07-07 12:00", + "2014-07-07 13:00", + "2014-07-07 14:00", + "2014-07-07 15:00", + "2014-07-07 16:00", + "2014-07-08 09:00", + "2014-07-08 10:00", + ], + freq="bh", + ) + for idx in [idx1, idx2, idx3]: + tm.assert_index_equal(idx, expected) + + idx1 = date_range(start="2014-07-04 15:45", end="2014-07-08 10:45", freq="bh") + idx2 = date_range(start="2014-07-04 15:45", periods=12, freq="bh") + idx3 = date_range(end="2014-07-08 10:45", periods=12, freq="bh") + + expected = idx1 + for idx in [idx1, idx2, idx3]: + tm.assert_index_equal(idx, expected) + @pytest.mark.parametrize("td_unit", ["s", "ms", "us", "ns"]) @pytest.mark.parametrize("unit", ["s", "ms", "us", "ns"]) def test_bday_ignores_timedeltas(self, unit, td_unit): diff --git a/pandas/tests/tseries/offsets/test_month.py b/pandas/tests/tseries/offsets/test_month.py index 2b643999c3ad3..fc12510369245 100644 --- a/pandas/tests/tseries/offsets/test_month.py +++ b/pandas/tests/tseries/offsets/test_month.py @@ -23,6 +23,7 @@ DatetimeIndex, Series, _testing as tm, + date_range, ) from pandas.tests.tseries.offsets.common import ( assert_is_on_offset, @@ -73,6 +74,11 @@ def test_offset_whole_year(self): exp = DatetimeIndex(dates[1:]) tm.assert_index_equal(result, exp) + # ensure generating a range with DatetimeIndex gives same result + result = date_range(start=dates[0], end=dates[-1], freq="SM") + exp = DatetimeIndex(dates, freq="SM") + tm.assert_index_equal(result, exp) + offset_cases = [] offset_cases.append( ( @@ -324,6 +330,11 @@ def test_offset_whole_year(self): exp = DatetimeIndex(dates[1:]) tm.assert_index_equal(result, exp) + # ensure generating a range with DatetimeIndex gives same result + result = date_range(start=dates[0], end=dates[-1], freq="SMS") + exp = DatetimeIndex(dates, freq="SMS") + tm.assert_index_equal(result, exp) + offset_cases = [ ( SemiMonthBegin(), diff --git a/pandas/tests/window/test_ewm.py b/pandas/tests/window/test_ewm.py index c5c395414b450..427780db79783 100644 --- a/pandas/tests/window/test_ewm.py +++ b/pandas/tests/window/test_ewm.py @@ -102,12 +102,38 @@ def test_ewma_with_times_equal_spacing(halflife_with_times, times, min_periods): tm.assert_frame_equal(result, expected) -def test_ewma_with_times_variable_spacing(tz_aware_fixture): +@pytest.mark.parametrize( + "unit", + [ + pytest.param( + "s", + marks=pytest.mark.xfail( + reason="ExponentialMovingWindow constructor raises on non-nano" + ), + ), + pytest.param( + "ms", + marks=pytest.mark.xfail( + reason="ExponentialMovingWindow constructor raises on non-nano" + ), + ), + pytest.param( + "us", + marks=pytest.mark.xfail( + reason="ExponentialMovingWindow constructor raises on non-nano" + ), + ), + "ns", + ], +) +def test_ewma_with_times_variable_spacing(tz_aware_fixture, unit): tz = tz_aware_fixture halflife = "23 days" - times = DatetimeIndex( - ["2020-01-01", "2020-01-10T00:04:05", "2020-02-23T05:00:23"] - ).tz_localize(tz) + times = ( + DatetimeIndex(["2020-01-01", "2020-01-10T00:04:05", "2020-02-23T05:00:23"]) + .tz_localize(tz) + .as_unit(unit) + ) data = np.arange(3) df = DataFrame(data) result = df.ewm(halflife=halflife, times=times).mean()