Python Pandas - 从具有特定时间序列频率的 DateTimeIndex 中提取年份
要从具有特定时间序列频率的DateTimeIndex中提取年份,请使用DateTimeIndex.year属性。
首先,导入所需的库-
import pandas as pd
DatetimeIndex,周期为6,频率为Y,即年。时区是澳大利亚/悉尼-
datetimeindex = pd.date_range('2021-09-24 02:35:55', periods=6, tz='Australia/Sydney',freq='Y')显示日期时间索引-
print("DateTimeIndex...\n", datetimeindex)获取年份-
print("\nGetting the year name..\n",datetimeindex.year)示例
以下是代码-
import pandas as pd
#DatetimeIndexwithperiod6andfrequencyasYi.e.years
#timezoneisAustralia/Sydney
datetimeindex = pd.date_range('2021-09-24 02:35:55', periods=6, tz='Australia/Sydney', freq='Y')
#displayDateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
#displayDateTimeIndex frequency
print("DateTimeIndex frequency...\n", datetimeindex.freq)
#gettheyear
print("\nGetting the year name..\n",datetimeindex.year)输出结果这将产生以下输出-
DateTimeIndex...
DatetimeIndex(['2021-12-31 02:35:55+11:00', '2022-12-31 02:35:55+11:00',
'2023-12-31 02:35:55+11:00', '2024-12-31 02:35:55+11:00',
'2025-12-31 02:35:55+11:00', '2026-12-31 02:35:55+11:00'],
dtype='datetime64[ns, Australia/Sydney]', freq='A-DEC')
DateTimeIndex frequency...
<YearEnd: month=12>
Getting the year name..
Int64Index([2021, 2022, 2023, 2024, 2025, 2026], dtype='int64')热门推荐
3 简短古诗词祝福语
10 送别老外祝福语英文简短
11 20岁新年祝福语简短
12 月度员工生日祝福语简短
13 生日上的简短祝福语
14 军官男友结婚祝福语简短
15 教师闺蜜祝福语简短
16 酒吧祝福语男生朋友简短
17 书店过年祝福语简短精辟
18 冬日温馨祝福语简短词语