Python Pandas - 从具有特定时间序列频率的 DateTimeIndex 中提取月份数
要从具有特定时间序列频率的DateTimeIndex中提取月数,请使用DateTimeIndex.month属性。
首先,导入所需的库-
import pandas as pd
DatetimeIndex,周期为6,频率为M,即月份。时区是澳大利亚/悉尼-
datetimeindex = pd.date_range('2021-09-24 02:35:55', periods=6, tz='Australia/Sydney',freq='M')显示日期时间索引-
print("DateTimeIndex...\n", datetimeindex)获取月份数,即一月=1,二月=2,...,十二月=12-
print("\nGetting the month number..\n",datetimeindex.month)示例
以下是代码-
import pandas as pd
#DatetimeIndexwithperiod6andfrequencyasMi.e.month
#timezoneisAustralia/Sydney
datetimeindex = pd.date_range('2021-09-24 02:35:55', periods=6, tz='Australia/Sydney',freq='M')
#displayDateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
#displayDateTimeIndex frequency
print("DateTimeIndex frequency...\n", datetimeindex.freq)
#getthemonthnumberi.e.January=1,february=2,...,December=12
print("\nGetting the month number..\n",datetimeindex.month)输出结果这将产生以下输出-
DateTimeIndex...
DatetimeIndex(['2021-09-30 02:35:55+10:00', '2021-10-31 02:35:55+11:00',
'2021-11-30 02:35:55+11:00', '2021-12-31 02:35:55+11:00',
'2022-01-31 02:35:55+11:00', '2022-02-28 02:35:55+11:00'],
dtype='datetime64[ns, Australia/Sydney]', freq='M')
DateTimeIndex frequency...
<MonthEnd>
Getting the month number..
Int64Index([9, 10, 11, 12, 1, 2], dtype='int64')热门推荐
2 送蛇的祝福语简短
4 怎么说祝福语简短
10 年月日祝福语简短
11 八十大寿简短祝福语
12 群里祝福语简短幽默
13 给婴儿祝福语简短的话
14 顾客简短祝福语大全集
15 庆双节祝福语简短
16 年末祝福语简短的文案
17 学生宴祝福语简短的
18 青春告白祖国简短祝福语