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

Method _add_offset

pandas/core/arrays/datetimes.py:807–843  ·  view source on GitHub ↗
(self, offset: BaseOffset)

Source from the content-addressed store, hash-verified

805 # Arithmetic Methods
806
807 def _add_offset(self, offset: BaseOffset) -> Self:
808 assert not isinstance(offset, Tick)
809
810 if self.tz is not None:
811 values = self.tz_localize(None)
812 else:
813 values = self
814
815 try:
816 res_values = offset._apply_array(values._ndarray)
817 if res_values.dtype.kind == "i":
818 # error: Argument 1 to "view" of "ndarray" has
819 # incompatible type
820 # "dtype[datetime64[date | int | None]] | DatetimeTZDtype";
821 # expected "dtype[Any] | _HasDType[dtype[Any]]" [arg-type]
822 res_values = res_values.view(values.dtype) # type: ignore[arg-type]
823 except NotImplementedError:
824 if get_option("performance_warnings"):
825 warnings.warn(
826 "Non-vectorized DateOffset being applied to Series or "
827 "DatetimeIndex.",
828 PerformanceWarning,
829 stacklevel=find_stack_level(),
830 )
831 res_values = self.astype("O") + offset
832 result = type(self)._from_sequence(res_values, dtype=self.dtype)
833
834 else:
835 result = type(self)._simple_new(res_values, dtype=res_values.dtype)
836 if offset.normalize:
837 result = result.normalize()
838 result._freq = None
839
840 if self.tz is not None:
841 result = result.tz_localize(self.tz)
842
843 return result
844
845 # -----------------------------------------------------------------
846 # Timezone Conversion and Localization Methods

Callers

nothing calls this directly

Calls 8

tz_localizeMethod · 0.95
astypeMethod · 0.95
get_optionFunction · 0.90
find_stack_levelFunction · 0.90
viewMethod · 0.45
_from_sequenceMethod · 0.45
_simple_newMethod · 0.45
normalizeMethod · 0.45

Tested by

no test coverage detected