Python Pandas - 从具有特定时间序列频率的 DateTimeIndex 中提取日期的季度
要从具有特定时间序列频率的 DateTimeIndex 中提取日期的季度,请使用 **DateTimeIndex.quarter**。
首先,导入所需的库 -
import pandas as pd
创建一个周期为 6 且频率为 M(即月份)的 DatetimeIndex。时区为澳大利亚/悉尼 -
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='2M')
显示 DateTimeIndex 频率 -
print("DateTimeIndex frequency...\n", datetimeindex.freq)获取日期的季度 -
print("\nGet the quarter of the date..\n",datetimeindex.quarter)
结果基于以下年份的季度 -
Quarter 1 = 1st January to 31st March Quarter 2 = 1st April to 30th June Quarter 3 = 1st July to 30th September Quarter 4 = 1st October to 31st December
示例
以下是代码 -
import pandas as pd
# DatetimeIndex with period 6 and frequency as M i.e. Month
# The timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='2M')
# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
# display DateTimeIndex frequency
print("DateTimeIndex frequency...\n", datetimeindex.freq)
# Get the quarter of the date
# Result is based on the following quarters of an year:
# Quarter 1 = 1st January to 31st March
# Quarter 2 = 1st April to 30th June
# Quarter 3 = 1st July to 30th September
# Quarter 4 = 1st October to 31st December
print("\nGet the quarter of the date..\n",datetimeindex.quarter)输出
这将生成以下代码 -
DateTimeIndex... DatetimeIndex(['2021-10-31 02:30:50+11:00', '2021-12-31 02:30:50+11:00', '2022-02-28 02:30:50+11:00', '2022-04-30 02:30:50+10:00', '2022-06-30 02:30:50+10:00', '2022-08-31 02:30:50+10:00'], dtype='datetime64[ns, Australia/Sydney]', freq='2M') DateTimeIndex frequency... <2 * MonthEnds> Get the quarter of the date.. Int64Index([4, 4, 1, 2, 2, 3], dtype='int64')
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP