如何确定 Pandas 中频率的周期范围?
在 pandas 中,我们可以借助 period_range() 确定带频率的周期范围。 pandas.period_range()是 Pandas 中的通用函数之一,用于返回固定频率 PeriodIndex,默认频率为 day(日历)。
Syntax: pandas.to_numeric(arg, errors=’raise’, downcast=None)
Parameters:
start : Left bound for generating periods
end : Right bound for generating periods
periods : Number of periods to generate
freq : Frequency alias
name : Name of the resulting PeriodIndex
Returns: PeriodIndex
示例 1:
Python3
import pandas as pd
# initialize country
country = ["India", "Australia", "Pak", "Sri Lanka",
"England", "Bangladesh"]
# perform period_range() function
match_date = pd.period_range('8/1/2020', '8/6/2020', freq='D')
# generates dataframes
df = pd.DataFrame(country, index=match_date, columns=['Country'])
df
Python3
import pandas as pd
# initialize country
Course = ["DSA", "OOPS", "DBMS", "Computer Network",
"System design", ]
# perform period_range() function
webinar_month = pd.period_range('8/1/2020', '12/1/2020', freq='M')
# generates dataframes
df = pd.DataFrame(Course, index=webinar_month, columns=['Course'])
df
Python3
import pandas as pd
# initialize gold price
gold_price = ["32k", "34k", "37k", "33k", "38k", "39k", "35k",
"32k", "42k", "52k", "62k", "52k", "38k", "39k",
"35k", "33k"]
# perform period_range() function
price_month = pd.period_range(start=pd.Period('2019Q1', freq='Q'),
end=pd.Period('2020Q2', freq='Q'),
freq='M')
# generates dataframes
df = pd.DataFrame(gold_price, index=price_month, columns=['Price'])
df
输出:
例子
Python3
import pandas as pd
# initialize country
Course = ["DSA", "OOPS", "DBMS", "Computer Network",
"System design", ]
# perform period_range() function
webinar_month = pd.period_range('8/1/2020', '12/1/2020', freq='M')
# generates dataframes
df = pd.DataFrame(Course, index=webinar_month, columns=['Course'])
df
输出:
示例 3:
Python3
import pandas as pd
# initialize gold price
gold_price = ["32k", "34k", "37k", "33k", "38k", "39k", "35k",
"32k", "42k", "52k", "62k", "52k", "38k", "39k",
"35k", "33k"]
# perform period_range() function
price_month = pd.period_range(start=pd.Period('2019Q1', freq='Q'),
end=pd.Period('2020Q2', freq='Q'),
freq='M')
# generates dataframes
df = pd.DataFrame(gold_price, index=price_month, columns=['Price'])
df
输出: