microsoft/qlib · error · TypeError

All elements in idx_list must be of the same datetime64 prec

Error message

All elements in idx_list must be of the same datetime64 precision

What it means

A stricter variant of the same-type check: when every element of idx_list is a numpy datetime64, qlib's Index additionally requires identical dtype precision (e.g. all datetime64[D] vs all datetime64[ns]). numpy silently upcasts mixed precisions when building arrays, which would silently misalign comparisons, so the constructor rejects it.

Source

Thrown at qlib/utils/index_data.py:116

    """

    def __init__(self, idx_list: Union[List, pd.Index, "Index", int]):
        self.idx_list: np.ndarray = None  # using array type for index list will make things easier
        if isinstance(idx_list, Index):
            # Fast read-only copy
            self.idx_list = idx_list.idx_list
            self.index_map = idx_list.index_map
            self._is_sorted = idx_list._is_sorted
        elif isinstance(idx_list, int):
            self.index_map = self.idx_list = np.arange(idx_list)
            self._is_sorted = True
        else:
            # Check if all elements in idx_list are of the same type
            if not all(isinstance(x, type(idx_list[0])) for x in idx_list):
                raise TypeError("All elements in idx_list must be of the same type")
            # Check if all elements in idx_list are of the same datetime64 precision
            if isinstance(idx_list[0], np.datetime64) and not all(x.dtype == idx_list[0].dtype for x in idx_list):
                raise TypeError("All elements in idx_list must be of the same datetime64 precision")
            self.idx_list = np.array(idx_list)
            # NOTE: only the first appearance is indexed
            self.index_map = dict(zip(self.idx_list, range(len(self))))
            self._is_sorted = False

    def __getitem__(self, i: int):
        return self.idx_list[i]

    def _convert_type(self, item):
        """

        After user creates indices with Type A, user may query data with other types with the same info.
            This method try to make type conversion and make query sane rather than raising KeyError strictly

        Parameters
        ----------
        item :
            The item to query index

View on GitHub (pinned to 79633dd950)

Solutions

  1. Cast the whole list to one precision before constructing: Index(np.array(dates, dtype='datetime64[ns]')).
  2. If interoperating with pandas, go through pd.DatetimeIndex(dates).values or pd.to_datetime(dates).to_numpy(dtype='datetime64[ns]').
  3. Check each element's .dtype in a quick assertion loop when dates come from multiple sources.

Example fix

// before
idx = Index([np.datetime64('2020-01-01', 'D'), np.datetime64('2020-01-02', 'ns')])  # TypeError

// after
idx = Index(np.array(['2020-01-01', '2020-01-02'], dtype='datetime64[D]'))
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np
dates = np.asarray(dates, dtype='datetime64[ns]')  # unify precision before Index(...)

Type guard

def same_datetime_precision(items) -> bool:
    d = [x.dtype for x in items if isinstance(x, np.datetime64)]
    return len(set(d)) <= 1

Try / catch

try:
    idx = Index(dates)
except TypeError:
    idx = Index(np.array(dates, dtype='datetime64[ns]'))

Prevention

When it happens

Trigger: Index([np.datetime64('2020-01-01', 'D'), np.datetime64('2020-01-01', 'ns')]) or building SingleData from arrays produced by different data sources with different datetime resolutions (daily 'D' vs nanosecond 'ns').

Common situations: Mixing dates read from an arrow/parquet file (often datetime64[ms] or [us]) with dates from pandas Timestamps (datetime64[ns]); combining qlib calendar arrays (frequently datetime64[D]) with numpy datetime64('now') style values which default to [ns] or the local resolution; numpy 2.x changing default resolutions.

Related errors


AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15). Data as JSON: /api/errors/e0e4c90c1eca2cff. Report an issue: GitHub.