In pandas, pandas.Timestamp
contains the replace()
function to replace the date and time fields of the given Timestamp object. It is equivalent to Python's datetime
object.
The replace()
function takes the syntax given below:
Timestamp.replace(year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, nanosecond=None, tzinfo=<class 'object'>)
The replace()
function take the following optional parameter values:
year
, month
, and day
: This represents the date fields. hour
, minute
, second
, microsecond
, and nanosecond
: This represents the time fields. tzinfo
: This enquires information about the Timestamp object.Note: The
replace()
function must take at least one of the date or time fields parameters.
The replace()
function returns a Timestamp object with replaced fields.
Let's look at the code below:
# A code to illustrate the replace() function in Pandas# importing the pandas libraryimport pandas as pd# creating a Timestamp objecta = pd.Timestamp(1999, 12, 15)# printing the Timestamp objectprint(a)# replacing the year, month, day and hourb = a.replace(year=2022, month=4, day=30, hour=9)print(b)
a
. a
.a
using the replace()
function. The result is assigned to b
variable.b
to the console.