Python| Pandas Timestamp.tz_localize
Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
Pandas Timestamp.tz_localize()函数将原始时间戳转换为本地时区,或从 tz-aware Timestamp 中删除时区。
Syntax :Timestamp.tz_localize()
Parameters :
tz : Time zone for time which Timestamp will be converted to. None will remove timezone holding local time.
ambiguous : bool, ‘NaT’, default ‘raise’
errors : ‘raise’, ‘coerce’, default ‘raise’
Return : localized : Timestamp
示例 #1:使用 Timestamp.tz_localize()函数将 tz-aware Timestamp 转换为简单的 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)
Python3
# convert to naive Timestamp
ts.tz_localize(tz = None)
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)
# Print the Timestamp object
print(ts)
Python3
# set to 'US / Pacific'
ts.tz_localize(tz = 'US/Pacific')
输出 :
现在我们将使用 Timestamp.tz_localize()函数将 tz-aware Timestamp 转换为 naive Timestamp。
Python3
# convert to naive Timestamp
ts.tz_localize(tz = None)
输出 :
正如我们在输出中看到的,Timestamp.tz_localize()函数已将给定的 Timestamp 转换为一个简单的 Timestamp。示例 #2:使用 Timestamp.tz_localize()函数将给定的原始 Timestamp 转换为 tz 感知 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)
# Print the Timestamp object
print(ts)
输出 :
现在我们将使用 Timestamp.tz_localize()函数将 ts 对象的时区设置为“US/Pacific”。
Python3
# set to 'US / Pacific'
ts.tz_localize(tz = 'US/Pacific')
输出 :
正如我们在输出中看到的,Timestamp.tz_localize()函数已将给定对象的时区设置为“美国/太平洋”。