Pandasで四半期の終わりを判定する:MonthBegin.is_quarter_endの使い方と代替方法
pandas.tseries.offsets.MonthBegin.is_quarter_end
は、Pandasライブラリで提供される「Data Offsets」機能の一つで、特定の日付が四半期の終わりかどうかを判断するためのメソッドです。
機能
- 返値は、True : 日付が四半期の終わり、False : 日付が四半期の終わりではない
- 引数として
Timestamp
型のオブジェクトを受け取ります。
例
import pandas as pd
# 2024年の各月の1日をリストに格納
dates = pd.to_datetime(['2024-01-01', '2024-02-01', '2024-03-01', '2024-04-01', '2024-05-01', '2024-06-01'])
# 各日付が四半期の終わりかどうかを判定
for date in dates:
is_quarter_end = pd.tseries.offsets.MonthBegin(n=1).is_quarter_end(date)
print(f"{date} : {is_quarter_end}")
出力
2024-01-01 : False
2024-02-01 : False
2024-03-01 : False
2024-04-01 : True
2024-05-01 : False
2024-06-01 : False
is_quarter_end
メソッドは、MonthBegin
オフセットを使用して、日付が四半期の終わりに一致するかどうかを判定します。MonthBegin
オフセットは、月の最初の日に移動します。- 四半期は、一般的に1月-3月、4月-6月、7月-9月、10月-12月の3ヶ月間を指します。
- 四半期の終わりにタスクをスケジュールする
- 四半期ごとのレポートを作成する
- 四半期の売上データを分析する際、四半期の終わりにどのような傾向があるかを確認する
Identifying Quarter-End Dates in a DataFrame
In this example, we'll create a DataFrame with dates and check which ones fall on the end of a quarter:
import pandas as pd
# Create a DataFrame with dates
dates = pd.to_datetime(['2023-01-10', '2023-03-31', '2023-05-15', '2023-07-01', '2023-09-20', '2023-11-14', '2024-02-29'])
df = pd.DataFrame({'Date': dates})
# Add a column indicating if the date is a quarter-end
df['Is Quarter End'] = df['Date'].apply(lambda x: pd.tseries.offsets.MonthBegin(n=1).is_quarter_end(x))
print(df)
Output
Date Is Quarter End
0 2023-01-10 False
1 2023-03-31 True
2 2023-05-15 False
3 2023-07-01 True
4 2023-09-20 False
5 2023-11-14 False
6 2024-02-29 False
Filtering Quarter-End Dates
This example filters a DataFrame to include only dates that fall on the end of a quarter:
import pandas as pd
# Create a DataFrame with dates
dates = pd.to_datetime(['2023-01-10', '2023-03-31', '2023-05-15', '2023-07-01', '2023-09-20', '2023-11-14', '2024-02-29'])
df = pd.DataFrame({'Date': dates})
# Filter for quarter-end dates
quarter_end_dates = df[df['Date'].apply(lambda x: pd.tseries.offsets.MonthBegin(n=1).is_quarter_end(x))]
print(quarter_end_dates)
Output
Date
0 2023-03-31
3 2023-07-01
Calculating Quarter-End Dates
This example calculates the quarter-end dates for a given year:
import pandas as pd
# Specify the year
year = 2024
# Create a list of month names
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
# Calculate quarter-end dates
quarter_end_dates = []
for i in range(1, 5):
quarter_end_date = pd.Timestamp(f"{year}-{i*3}-31")
if pd.tseries.offsets.MonthBegin(n=1).is_quarter_end(quarter_end_date):
quarter_end_dates.append(quarter_end_date)
print(quarter_end_dates)
[Timestamp('2024-03-31 00:00:00'), Timestamp('2024-06-30 00:00:00'), Timestamp('2024-09-30 00:00:00'), Timestamp('2024-12-31 00:00:00')]
Using quarter attribute and comparison
import pandas as pd
# Create a DataFrame with dates
dates = pd.to_datetime(['2023-01-10', '2023-03-31', '2023-05-15', '2023-07-01', '2023-09-20', '2023-11-14', '2024-02-29'])
df = pd.DataFrame({'Date': dates})
# Check if the last day of the quarter matches the current date
df['Is Quarter End'] = df['Date'].dt.is_month_end & (df['Date'].dt.quarter == df['Date'].dt.is_quarter_end)
print(df)
Output
Date Is Quarter End
0 2023-01-10 False
1 2023-03-31 True
2 2023-05-15 False
3 2023-07-01 True
4 2023-09-20 False
5 2023-11-14 False
6 2024-02-29 False
This approach utilizes the dt.is_month_end
and dt.quarter
attributes to determine if the date is the last day of the month and if the month corresponds to the end of a quarter.
Custom function using qtr_end function
import pandas as pd
def qtr_end(date):
# Calculate the quarter end date
qtr_end_date = pd.Timestamp(f"{date.year}-{date.quarter*3}-31")
return qtr_end_date == date
# Create a DataFrame with dates
dates = pd.to_datetime(['2023-01-10', '2023-03-31', '2023-05-15', '2023-07-01', '2023-09-20', '2023-11-14', '2024-02-29'])
df = pd.DataFrame({'Date': dates})
# Apply the custom function to identify quarter-end dates
df['Is Quarter End'] = df['Date'].apply(qtr_end)
print(df)
Output
Date Is Quarter End
0 2023-01-10 False
1 2023-03-31 True
2 2023-05-15 False
3 2023-07-01 True
4 2023-09-20 False
5 2023-11-14 False
6 2024-02-29 False
This approach defines a custom function qtr_end
that calculates the quarter-end date for a given date and checks if it matches the current date.