• 添加数据,满足所有的 时间序列,以nan值为插入函数


    import numpy as np
    import pandas as pd
    import datetime
    import re
    import time
    import glob
    import copy

    def buchong_shuju(path_in):
    time_parse = lambda date: datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
    df = pd.read_csv(path_in, encoding='utf-8', parse_dates=['date'],
    date_parser=time_parse)
    c= pd.read_csv(r'. an值补充.csv')
    d = []
    a = np.array(c['hb'])

    helper = pd.DataFrame({'date': pd.date_range(df['date'].min(), df['date'].max(), freq='H')})
    df1 = pd.merge(df,helper,on = 'date',how='outer').sort_values('date')

    op1 = df1.groupby(df1['date'])
    for date,op2 in op1:
    # print(len(op2))

    if len(op2)==1:
    s = op2['date']

    op2 =copy.deepcopy(c)
    op2['date'] = op2['date'].apply(lambda x:s)
    # print(op2)
    # d.append(op2)
    # s =d
    # print(s)
    op2 = op2.sort_values('hb')
    d.append(op2.sort_values('hb'))
    print(d)

    return d
    def save_weniian(path_in,path_out):
    with open(path_in,'r')as f:
    df = pd.DataFrame()
    df = df.append(buchong_shuju(path_in))
    # print(df)
    df.to_csv(path_out,index=False)
    print('Finished ' + path_in)
    if __name__ == "__main__":
    ff = glob.glob("*_日t.csv")
    if not ff:
    print('No files to convert.')
    exit()
    else:
    print("Detected ASCII *日_.csv files: ", " ".join(ff))
    for f in ff:
    path_in = f
    path_out = re.sub(r'(?i)t.csv', 't_1.csv', f)
    s = buchong_shuju(f)
    d = save_weniian(path_in, path_out)



  • 相关阅读:
    单词 统计
    第九周周进度报告
    日常总结
    Docker 初识
    读书笔记
    《未来简史》有感
    自己的Java规范文档
    nginx自动部署脚本
    jdk 自动化脚本
    我Java学习时的模样(三)
  • 原文地址:https://www.cnblogs.com/chenyun-delft3d/p/13656691.html
Copyright © 2020-2023  润新知