From ae30ca66dfcf1cf1c58cb9d3f7c212aa07864b18 Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Wed, 11 Nov 2026 16:53:07 +0000 Subject: [PATCH 1/2] MAINT: Describe date_index as passed in date_to_index errors Follow-up to #1016. The errors for an empty or unsorted date_index describe it as it was passed, so a Series is reported as a Series and not as the DatetimeIndex it is converted to, and the docstring lists Series as an accepted type. --- arch/tests/utility/test_array.py | 23 +++++++++++++++++++++++ arch/utility/array.py | 19 +++++++++++-------- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/arch/tests/utility/test_array.py b/arch/tests/utility/test_array.py index db9ebfd537..05564e7aee 100644 --- a/arch/tests/utility/test_array.py +++ b/arch/tests/utility/test_array.py @@ -1,5 +1,6 @@ from abc import abstractmethod import datetime as dt +import re import numpy as np from numpy.random import RandomState @@ -445,6 +446,28 @@ def test_date_to_index_tz_aware_not_monotonic(): date_to_index("2021-03-14", date_index) +@pytest.mark.filterwarnings(*NO_DEPRECATIONS) +@pytest.mark.parametrize("tz", [None, "Asia/Kolkata"]) +@pytest.mark.parametrize("box", [Series, DatetimeIndex]) +@pytest.mark.parametrize( + ("positions", "rule"), + [ + ([1, 0, 2], "increasing and unique"), + ([0, 1, 1], "increasing and unique"), + ([], "a non-empty array of dates"), + ], +) +def test_date_to_index_error_describes_input(tz, box, positions, rule): + # The error describes date_index as it was passed, not as it was converted + dates = date_range("2021-03-13", periods=3, tz=tz)[positions] + date_index = box(dates) + message = ( + f"date_index must be {rule}; got shape ({len(positions)},) ({box.__name__})" + ) + with pytest.raises(ValueError, match=re.escape(message)): + date_to_index("2021-03-14", date_index) + + def test_date_to_index_timestamp(): dr = date_range("20000101", periods=3000, freq="W") y = Series(np.arange(3000.0), index=dr) diff --git a/arch/utility/array.py b/arch/utility/array.py index 2eadf7b3ff..28ec83a58a 100644 --- a/arch/utility/array.py +++ b/arch/utility/array.py @@ -769,7 +769,7 @@ def date_to_index( ---------- date : str, datetime, datetime64 or Timestamp Date to use when returning the index. - date_index : DatetimeIndex or ndarray + date_index : DatetimeIndex, Series or ndarray Index data containing datetime64 values. Returns @@ -804,16 +804,19 @@ def date_to_index( "a datetime64 array", _describe(date_index), ) - if isinstance(date_index, Series): - date_index = DatetimeIndex(date_index) - if isinstance(date_index, DatetimeIndex): - if date_index.tzinfo is not None: + # The errors describe date_index as it was passed, so the converted dates are kept + # separately + dates = date_index + if isinstance(dates, Series): + dates = DatetimeIndex(dates) + if isinstance(dates, DatetimeIndex): + if dates.tzinfo is not None: # The same UTC values without a time zone that .values returned. Using # .values is deprecated for time zone aware indices. - date_index = date_index.tz_convert("GMT").tz_localize(None) - values = date_index.to_numpy() + dates = dates.tz_convert("GMT").tz_localize(None) + values = dates.to_numpy() else: - values = np.asarray(date_index) + values = np.asarray(dates) if values.shape[0] == 0: _fail( ValueError, From a7028826c64f57154c4c8cdfe257cb6b82844f08 Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Wed, 11 Nov 2026 16:53:07 +0000 Subject: [PATCH 2/2] DOC: Add a release note for time zone aware Series in date_to_index --- doc/source/changes/9.0.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/source/changes/9.0.rst b/doc/source/changes/9.0.rst index 1c140d7597..c4278ad762 100644 --- a/doc/source/changes/9.0.rst +++ b/doc/source/changes/9.0.rst @@ -1222,6 +1222,9 @@ Bug fixes - The output of :class:`~arch.utility.testing.WaldTestStatistic` shows the name of the test once, as its first line. Since 4.8.0 the name was also printed between the null and the alternative hypotheses. Contributed by sohailshk (:issue:`1014`). +- ``date_to_index`` in ``arch.utility.array`` accepts a ``Series`` of time zone aware + dates, as it accepts a time zone aware ``DatetimeIndex``, where it raised a + ``TypeError``. Contributed by jackiectl2 (:issue:`1016`). - Fixed ``SkewStudent.ppf`` for probabilities close to 1, which lost precision and returned ``inf`` for some probabilities below 1. For example, with :math:`\eta=8` and :math:`\lambda=0.9` the quantile of the largest float below 1 was ``inf`` and is