Convert indexer for partial string selection if data has DatetimeIndex/PeriodIndex
(index, indexer, unit="ns")
| 78 | |
| 79 | |
| 80 | def _maybe_partial_time_string(index, indexer, unit="ns"): |
| 81 | """ |
| 82 | Convert indexer for partial string selection |
| 83 | if data has DatetimeIndex/PeriodIndex |
| 84 | """ |
| 85 | # do not pass dd.Index |
| 86 | assert is_index_like(index) |
| 87 | unit = unit or "ns" |
| 88 | |
| 89 | if not isinstance(index, (pd.DatetimeIndex, pd.PeriodIndex)): |
| 90 | return indexer |
| 91 | |
| 92 | if isinstance(indexer, slice): |
| 93 | if isinstance(indexer.start, str): |
| 94 | start = index._maybe_cast_slice_bound(indexer.start, "left") |
| 95 | else: |
| 96 | start = indexer.start |
| 97 | |
| 98 | if isinstance(indexer.stop, str): |
| 99 | stop = index._maybe_cast_slice_bound(indexer.stop, "right") |
| 100 | else: |
| 101 | stop = indexer.stop |
| 102 | if PANDAS_GE_300 and hasattr(start, "as_unit"): |
| 103 | start = None if start is None else start.as_unit(unit) |
| 104 | stop = None if stop is None else stop.as_unit(unit) |
| 105 | return slice(start, stop) |
| 106 | |
| 107 | elif isinstance(indexer, str): |
| 108 | start = index._maybe_cast_slice_bound(indexer, "left") |
| 109 | stop = index._maybe_cast_slice_bound(indexer, "right") |
| 110 | if PANDAS_GE_300 and hasattr(start, "as_unit"): |
| 111 | start = None if start is None else start.as_unit(unit) |
| 112 | stop = None if stop is None else stop.as_unit(unit) |
| 113 | return slice(min(start, stop), max(start, stop)) |
| 114 | |
| 115 | return indexer |
no test coverage detected