If possible, convert this index to a pandas.DatetimeIndex. Parameters ---------- unsafe : bool Flag to turn off calendar mismatch warnings (default ``False``). time_unit : str Time resolution of resulting DatetimeIndex. Can be one of `"s"`,
(
self, unsafe: bool = False, time_unit: PDDatetimeUnitOptions | None = None
)
| 551 | ) from err |
| 552 | |
| 553 | def to_datetimeindex( |
| 554 | self, unsafe: bool = False, time_unit: PDDatetimeUnitOptions | None = None |
| 555 | ) -> pd.DatetimeIndex: |
| 556 | """If possible, convert this index to a pandas.DatetimeIndex. |
| 557 | |
| 558 | Parameters |
| 559 | ---------- |
| 560 | unsafe : bool |
| 561 | Flag to turn off calendar mismatch warnings (default ``False``). |
| 562 | time_unit : str |
| 563 | Time resolution of resulting DatetimeIndex. Can be one of `"s"`, |
| 564 | ``"ms"``, ``"us"``, or ``"ns"`` (default ``"ns"``). |
| 565 | |
| 566 | Returns |
| 567 | ------- |
| 568 | pandas.DatetimeIndex |
| 569 | |
| 570 | Raises |
| 571 | ------ |
| 572 | ValueError |
| 573 | If the CFTimeIndex contains dates that are not possible in the |
| 574 | standard calendar or outside the range representable by the |
| 575 | specified ``time_unit``. |
| 576 | |
| 577 | Warns |
| 578 | ----- |
| 579 | RuntimeWarning |
| 580 | If converting from a non-standard calendar, or a Gregorian |
| 581 | calendar with dates prior to the reform (1582-10-15). |
| 582 | |
| 583 | Warnings |
| 584 | -------- |
| 585 | Note that for non-proleptic Gregorian calendars, this will change the |
| 586 | calendar type of the index. In that case the result of this method |
| 587 | should be used with caution. |
| 588 | |
| 589 | Examples |
| 590 | -------- |
| 591 | >>> times = xr.date_range( |
| 592 | ... "2000", periods=2, calendar="gregorian", use_cftime=True |
| 593 | ... ) |
| 594 | >>> times |
| 595 | CFTimeIndex([2000-01-01 00:00:00, 2000-01-02 00:00:00], |
| 596 | dtype='object', length=2, calendar='standard', freq=None) |
| 597 | >>> times.to_datetimeindex(time_unit="ns") |
| 598 | DatetimeIndex(['2000-01-01', '2000-01-02'], dtype='datetime64[ns]', freq=None) |
| 599 | """ |
| 600 | |
| 601 | if not self._data.size: |
| 602 | return pd.DatetimeIndex([]) |
| 603 | |
| 604 | if time_unit is None: |
| 605 | emit_user_level_warning( |
| 606 | "In a future version of xarray to_datetimeindex will default " |
| 607 | "to returning a 'us'-resolution DatetimeIndex instead of a " |
| 608 | "'ns'-resolution DatetimeIndex. This warning can be silenced " |
| 609 | "by explicitly passing the `time_unit` keyword argument.", |
| 610 | FutureWarning, |