【超便利】pandasで日付の分析を効率化!PeriodIndexとday_of_yearの活用術
pandas.PeriodIndex.day_of_year
属性は、PeriodIndex
オブジェクト内の各要素が属する年の何日目かを示す数値を返します。これは、日付の分析や可視化において役立ちます。
構文
period_index.day_of_year
引数
なし
戻り値
Index
オブジェクト:各要素が属する年の何日目かを表す数値
例
import pandas as pd
# PeriodIndexを作成
dates = pd.period_range('2020-01-01', '2020-12-31')
period_index = pd.PeriodIndex(dates)
# day_of_year属性を取得
day_of_year = period_index.day_of_year
# 確認
print(day_of_year)
上記の例では、day_of_year
属性によって、各日付が2020年における何日目かが計算されます。
入力と出力
pandas.PeriodIndex.day_of_year
属性は、PeriodIndex
オブジェクトから数値の Index
オブジェクトを返すため、入力と出力を以下のように分類できます。
入力
period_index
:PeriodIndex
オブジェクト。日付情報を含む必要があります。
出力
day_of_year
:Index
オブジェクト。各要素が属する年の何日目かを表す数値
利点
- 可視化において、日付の分布をわかりやすく表現できます。
- 日付の比較や分析に役立ちます。
- 特定の日付が年の何日目か簡単に確認できます。
- 日付の解析には、
pandas.Timestamp
やdatetime
モジュールと併用することをおすすめします。 day_of_year
属性は、閏年かどうかを考慮しません。
pandas.PeriodIndex
オブジェクトは、さまざまな操作や分析を行うことができます。PeriodIndex
オブジェクトは、start_time
、end_time
、freq
などの属性を持ち、日付情報と周期情報を含みます。pandas.PeriodIndex
オブジェクトは、pandas
ライブラリで時間軸データを表すために使用されます。
- 最新の情報については、公式ドキュメントを参照することをおすすめします。
- 本解説は、
pandas
バージョン 2.2.2 を基に作成されています。
import pandas as pd
# PeriodIndexを作成
dates = pd.period_range('2020-01-01', '2020-12-31')
period_index = pd.PeriodIndex(dates)
# 特定の月の `day_of_year` を抽出
march_day_of_year = period_index[period_index.month == 3].day_of_year
# 確認
print(march_day_of_year)
例2:閏年の day_of_year
と非閏年の day_of_year
を比較
import pandas as pd
# 閏年の PeriodIndexを作成
leap_year_dates = pd.period_range('2020-01-01', '2020-12-31')
leap_year_period_index = pd.PeriodIndex(leap_year_dates)
# 非閏年の PeriodIndexを作成
non_leap_year_dates = pd.period_range('2019-01-01', '2019-12-31')
non_leap_year_period_index = pd.PeriodIndex(non_leap_year_dates)
# 特定の日付における `day_of_year` を比較
leap_day_of_year = leap_year_period_index[leap_year_period_index == pd.Period('2020-02-29')].day_of_year
non_leap_day_of_year = non_leap_year_period_index[non_leap_year_period_index == pd.Period('2019-02-28')].day_of_year
# 確認
print(leap_day_of_year)
print(non_leap_day_of_year)
import pandas as pd
import matplotlib.pyplot as plt
# PeriodIndexを作成
dates = pd.period_range('2020-01-01', '2020-12-31')
period_index = pd.PeriodIndex(dates)
# day_of_year を算出
day_of_year = period_index.day_of_year
# 可視化
plt.plot(day_of_year)
plt.xlabel('Date')
plt.ylabel('Day of Year')
plt.title('Day of Year in 2020')
plt.show()
そこで、pandas.PeriodIndex.day_of_year
の代替方法として、以下の方法を検討することができます。
dt.dayofyear 属性を使用する
pandas.PeriodIndex
オブジェクトを Timestamp
オブジェクトに変換し、dt.dayofyear
属性を使用する方法です。
import pandas as pd
# PeriodIndexを作成
dates = pd.period_range('2020-01-01', '2020-12-31')
period_index = pd.PeriodIndex(dates)
# Timestampオブジェクトに変換
timestamp_index = period_index.to_timestamp()
# day_of_year属性を取得
day_of_year = timestamp_index.dt.dayofyear
# 確認
print(day_of_year)
利点
dt
属性の他のメソッドと併用できる。- 閏年にも対応している。
注意点
to_timestamp()
メソッドを使用するため、処理速度が遅くなる可能性がある。
pandas.Series.dt.dayofyear を使用する
PeriodIndex
オブジェクトを Series
オブジェクトに変換し、dt.dayofyear
属性を使用する方法です。
import pandas as pd
# PeriodIndexを作成
dates = pd.period_range('2020-01-01', '2020-12-31')
period_index = pd.PeriodIndex(dates)
# Seriesオブジェクトに変換
series = pd.Series(period_index)
# day_of_year属性を取得
day_of_year = series.dt.dayofyear
# 確認
print(day_of_year)
利点
to_timestamp()
メソッドを使用しないため、処理速度が速い。
注意点
- 閏年にも対応しているが、
to_timestamp()
メソッドを使用するよりも処理速度が遅い場合がある。
カスタム関数を作成する
状況に合わせて、独自の関数を作成する方法です。
import pandas as pd
def get_day_of_year(period_index):
"""
PeriodIndexオブジェクト内の各要素が属する年の何日目かを返す関数
Args:
period_index: PeriodIndexオブジェクト
Returns:
Indexオブジェクト:各要素が属する年の何日目かを表す数値
"""
days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
day_of_year = []
for period in period_index:
year = period.year
month = period.month
day = period.day
# 閏年の判定
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
days_per_month[1] = 29
# day_of_yearの計算
for i in range(month - 1):
day += days_per_month[i]
day_of_year.append(day)
return pd.Index(day_of_year)
# PeriodIndexを作成
dates = pd.period_range('2020-01-01', '2020-12-31')
period_index = pd.PeriodIndex(dates)
# カスタム関数でday_of_yearを取得
day_of_year = get_day_of_year(period_index)
# 確認
print(day_of_year)
利点
- 状況に合わせて柔軟に処理をカスタマイズできる。
注意点
- 処理速度が遅くなる可能性がある。
- コード 작성の手間がかかる。
選択の指針
上記の方法の中から、状況に合った方法を選択してください。
- 閏年にも確実に対応したい場合は、
dt.dayofyear
- 処理速度が重要であれば、
pandas.Series.dt.dayofyear
を使用するのがおすすめです。