qis.find_upto_date_from_datetime_index

qis.find_upto_date_from_datetime_index(index, date)[source]

Finds the latest date in the index that is on or before the specified date.

Uses binary search to efficiently find the largest date in the index that is less than or equal to the target date, even when the target date is not present in the index. Input order does not affect the result, and NaT entries do not match a finite target.

Parameters:
  • index (DatetimeIndex | List[Timestamp]) – DatetimeIndex or list of timestamps to search within. The timestamps need not be chronologically ordered.

  • date (Timestamp) – Target date to find the latest preceding date for.

Returns:

The latest timestamp in index that is <= date, or None if date is before the earliest finite timestamp in the index or the index contains no finite timestamps.

Warns:

UserWarning – If the target date is before the earliest finite date in the index or the index contains no finite timestamps.

Return type:

Timestamp | None

Example

>>> index = pd.date_range('2023-01-01', periods=5, freq='D')
>>> find_upto_date_from_datetime_index(index, pd.Timestamp('2023-01-03'))
Timestamp('2023-01-03 00:00:00')
>>> find_upto_date_from_datetime_index(index, pd.Timestamp('2023-01-02 12:00'))
Timestamp('2023-01-02 00:00:00')