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

Method to_timestamp

pandas/core/arrays/period.py:758–867  ·  view source on GitHub ↗

Cast to DatetimeArray/Index. If possible, gives microsecond-unit DatetimeArray/Index. Otherwise gives nanosecond unit. Parameters ---------- freq : str or DateOffset, optional Target frequency. The default is 'D' for week or longer,

(self, freq=None, how: str = "start")

Source from the content-addressed store, hash-verified

756 return isleapyear_arr(np.asarray(self.year))
757
758 def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray:
759 """
760 Cast to DatetimeArray/Index.
761
762 If possible, gives microsecond-unit DatetimeArray/Index. Otherwise
763 gives nanosecond unit.
764
765 Parameters
766 ----------
767 freq : str or DateOffset, optional
768 Target frequency. The default is 'D' for week or longer,
769 's' otherwise.
770 how : {'s', 'e', 'start', 'end'}
771 Whether to use the start or end of the time period being converted.
772
773 Returns
774 -------
775 DatetimeArray/Index
776 Timestamp representation of given Period-like object.
777
778 See Also
779 --------
780 PeriodIndex.day : The days of the period.
781 PeriodIndex.from_fields : Construct a PeriodIndex from fields
782 (year, month, day, etc.).
783 PeriodIndex.from_ordinals : Construct a PeriodIndex from ordinals.
784 PeriodIndex.hour : The hour of the period.
785 PeriodIndex.minute : The minute of the period.
786 PeriodIndex.month : The month as January=1, December=12.
787 PeriodIndex.second : The second of the period.
788 PeriodIndex.year : The year of the period.
789
790 Examples
791 --------
792 >>> idx = pd.PeriodIndex(["2023-01", "2023-02", "2023-03"], freq="M")
793 >>> idx.to_timestamp()
794 DatetimeIndex(['2023-01-01', '2023-02-01', '2023-03-01'],
795 dtype='datetime64[us]', freq='MS')
796
797 The frequency will not be inferred if the index contains less than
798 three elements, or if the values of index are not strictly monotonic:
799
800 >>> idx = pd.PeriodIndex(["2023-01", "2023-02"], freq="M")
801 >>> idx.to_timestamp()
802 DatetimeIndex(['2023-01-01', '2023-02-01'], dtype='datetime64[us]', freq=None)
803
804 >>> idx = pd.PeriodIndex(
805 ... ["2023-01", "2023-02", "2023-02", "2023-03"], freq="2M"
806 ... )
807 >>> idx.to_timestamp()
808 DatetimeIndex(['2023-01-01', '2023-02-01', '2023-02-01', '2023-03-01'],
809 dtype='datetime64[us]', freq=None)
810 """
811 from pandas.core.arrays import DatetimeArray
812
813 how = libperiod.validate_end_alias(how)
814
815 if self.freq.base == "ns" or freq == "ns":

Callers 1

astypeMethod · 0.95

Calls 3

asfreqMethod · 0.95
_from_sequenceMethod · 0.45
_with_freqMethod · 0.45

Tested by

no test coverage detected