Python Pandas Timestamp.astimezone

Python Pandas Timestamp.astimezone

Python是一种进行数据分析的伟大语言,主要是因为以数据为中心的Python包的奇妙生态系统。Pandas就是这些包中的一个,它使导入和分析数据变得更加容易。

Pandas Timestamp.astimezone()函数将有tz意识的Timestamp转换成另一个时区。对于无tz的Timestamp对象,该函数会抛出一个错误。

语法 :

Timestamp.astimezone()

参数 :
tz :时间戳将被转换为的时间的时区。

返回:转换:时间戳

例子#1:使用Timestamp.astimezone()函数将给定对象的时区改为’Europe/Berlin’。

# 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)

输出 :

Python Pandas Timestamp.astimezone

现在我们将使用Timestamp.astimezone()函数将ts对象的时区改为所需的时区。

# change the timezone to 'Europe/Berlin'
ts.astimezone('Europe/Berlin')

输出 :

Python Pandas Timestamp.astimezone

正如我们在输出中看到的,Timestamp.astimezone()函数已经将给定的Timestamp对象的时区从 “美国/中部 “改为 “欧洲/柏林”。

示例#2:使用Timestamp.astimezone()函数将给定对象的时区改为’Asia/Calcutta’。

# 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)

输出 :

Python Pandas Timestamp.astimezone

现在我们将使用Timestamp.astimezone()函数将ts对象的时区改为所需的时区。

# change the timezone to 'Asia / Calcutta'
ts.astimezone('Asia/Calcutta')

输出 :

Python Pandas Timestamp.astimezone

正如我们在输出中看到的,Timestamp.astimezone()函数已经将给定的Timestamp对象的时区从 “欧洲/柏林 “改为 “亚洲/卡尔库塔”。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程