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

Method to_timestamp

pandas/core/frame.py:16211–16297  ·  view source on GitHub ↗

Cast PeriodIndex to DatetimeIndex of timestamps, at *beginning* of period. This can be changed to the *end* of the period, by specifying `how="e"`. Parameters ---------- freq : str, default frequency of PeriodIndex Desired frequency. how

(
        self,
        freq: Frequency | None = None,
        how: ToTimestampHow = "start",
        axis: Axis = 0,
        copy: bool | lib.NoDefault = lib.no_default,
    )

Source from the content-addressed store, hash-verified

16209 return result.__finalize__(self, method="quantile")
16210
16211 def to_timestamp(
16212 self,
16213 freq: Frequency | None = None,
16214 how: ToTimestampHow = "start",
16215 axis: Axis = 0,
16216 copy: bool | lib.NoDefault = lib.no_default,
16217 ) -> DataFrame:
16218 """
16219 Cast PeriodIndex to DatetimeIndex of timestamps, at *beginning* of period.
16220
16221 This can be changed to the *end* of the period, by specifying `how="e"`.
16222
16223 Parameters
16224 ----------
16225 freq : str, default frequency of PeriodIndex
16226 Desired frequency.
16227 how : {'s', 'e', 'start', 'end'}
16228 Convention for converting period to timestamp; start of period
16229 vs. end.
16230 axis : {0 or 'index', 1 or 'columns'}, default 0
16231 The axis to convert (the index by default).
16232 copy : bool, default False
16233 This keyword is now ignored; changing its value will have no
16234 impact on the method.
16235
16236 .. deprecated:: 3.0.0
16237
16238 This keyword is ignored and will be removed in pandas 4.0. Since
16239 pandas 3.0, this method always returns a new object using a lazy
16240 copy mechanism that defers copies until necessary
16241 (Copy-on-Write). See the `user guide on Copy-on-Write
16242 <https://pandas.pydata.org/docs/dev/user_guide/copy_on_write.html>`__
16243 for more details.
16244
16245 Returns
16246 -------
16247 DataFrame with DatetimeIndex
16248 DataFrame with the PeriodIndex cast to DatetimeIndex.
16249
16250 See Also
16251 --------
16252 DataFrame.to_period: Inverse method to cast DatetimeIndex to PeriodIndex.
16253 Series.to_timestamp: Equivalent method for Series.
16254
16255 Examples
16256 --------
16257 >>> idx = pd.PeriodIndex(["2023", "2024"], freq="Y")
16258 >>> d = {"col1": [1, 2], "col2": [3, 4]}
16259 >>> df1 = pd.DataFrame(data=d, index=idx)
16260 >>> df1
16261 col1 col2
16262 2023 1 3
16263 2024 2 4
16264
16265 The resulting timestamps will be at the beginning of the year in this case
16266
16267 >>> df1 = df1.to_timestamp()
16268 >>> df1

Callers 3

test_to_timestampMethod · 0.95

Calls 3

_get_axis_nameMethod · 0.80
copyMethod · 0.45

Tested by 3

test_to_timestampMethod · 0.76