Python Pandas DatetimeIndex.tz_convert()
Python是一种进行数据分析的伟大语言,主要是因为以数据为中心的Python包的奇妙生态系统。Pandas就是这些包中的一个,它使导入和分析数据变得更加容易。
Pandas DatetimeIndex.tz_convert()函数将具有tz意识的DatetimeIndex从一个时区转换到另一个时区。该函数需要一个输入参数,即我们要将当前DatetimeIndex对象转换为哪个时区。
语法: DatetimeIndex.tz_convert(tz)
参数 :
tz :时间的时区。相应的时间戳将被转换为DatetimeIndex的这个时区。无的tz将转换为UTC并删除时区信息。
返回 : normalized : DatetimeIndex
示例#1:使用DatetimeIndex.tz_convert()函数将给定的DatetimeIndex对象转换为所需的时区。
# importing pandas as pd
import pandas as pd
# Create the DatetimeIndex
# Here 'Q' represents quarter end frequency
didx = pd.DatetimeIndex(start ='2000-01-15 08:00', freq ='Q',
periods = 4, tz ='Asia/Calcutta')
# Print the DatetimeIndex
print(didx)
输出 :
现在我们要将时区从 “亚洲/卡尔卡特 “转换为 “美国/中部”。
# Convert the timezone to 'US / Central'
didx.tz_convert('US/Central')</div>
输出 :
正如我们在输出中看到的,该函数已经改变了didx对象的时区。
示例#2:使用DatetimeIndex.tz_convert()函数将给定的DatetimeIndex对象转换为所需的时区。
# importing pandas as pd
import pandas as pd
# Create the DatetimeIndex
# Here 'Q' represents quarter end frequency
didx = pd.DatetimeIndex(start ='2000-01-15 08:00', freq ='Q',
periods = 4, tz ='Asia/Calcutta')
# Print the DatetimeIndex
print(didx)
输出 :
现在我们要将时区从 “亚洲/卡尔卡塔 “转换为 “欧洲/柏林”。
# Convert the timezone to 'Europe / Berlin'
didx.tz_convert('Europe/Berlin')
输出 :
正如我们在输出中看到的,该函数已经改变了didx对象的时区。