{"record":{"id":"e0e4c90c1eca2cff","repo":"microsoft/qlib","slug":"all-elements-in-idx-list-must-be-of-the-same-datet","errorCode":null,"errorMessage":"All elements in idx_list must be of the same datetime64 precision","messagePattern":"All elements in idx_list must be of the same datetime64 precision","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"qlib/utils/index_data.py","lineNumber":116,"sourceCode":"    \"\"\"\n\n    def __init__(self, idx_list: Union[List, pd.Index, \"Index\", int]):\n        self.idx_list: np.ndarray = None  # using array type for index list will make things easier\n        if isinstance(idx_list, Index):\n            # Fast read-only copy\n            self.idx_list = idx_list.idx_list\n            self.index_map = idx_list.index_map\n            self._is_sorted = idx_list._is_sorted\n        elif isinstance(idx_list, int):\n            self.index_map = self.idx_list = np.arange(idx_list)\n            self._is_sorted = True\n        else:\n            # Check if all elements in idx_list are of the same type\n            if not all(isinstance(x, type(idx_list[0])) for x in idx_list):\n                raise TypeError(\"All elements in idx_list must be of the same type\")\n            # Check if all elements in idx_list are of the same datetime64 precision\n            if isinstance(idx_list[0], np.datetime64) and not all(x.dtype == idx_list[0].dtype for x in idx_list):\n                raise TypeError(\"All elements in idx_list must be of the same datetime64 precision\")\n            self.idx_list = np.array(idx_list)\n            # NOTE: only the first appearance is indexed\n            self.index_map = dict(zip(self.idx_list, range(len(self))))\n            self._is_sorted = False\n\n    def __getitem__(self, i: int):\n        return self.idx_list[i]\n\n    def _convert_type(self, item):\n        \"\"\"\n\n        After user creates indices with Type A, user may query data with other types with the same info.\n            This method try to make type conversion and make query sane rather than raising KeyError strictly\n\n        Parameters\n        ----------\n        item :\n            The item to query index","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/utils/index_data.py#L98-L134","documentation":"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.","triggerScenarios":"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').","commonSituations":"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.","solutions":["Cast the whole list to one precision before constructing: Index(np.array(dates, dtype='datetime64[ns]')).","If interoperating with pandas, go through pd.DatetimeIndex(dates).values or pd.to_datetime(dates).to_numpy(dtype='datetime64[ns]').","Check each element's .dtype in a quick assertion loop when dates come from multiple sources."],"exampleFix":"// before\nidx = Index([np.datetime64('2020-01-01', 'D'), np.datetime64('2020-01-02', 'ns')])  # TypeError\n\n// after\nidx = Index(np.array(['2020-01-01', '2020-01-02'], dtype='datetime64[D]'))","handlingStrategy":"validation","validationCode":"import numpy as np\ndates = np.asarray(dates, dtype='datetime64[ns]')  # unify precision before Index(...)","typeGuard":"def same_datetime_precision(items) -> bool:\n    d = [x.dtype for x in items if isinstance(x, np.datetime64)]\n    return len(set(d)) <= 1","tryCatchPattern":"try:\n    idx = Index(dates)\nexcept TypeError:\n    idx = Index(np.array(dates, dtype='datetime64[ns]'))","preventionTips":["Pick one canonical datetime precision (ns is pandas' default) and cast every date array to it on ingest.","Be wary mixing arrays from parquet/arrow (us/ms resolution) with pandas Timestamps, especially on numpy>=2."],"tags":["qlib","index-data","datetime64","numpy","precision"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}