Pandas 返回python datetime.time对象中的numpy数组
要返回python datetime.time 对象的numpy数组,请在Pandas中使用datetimeindex.time属性。
首先,导入所需的库 -
import pandas as pd
创建一个周期为3,频率为us(即纳秒)的DatetimeIndex。时区为澳大利亚/悉尼 -
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=3, tz='Australia/Sydney', freq='ns')
显示DatetimeIndex -
print("DateTimeIndex...\n", datetimeindex)
只返回不带时区信息的时间戳的时间部分 -
print("\nThe numpy array (time part)..\n",datetimeindex.time)
示例
以下为代码 -
import pandas as pd # DatetimeIndex with period 3 and frequency as us i.e. nanoseconds # The timezone is Australia/Sydney datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=3, tz='Australia/Sydney', freq='ns') # display DateTimeIndex print("DateTimeIndex...\n", datetimeindex) # Returns only the date part of Timestamps without timezone information print("\nThe numpy array (date part)..\n",datetimeindex.date) # Returns only the time part of Timestamps without timezone information print("\nThe numpy array (time part)..\n",datetimeindex.time)
输出
以下为代码生成结果 -
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'], dtype='datetime64[ns, Australia/Sydney]', freq='N') The numpy array (date part).. [datetime.date(2021, 10, 20) datetime.date(2021, 10, 20) datetime.date(2021, 10, 20)] The numpy array (time part).. [datetime.time(2, 30, 50) datetime.time(2, 30, 50) datetime.time(2, 30, 50)]
广告