Python Pandas 简明教程

Python Pandas - Date Functionality

扩展时间序列,日期功能在金融数据分析中扮演着重要角色。在使用日期数据时,我们经常会遇到以下问题−

Extending the Time series, Date functionalities play major role in financial data analysis. While working with Date data, we will frequently come across the following −

  1. Generating sequence of dates

  2. Convert the date series to different frequencies

Create a Range of Dates

通过指定周期和频率使用 date.range() 函数,我们可以创建日期序列。默认情况下,范围的频率为天。

Using the date.range() function by specifying the periods and the frequency, we can create the date series. By default, the frequency of range is Days.

import pandas as pd

print pd.date_range('1/1/2011', periods=5)

它的 output 如下所示 −

Its output is as follows −

DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03', '2011-01-04', '2011-01-05'],
   dtype='datetime64[ns]', freq='D')

Change the Date Frequency

import pandas as pd

print pd.date_range('1/1/2011', periods=5,freq='M')

它的 output 如下所示 −

Its output is as follows −

DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-30', '2011-05-31'],
   dtype='datetime64[ns]', freq='M')

bdate_range

bdate_range() 代表业务日期范围。不同于 date_range(),它排除了星期六和星期日。

bdate_range() stands for business date ranges. Unlike date_range(), it excludes Saturday and Sunday.

import pandas as pd

print pd.date_range('1/1/2011', periods=5)

它的 output 如下所示 −

Its output is as follows −

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 日。只需查看您日历中的天数即可。

Observe, after 3rd March, the date jumps to 6th march excluding 4th and 5th. Just check your calendar for the days.

date_rangebdate_range 这样的便捷函数利用了各种频率别名。date_range 的默认频率是日历日,而 bdate_range 的默认频率是工作日。

Convenience functions like date_range and bdate_range utilize a variety of frequency aliases. The default frequency for date_range is a calendar day while the default for bdate_range is a business day.

import pandas as pd
start = pd.datetime(2011, 1, 1)
end = pd.datetime(2011, 1, 5)

print pd.date_range(start, end)

它的 output 如下所示 −

Its output is as follows −

DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03', '2011-01-04', '2011-01-05'],
   dtype='datetime64[ns]', freq='D')

Offset Aliases

将许多字符串别名提供给有用的常见时间序列频率。我们将这些别名称为偏移别名。

A number of string aliases are given to useful common time series frequencies. We will refer to these aliases as offset aliases.

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