- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.to_timestamp#
- DataFrame.to_timestamp(freq=None,how='start',axis=0,copy=None)[source]#
Cast to DatetimeIndex of timestamps, atbeginning of period.
- Parameters:
- freqstr, default frequency of PeriodIndex
Desired frequency.
- how{‘s’, ‘e’, ‘start’, ‘end’}
Convention for converting period to timestamp; start of periodvs. end.
- axis{0 or ‘index’, 1 or ‘columns’}, default 0
The axis to convert (the index by default).
- copybool, default True
If False then underlying input data is not copied.
Note
Thecopy keyword will change behavior in pandas 3.0.Copy-on-Writewill be enabled by default, which means that all methods with acopy keyword will use a lazy copy mechanism to defer the copy andignore thecopy keyword. Thecopy keyword will be removed in afuture version of pandas.
You can already get the future behavior and improvements throughenabling copy on write
pd.options.mode.copy_on_write=True
- Returns:
- DataFrame
The DataFrame has a DatetimeIndex.
Examples
>>>idx=pd.PeriodIndex(['2023','2024'],freq='Y')>>>d={'col1':[1,2],'col2':[3,4]}>>>df1=pd.DataFrame(data=d,index=idx)>>>df1 col1 col22023 1 32024 2 4
The resulting timestamps will be at the beginning of the year in this case
>>>df1=df1.to_timestamp()>>>df1 col1 col22023-01-01 1 32024-01-01 2 4>>>df1.indexDatetimeIndex(['2023-01-01', '2024-01-01'], dtype='datetime64[ns]', freq=None)
Usingfreq which is the offset that the Timestamps will have
>>>df2=pd.DataFrame(data=d,index=idx)>>>df2=df2.to_timestamp(freq='M')>>>df2 col1 col22023-01-31 1 32024-01-31 2 4>>>df2.indexDatetimeIndex(['2023-01-31', '2024-01-31'], dtype='datetime64[ns]', freq=None)