Python Pandas - 计算在指定频率下转换为 PeriodArray 的索引值和索引之间的差异的 TimedeltaArray
要计算索引值与在指定频率下转换为PeriodArray的索引之间的差异的TimedeltaArray,请使用方法。使用freq参数设置频率。datetimeindex.to_perioddelta()
首先,导入所需的库-
import pandas as pd
创建一个日期时间索引,周期为7,频率为Y,即年份-
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, freq='2Y')显示日期时间索引-
print("DateTimeIndex...\n", datetimeindex)计算索引值与转换为PeriodArray的索引之间的差异的TimedeltaArray。我们已经使用值为“M”的“freq”参数设置了周期频率-
print("\nConvert DateTimeIndex to PeriodDelta...\n",
datetimeindex.to_perioddelta(freq='M'))示例
以下是代码-
import pandas as pd
#DatetimeIndexwithperiod7andfrequencyasYi.e.year
#timezoneisAustralia/Adelaide
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, freq='2Y')
#displayDateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
#displayDateTimeIndex frequency
print("DateTimeIndex frequency...\n", datetimeindex.freq)
#ConvertDateTimeIndextoPeriod
# We have set the frequency as Month using the "freq" parameter with value 'M'
print("\nConvert DateTimeIndex to Period...\n",
datetimeindex.to_period(freq='M'))
#CalculateTimedeltaArrayofdifferencebetweenindexvaluesandindexconvertedtoPeriodArray
# We have set the Period frequency using the "freq" parameter with value 'M'
print("\nConvert DateTimeIndex to PeriodDelta...\n",
datetimeindex.to_perioddelta(freq='M'))输出结果这将产生以下代码-
DateTimeIndex... DatetimeIndex(['2021-12-31 07:20:32.261811624', '2023-12-31 07:20:32.261811624', '2025-12-31 07:20:32.261811624', '2027-12-31 07:20:32.261811624', '2029-12-31 07:20:32.261811624'], dtype='datetime64[ns]', freq='2A-DEC') DateTimeIndex frequency... <2 * YearEnds: month=12> Convert DateTimeIndex to Period... PeriodIndex(['2021-12', '2023-12', '2025-12', '2027-12', '2029-12'], dtype='period[M]') Convert DateTimeIndex to PeriodDelta... TimedeltaIndex(['30 days 07:20:32.261811624', '30 days 07:20:32.261811624', '30 days 07:20:32.261811624', '30 days 07:20:32.261811624', '30 days 07:20:32.261811624'], dtype='timedelta64[ns]', freq=None)
热门推荐
10 幼师元旦祝福语大全简短
11 祝福语简短幽默的句子
12 给亲人送祝福语简短
13 月度员工生日祝福语简短
14 奶奶生日祝福语 简短独特
15 长辈过生祝福语简短
16 给英雄的祝福语简短
17 一生祝福语简短
18 祝老妈生日祝福语简短