Python Pandas – 带时区信息的Python datetime.time对象的numpy数组返回

Python Pandas – 带时区信息的Python datetime.time对象的numpy数组返回

要返回带有时区信息的Python datetime.time对象的numpy数组,请使用 datetimeindex.timetz 属性。

首先,导入所需的库 −

import pandas as pd

创建一个周期为5,频率为微秒的DatetimeIndex,即纳秒 −

datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=5, tz='Australia/Sydney', freq='ns')

显示DateTimeIndex −

print("DateTimeIndex...\n", datetimeindex)

返回带有时区信息的时间戳的时间部分 −

print("\nThe numpy array (time part with timezone)..\n",datetimeindex.timetz)

示例

下面是代码 −

import pandas as pd

# DatetimeIndex with period 5 and frequency as us i.e. nanoseconds
# The timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=5, tz='Australia/Sydney', freq='ns')

# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)

# Returns only the time part of Timestamp with timezone information
print("\nThe numpy array (time part with timezone)..\n",datetimeindex.timetz)

输出

这将产生以下代码−

DateTimeIndex...
DatetimeIndex([ '2021-10-20 02:30:50+11:00',
'2021-10-20 02:30:50.000000001+11:00',
'2021-10-20 02:30:50.000000002+11:00',
'2021-10-20 02:30:50.000000003+11:00',
'2021-10-20 02:30:50.000000004+11:00'],
dtype='datetime64[ns, Australia/Sydney]', freq='N')

The numpy array (time part with timezone)..
[datetime.time(2, 30, 50, tzinfo=<DstTzInfo 'Australia/Sydney' AEDT+11:00:00 DST>)
datetime.time(2, 30, 50, tzinfo=<DstTzInfo 'Australia/Sydney' AEDT+11:00:00 DST>)
datetime.time(2, 30, 50, tzinfo=<DstTzInfo 'Australia/Sydney' AEDT+11:00:00 DST>)
datetime.time(2, 30, 50, tzinfo=<DstTzInfo 'Australia/Sydney' AEDT+11:00:00 DST>)
datetime.time(2, 30, 50, tzinfo=<DstTzInfo 'Australia/Sydney' AEDT+11:00:00 DST>)]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程