utils.py12 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import consts;
  2. import numpy as np;# 近3月开户最高贷款本金
  3. import time;
  4. import math;
  5. import logResult;
  6. log4r = logResult.logger
  7. import json;
  8. #某个日期离当前时间
  9. def difDate(dateStr):
  10. return int(str(np.datetime64(np.datetime64(time.strftime("%Y-%m-%d")), "D") -np.datetime64(dateStr, "D")).split(" ")[0]);
  11. #某个日期离报告期天数
  12. def difDateReportTime(reportTime,dateStr):
  13. return int(str(np.datetime64(reportTime, "D") -np.datetime64(dateStr, "D")).split(" ")[0])
  14. # 最近几个月的日期
  15. def getLastMonthDate(dateStr,month):
  16. lastMonthDateStr = dateStr.replace(".", "-")
  17. # 最近一个月
  18. lastMonthDate = np.datetime64(lastMonthDateStr, "D") - np.timedelta64(30 * month, 'D')
  19. return str(lastMonthDate);
  20. #离当前时间月份
  21. def difMonthNow(dateStr):
  22. return math.ceil(int(str(np.datetime64(time.strftime("%Y-%m-%d")) - np.datetime64(dateStr.replace('.', '-'), "D")).split(" ")[0]) / 30);
  23. #离报告时间月份
  24. def difMonthReportTime(dateStr,reportTime):
  25. try:
  26. reportTimeTmp = np.datetime64(reportTime, "D");
  27. dateStrTmp = np.datetime64(dateStr, "D");
  28. except:
  29. print(dateStr+' 解析截止日期报错')
  30. month = int(str(reportTimeTmp - dateStrTmp ).split(" ")[0]) / 30;
  31. return math.ceil(month)
  32. #转json
  33. def toJson(df):
  34. jsonStr = df.to_json(orient="index",force_ascii=False);
  35. # print(json)
  36. log4r.info(jsonStr)
  37. reult = formatJson(json.loads(jsonStr));
  38. return reult
  39. def formatJson(jsonObj):
  40. json_dicts = json.dumps(jsonObj, indent=4, ensure_ascii=False)
  41. return json_dicts;
  42. # 判断是否有表头
  43. def checkHeader(df,allHeaders):
  44. result = False
  45. headerList = df.loc[0, :].tolist() # 第0行为表头
  46. headerList1 = []
  47. if df.index.size>1:
  48. headerList1 = df.loc[1, :].tolist() # 第1行为表头
  49. for header in allHeaders: # 第0行或者1行为表头的话,代表不是数据,否则是数据
  50. if list(filter(None, header)) == list(filter(None, headerList)) or list(filter(None, header)) == list(
  51. filter(None, headerList1)):
  52. result = True;
  53. break;
  54. return result;
  55. #替换还款状态规则
  56. #1、2、3、4、5、6、7、D、Z、G、B Z/B=7
  57. def replacePayRcdStatus(df):
  58. if df.empty:
  59. return df;
  60. df['还款状态'] = df['还款状态'].map({"/":0,"*":0,"C":0,"N":0,"#":0,"M":0,"G":7,"D":1,"":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7})
  61. return df;
  62. def doFilterCalc(self,dfx):
  63. dfx = dfx.replace('--', 0)
  64. return dfx;
  65. #开户数使用,出现逾期的情况
  66. def replacePayRcdStatusOverdue(df):
  67. if df.empty:
  68. return df;
  69. df['还款状态'] = df['还款状态'].map({"/":0,"*":0,"C":0,"N":0,"#":0,"M":0,"G":7,"B":7,"Z":7,"D":1,"":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7})
  70. return df;
  71. # 替换日期格式转换briefInfo
  72. def replaceDateCol(dfx):
  73. if not dfx.empty:
  74. dfx[1] = dfx[1].str.replace(".", '-') # 2018.09.02->>2018-09-02
  75. return dfx;
  76. # 替换日期格式转换briefInfo
  77. def replaceDateColIdx(dfx,idx):
  78. if not dfx.empty:
  79. dfx[idx] = dfx[idx].str.replace(".", '-') # 2018.09.02->>2018-09-02
  80. return dfx;
  81. # 科学计数法转换
  82. def replaceAmt(value):
  83. if str(value)=="" or str(value)=="--" or str(value)=="nan":
  84. # return value.replace(',', '')
  85. return 0.0
  86. else:
  87. return float(value.replace(',', ''))
  88. #转整型
  89. def toInt(value):
  90. if str(value)=="" or str(value)=="--" or str(value)=="nan":
  91. return 0
  92. else:
  93. return int(value)
  94. #替换日期
  95. def formatDate(str):
  96. return st