Time index
import pandas as pd
PERIODS = 3
# 年 '2020-12-31', '2021-12-31', '2022-12-31'
print(pd.date_range('1/1/2020', periods=PERIODS, freq='Y'))
# 同上
print(pd.date_range('20200101', periods=PERIODS, freq='Y'))
# 月 '2020-01-31', '2020-02-29', '2020-03-31'
print(pd.date_range('1/1/2020', periods=PERIODS, freq='M'))
# 日 '2020-01-01', '2020-01-02', '2020-01-03' D是freq默认值
print(pd.date_range('1/1/2020', periods=PERIODS, freq='D'))
# 时
print(pd.date_range('1/1/2020', periods=PERIODS, freq='H'))
# 分 也可以使用min
print(pd.date_range('1/1/2020', periods=PERIODS, freq='T'))
# 秒
print(pd.date_range('1/1/2020', periods=PERIODS, freq='S'))
# 季度 '2020-03-31', '2020-06-30', '2020-09-30', '2020-12-31'
print(pd.date_range('1/1/2020', periods=4, freq='Q'))
# 2020-01-01 00:00:00', '2020-01-01 01:20:00', '2020-01-01 02:40:00'
print(pd.date_range('1/1/2020', periods=3, freq='1H20min'))
date_range can generate a list of DatetimeIndex type: the
first parameter is: base time,
the second parameter is: how many to generate, and
the third parameter is: the time of each addition
IntervalIndex
interval_range(
start=None, end=None, periods=None, freq=None, name=None, closed="right"
)
parameter | Description |
---|---|
start | Starting value |
end | End value |
periods | How many data are generated |
freq | It can be understood as the step size, which can be a number, a string, a DateOffset, for example: 5,'D', '5H' |
name | The name of IntervalIndex |
closed | Interval opening and closing control,'left','right','both','neither' |
import pandas as pd
interval_index_one = pd.interval_range(start=0, end=20, freq=5)
print(type(interval_index_one))
print(interval_index_one)
data = [(0, 30), (60, 80), (90, 100)]
interval_index_two = pd.IntervalIndex.from_tuples(data)
print(interval_index_two)