changing_hebing_guzhang.py 1.6 KB

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