import copy import datetime import pandas as pd read_path = r'D:\data\长清\故障记录_20230420_20240419.csv' df = pd.read_csv(read_path, encoding='gb18030') df['风机名'] = df['风机名'].apply(lambda wind_name: 'A' + wind_name.replace('号风机', '').zfill(2)) df = df[~df['状态码描述'].isin(['高偏航误差穿越', '手动偏航'])] df['激活时间'] = pd.to_datetime(df['激活时间'].apply(lambda x: x[0:x.rfind(":")]), errors='coerce') df['复位时间'] = pd.to_datetime(df['复位时间'].apply(lambda x: x[0:x.rfind(":")]), errors='coerce') df.dropna(subset=['激活时间', '复位时间'], inplace=True) def generate_next_10_min(dt): minute = dt.minute chazhi = 10 - int(minute % 10) now = dt + datetime.timedelta(minutes=chazhi) now = now.replace(second=0, microsecond=0) return now df['begin_time'] = df['激活时间'].apply(generate_next_10_min) df['end_time'] = df['复位时间'].apply(generate_next_10_min) df['chazhi_count'] = ((df['end_time'] - df['begin_time']).dt.seconds) // 600 + 1 result_df = df[df['chazhi_count'] == 1] datas = [[]] for index, row in df[df['chazhi_count'] > 1].iterrows(): for i in range(row['chazhi_count']): data = copy.deepcopy(row.values) data[6] = data[6] + datetime.timedelta(minutes=10 * i) datas.append(data) now_df = pd.DataFrame(datas, columns=df.columns) result_df = pd.concat([result_df, now_df]) result_df.reset_index(inplace=True, drop=True) result_df.sort_values(by=['风机名', '激活时间', 'begin_time'], inplace=True) result_df.to_csv("故障记录.csv", encoding='utf8')