import pandas as pd

# -->   数据转换
# *************************************************************************************************************
# *************************************************************************************************************
# .............................................................................................................
# .............................................................................................................

# ......路径准备
# -------------------------------------------------------------------------------------------------------------
path_ar = []
asset = 'AUDNZD'

# 09年路径
month_y09 = ['05', '06', '07', '08', '09', '10', '11', '12']
for each_month in month_y09:
    path_ar.append('I:/fdata_pep/' +
                   asset + '/09/' + asset + '-2009-' + each_month + '.csv')

# 10~15年路径
year = ['10', '11', '12', '13', '14', '15']
month = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
for each_year in year:
    for each_month in month:
        path_ar.append('I:/fdata_pep/' +
                       asset + '/' + each_year + '/' + asset + '-20' + each_year + '-' + each_month + '.csv')

# 16年路径
month_y16 = ['01', '02', '03', '04', '05', '06']
for each_month in month_y16:
    path_ar.append('I:/fdata_pep/' + asset + '/16/' + asset + '-2016-' + each_month + '.csv')

# ......读取数据
# -------------------------------------------------------------------------------------------------------------
for each_path in path_ar:
    print(each_path)
    year_save = each_path.split('/' + asset + '-')[0][-2:]

    df = pd.read_csv(each_path, names=['forex', 'date', 'price1', 'price2'])  # 读取文件数据
    df = df.dropna()  # 移除nan行
    df['price'] = (df['price1'] + df['price2']) / 2.0  # 增加平均价格
    del df['forex']  # 删除符号
    del df['price1']  # 删除价格1
    del df['price2']  # 删除价格2
    df.index = df['date']  # 将索引设置为‘date’
    df = df[~df.index.duplicated(keep='first')]  # 移除'date'重复的行
    df['day'] = df['date'].apply(lambda x: x.split(' ')[0])
    del df['date']

    # 按天分组保存数据
    for name, group in df.groupby('day'):
        del group['day']
        group.index = pd.DatetimeIndex(group.index)  # 索引设置为‘date’ DatetimeIndex格式
        group = group.resample('500L').mean().interpolate()  # 重采样并插值 间隔0.5S
        group['price'] = group['price'].apply(lambda x: '%.6f' % x)  # 格式化平均价格为6位小数
        path2_save = 'I:/fdata_pep/' + asset + '/' + year_save + '/' + name + '.csv'
        group.to_csv(path2_save, header=False)  # 不写列标签

# .............................................................................................................
# .............................................................................................................
# *************************************************************************************************************
# *************************************************************************************************************
