pythonimport pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import re
# Define a function to convert the date format
def convert_japanese_date(date_str):
era, year, month, day = re.match(r'([A-Z])(\d+)\.(\d+)\.(\d+)', date_str).groups()
if era == 'S': # Showa era
year = int(year) + 1925
elif era == 'H': # Heisei era
year = int(year) + 1988
elif era == 'R': # Reiwa era
year = int(year) + 2018
return f'{year}-{month}-{day}'
# Load the data with Shift-JIS encoding
data = pd.read_csv('/mnt/data/jgbcm_all.csv', encoding='shift_jis')
# Remove header row and reset column names
data.columns = data.iloc[0]
data = data[1:]
# Convert the date column to datetime
data['基準日'] = pd.to_datetime(data['基準日'].apply(convert_japanese_date))
# Replace '-' with NaN and convert the 10 year bond yield to numeric
data['10年'] = pd.to_numeric(data['10年'].replace('-', float('NaN')))
# Filter the data starting from Showa 61 year, July 5th
start_date = pd.to_datetime('1986-07-05')
filtered_data = data[data['基準日'] >= start_date].copy()
# Calculate the 30-day moving average of the yield
filtered_data['10年_30日移動平均'] = filtered_data['10年'].rolling(window=30).mean()
# Plot the 30-day moving average of the 10-year bond yield
plt.figure(figsize=(10, 6))
plt.plot(filtered_data['基準日'], filtered_data['10年_30日移動平均'])
plt.title('30-Day Moving Average of 10 Year Japanese Government Bond Yield Over Time')
plt.xlabel('Date')
plt.ylabel('Yield (%)')
plt.yticks(np.arange(0, filtered_data['10年_30日移動平均'].max() + 0.5, 0.5))
plt.grid(True)
plt.show()