Python-pandas-date-functionality
提供:Dev Guides
Pythonパンダ-日付機能
時系列を拡張する日付機能は、財務データ分析で重要な役割を果たします。 日付データで作業している間、私たちは頻繁に次のものに遭遇します-
- 日付のシーケンスを生成する
- 日付シリーズを異なる頻度に変換する
日付の範囲を作成する
期間と頻度を指定して* 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
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日以降、日付は4日と5日を除いて6月3日にジャンプします。 カレンダーを確認してください。
*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 |