MCPcopy Index your code
hub / github.com/pydata/xarray / BaseCFTimeOffset

Class BaseCFTimeOffset

xarray/coding/cftime_offsets.py:108–197  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

106
107
108class BaseCFTimeOffset:
109 _freq: ClassVar[str | None] = None
110 _day_option: ClassVar[DayOption | None] = None
111 n: int
112
113 def __init__(self, n: int = 1) -> None:
114 if not isinstance(n, int):
115 raise TypeError(
116 "The provided multiple 'n' must be an integer. "
117 f"Instead a value of type {type(n)!r} was provided."
118 )
119 self.n = n
120
121 def rule_code(self) -> str | None:
122 return self._freq
123
124 def __eq__(self, other: object) -> bool:
125 if not isinstance(other, BaseCFTimeOffset):
126 return NotImplemented
127 return self.n == other.n and self.rule_code() == other.rule_code()
128
129 def __ne__(self, other: object) -> bool:
130 return not self == other
131
132 def __add__(self, other):
133 return self.__apply__(other)
134
135 def __sub__(self, other):
136 if TYPE_CHECKING:
137 import cftime
138 else:
139 cftime = attempt_import("cftime")
140
141 if isinstance(other, cftime.datetime):
142 raise TypeError("Cannot subtract a cftime.datetime from a time offset.")
143 elif type(other) is type(self):
144 return type(self)(self.n - other.n)
145 else:
146 return NotImplemented
147
148 def __mul__(self, other: int) -> Self:
149 if not isinstance(other, int):
150 return NotImplemented
151 return type(self)(n=other * self.n)
152
153 def __neg__(self) -> Self:
154 return self * -1
155
156 def __rmul__(self, other):
157 return self.__mul__(other)
158
159 def __radd__(self, other):
160 return self.__add__(other)
161
162 def __rsub__(self, other):
163 if isinstance(other, BaseCFTimeOffset) and type(self) is not type(other):
164 raise TypeError("Cannot subtract cftime offsets of differing types")
165 return -self + other

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…