MCPcopy
hub / github.com/pandas-dev/pandas / _adjust_dates_anchored

Function _adjust_dates_anchored

pandas/core/resample.py:2995–3076  ·  view source on GitHub ↗
(
    first: Timestamp,
    last: Timestamp,
    freq: Tick,
    closed: Literal["right", "left"] = "right",
    origin: TimeGrouperOrigin = "start_day",
    offset: Timedelta | None = None,
    unit: TimeUnit = "ns",
)

Source from the content-addressed store, hash-verified

2993
2994
2995def _adjust_dates_anchored(
2996 first: Timestamp,
2997 last: Timestamp,
2998 freq: Tick,
2999 closed: Literal["right", "left"] = "right",
3000 origin: TimeGrouperOrigin = "start_day",
3001 offset: Timedelta | None = None,
3002 unit: TimeUnit = "ns",
3003) -> tuple[Timestamp, Timestamp]:
3004 # First and last offsets should be calculated from the start day to fix an
3005 # error cause by resampling across multiple days when a one day period is
3006 # not a multiple of the frequency. See GH 8683
3007 # To handle frequencies that are not multiple or divisible by a day we let
3008 # the possibility to define a fixed origin timestamp. See GH 31809
3009 first = first.as_unit(unit)
3010 last = last.as_unit(unit)
3011 if offset is not None:
3012 offset = offset.as_unit(unit)
3013
3014 freq_value = Timedelta(freq).as_unit(unit)._value
3015
3016 origin_timestamp = 0 # origin == "epoch"
3017 if origin == "start_day":
3018 origin_timestamp = first.normalize()._value
3019 elif origin == "start":
3020 origin_timestamp = first._value
3021 elif isinstance(origin, Timestamp):
3022 origin_timestamp = origin.as_unit(unit)._value
3023 elif origin in ["end", "end_day"]:
3024 origin_last = last if origin == "end" else last.ceil("D")
3025 sub_freq_times = (origin_last._value - first._value) // freq_value
3026 if closed == "left":
3027 sub_freq_times += 1
3028 first = origin_last - sub_freq_times * freq
3029 origin_timestamp = first._value
3030 origin_timestamp += offset._value if offset else 0
3031
3032 # GH 10117 & GH 19375. If first and last contain timezone information,
3033 # Perform the calculation in UTC in order to avoid localizing on an
3034 # Ambiguous or Nonexistent time.
3035 first_tzinfo = first.tzinfo
3036 last_tzinfo = last.tzinfo
3037 if first_tzinfo is not None:
3038 first = first.tz_convert("UTC")
3039 if last_tzinfo is not None:
3040 last = last.tz_convert("UTC")
3041
3042 foffset = (first._value - origin_timestamp) % freq_value
3043 loffset = (last._value - origin_timestamp) % freq_value
3044
3045 if closed == "right":
3046 if foffset > 0:
3047 # roll back
3048 fresult_int = first._value - foffset
3049 else:
3050 fresult_int = first._value - freq_value
3051
3052 if loffset > 0:

Callers 1

Calls 5

ceilMethod · 0.80
as_unitMethod · 0.45
normalizeMethod · 0.45
tz_convertMethod · 0.45
tz_localizeMethod · 0.45

Tested by

no test coverage detected