Open In App

Python | Pandas Timestamp.replace

Last Updated : 04 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Pandas Timestamp.replace Syntax

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

Timestamp.replace in Pandas Examples

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:

Change time in timestamp using Pandas

The code shows how to change particular parts of a timestamp using the Pandas Timestamp.replace method.

Python3




import pandas as pd
t = pd.Timestamp('2000-10-15 00:00:00')
t = t.replace(hour=13, minute=0, second=0)
print(t)


Ouput:

2000-10-15 13:00:00

Modifying Year and Month of a Timestamp Object using Pandas

The 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.

Python3




import pandas as pd
 
# Create a Timestamp object
timestamp = pd.Timestamp("2022-06-15 12:30:45")
 
# Replace the year
modified_timestamp = timestamp.replace(year=2023, month=10)
 
print("Org_Timestamp:", timestamp)
print("Mod_Timestamp:", modified_timestamp)


Output:

Org_Timestamp: 2022-06-15 12:30:45
Mod_Timestamp: 2023-10-15 12:30:45

Creating a Timestamp Object with US/Central Timezone

Use Timestamp.replace() function to replace the year value in the given Timestamp.

Python3




# importing pandas as pd
import pandas as pd
 
# Create the Timestamp object
ts = pd.Timestamp(year = 2011,  month = 11, day = 21,
                  hour = 10, second = 49, tz = 'US/Central')
 
# Print the Timestamp object
print(ts)


Output:

 2011-11-21 10:00:49-06:00

Now we will use the Timestamp.replace() function to replace the current year in the object with 2019.

Python3




# replace year
ts.replace(year = 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.

Creating Timestamp Object with Europe/Berlin Timezone

Use Timestamp.replace() function to replace the year, month and hour value in the given Timestamp.

Python3




# importing pandas as pd
import pandas as pd
 
# Create the Timestamp object
ts = pd.Timestamp(year = 2009, month = 5, day = 31,
                  hour = 4, second = 49, tz = 'Europe/Berlin')
 
# Print the Timestamp object
print(ts)


Output :

2009-05-31 04:00:49+02:00

Now we will use the Timestamp.replace() function to replace the current year, month and hour value in the object.

Python3




# replace year, month and hour value
ts.replace(year = 2019, month = 12, hour = 1)


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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads