![]() |
VOOZH | about |
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages that makes importing and analyzing data much easier.
Pandas Timestamp.replace() function is used to replace the member values of the given Timestamp. The function implements datetime.replace, and it also handles nanoseconds.
Syntax : Timestamp.replace()
Parameters :
year : int
month : int
day : int
hour : int
minute : int
second : int
microsecond : int
nanosecond : int
tzinfo : int
fold : int
Return : Timestamp with fields replaced
The replace() method of the Timestamp object in Pandas is used to modify the components of a timestamp, such as the year, month, day, hour, minute, and second. It takes various parameters to specify the components to be replaced and their new values. Here we will see different examples of how to use this method:
The code shows how to change particular parts of a timestamp using the Pandas Timestamp.replace method.
Ouput:
2000-10-15 13:00:00The code shows how to build an object representing a Pandas Timestamp and then edit individual parts of the timestamp using the Pandas Timestamp.replace method.
Output:
Org_Timestamp: 2022-06-15 12:30:45
Mod_Timestamp: 2023-10-15 12:30:45
Use Timestamp.replace() function to replace the year value in the given Timestamp.
Output:
2011-11-21 10:00:49-06:00Now we will use the Timestamp.replace() function to replace the current year in the object with 2019.
Output :
Timestamp('2019-11-21 10:00:49-0600', tz='US/Central')As we can see in the output, the Timestamp.replace() function has returned a Timestamp object with year value equal to 2019.
Use Timestamp.replace() function to replace the year, month and hour value in the given Timestamp.
Output :
2009-05-31 04:00:49+02:00Now we will use the Timestamp.replace() function to replace the current year, month and hour value in the object.
Output :
Timestamp('2019-12-31 01:00:49+0100', tz='Europe/Berlin')As we can see in the output, the Timestamp.replace() function has returned a Timestamp object with year value equal to 2019, month value equal to 12 and hour value equal to 1.