📅  最后修改于: 2020-11-06 05:49:04             🧑  作者: Mango
扩展时间序列后,日期功能在财务数据分析中起着重要作用。使用日期数据时,我们经常会遇到以下情况-
通过指定日期和频率使用date.range()函数,我们可以创建日期序列。默认情况下,范围的频率为天。
import pandas as pd
print pd.date_range('1/1/2011', periods=5)
其输出如下-
DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03', '2011-01-04', '2011-01-05'],
dtype='datetime64[ns]', freq='D')
import pandas as pd
print pd.date_range('1/1/2011', periods=5,freq='M')
其输出如下-
DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-30', '2011-05-31'],
dtype='datetime64[ns]', freq='M')
bdate_range()代表营业日期范围。与date_range()不同,它不包括星期六和星期日。
import pandas as pd
print pd.date_range('1/1/2011', periods=5)
其输出如下-
DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03', '2011-01-04', '2011-01-05'],
dtype='datetime64[ns]', freq='D')
请注意,3月3日之后,日期跳至3月6日(不包括4日和5日)。只需检查日历中的日期即可。
诸如date_range和bdate_range之类的便利功能利用了多种频率别名。 date_range的默认频率是日历日,而bdate_range的默认频率是工作日。
import pandas as pd
start = pd.datetime(2011, 1, 1)
end = pd.datetime(2011, 1, 5)
print pd.date_range(start, end)
其输出如下-
DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03', '2011-01-04', '2011-01-05'],
dtype='datetime64[ns]', freq='D')
为有用的通用时间序列频率提供了许多字符串别名。我们将这些别名称为偏移别名。
Alias | Description | Alias | Description |
---|---|---|---|
B | business day frequency | BQS | business quarter start frequency |
D | calendar day frequency | A | annual(Year) end frequency |
W | weekly frequency | BA | business year end frequency |
M | month end frequency | BAS | business year start frequency |
SM | semi-month end frequency | BH | business hour frequency |
BM | business month end frequency | H | hourly frequency |
MS | month start frequency | T, min | minutely frequency |
SMS | SMS semi month start frequency | S | secondly frequency |
BMS | business month start frequency | L, ms | milliseconds |
Q | quarter end frequency | U, us | microseconds |
BQ | business quarter end frequency | N | nanoseconds |
QS | quarter start frequency |