Python Pandas Timestamp.replace
Python是一种进行数据分析的伟大语言,主要是因为以数据为中心的Python包的奇妙生态系统。Pandas就是这些包中的一个,它使导入和分析数据变得更加容易。
Pandas Timestamp.replace()函数用于替换给定Timestamp的成员值。该函数实现了datetime.replace,并且它还能处理纳秒数。
语法 :
Timestamp.replace()
参数 :
year : int
month: int
day : int
hour : int
minute : int
second:int
microsecond : int
nanosecond : int
tzinfo : int
fold : int
返回:时间戳,字段被替换。
例子#1:使用Timestamp.replace()函数来替换给定Timestamp中的年份值。
# 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)
输出 :

现在我们将使用Timestamp.replace()函数将对象中的当前年份替换为2019年。
# replace year
ts.replace(year = 2019)
输出 :

正如我们在输出中看到的,Timestamp.replace()函数返回了一个年值等于2019的Timestamp对象。
例子2:使用Timestamp.replace()函数来替换给定Timestamp中的年、月、小时值。
# 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)
输出 :

现在我们将使用Timestamp.replace()函数来替换对象中的当前年、月、小时值。
# replace year, month and hour value
ts.replace(year = 2019, month = 12, hour = 1)
输出 :

正如我们在输出中看到的,Timestamp.replace()函数返回了一个Timestamp对象,其年值等于2019年,月值等于12,小时值等于1。
极客教程