utils.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. import copy
  9. import pandas as pd
  10. import datetime
  11. #某个日期离当前时间
  12. def difDate(dateStr):
  13. return int(str(np.datetime64(np.datetime64(time.strftime("%Y-%m-%d")), "D") -np.datetime64(dateStr, "D")).split(" ")[0]);
  14. #某个日期离报告期天数
  15. def difDateReportTime(reportTime,dateStr):
  16. return int(str(np.datetime64(reportTime, "D") -np.datetime64(dateStr, "D")).split(" ")[0])
  17. # 最近几个月的日期
  18. def getLastMonthDate(dateStr,month):
  19. lastMonthDateStr = dateStr.replace(".", "-")
  20. # 最近一个月
  21. lastMonthDate = np.datetime64(lastMonthDateStr, "D") - np.timedelta64(30 * month, 'D')
  22. return str(lastMonthDate);
  23. #离当前时间月份
  24. def difMonthNow(dateStr):
  25. return math.ceil(int(str(np.datetime64(time.strftime("%Y-%m-%d")) - np.datetime64(dateStr.replace('.', '-'), "D")).split(" ")[0]) / 30);
  26. #离报告时间月份
  27. def difMonthReportTime(dateStr,reportTime):
  28. try:
  29. reportTimeTmp = np.datetime64(reportTime, "D");
  30. dateStrTmp = np.datetime64(dateStr, "D");
  31. except:
  32. print(dateStr+' 解析截止日期报错')
  33. month = int(str(reportTimeTmp - dateStrTmp ).split(" ")[0]) / 30;
  34. return math.ceil(month)
  35. #离报告时间月份向下
  36. def difMonthReportTimeFloorNew(dateStr,reportTime):
  37. try:
  38. reportTimeTmp = np.datetime64(reportTime, "D");
  39. dateStrTmp = np.datetime64(dateStr, "D");
  40. except:
  41. print(dateStr+' 解析截止日期报错')
  42. month = int(str(reportTimeTmp - dateStrTmp ).split(" ")[0]) / 30;
  43. return math.floor(month)
  44. #向下取整
  45. def difMonthReportTimeFloor(dateStr,reportTime):
  46. reportTimeTmp = pd.to_datetime(reportTime)
  47. dateStrTmp = pd.to_datetime(dateStr)
  48. month = (reportTimeTmp.year-dateStrTmp.year)*12+(reportTimeTmp.month-dateStrTmp.month)
  49. return month
  50. #向下取整-考虑天使
  51. def difMonthReportTimeFloorHsz(dateStr,reportTime):
  52. reportTimeTmp = pd.to_datetime(reportTime)
  53. dateStrTmp = pd.to_datetime(dateStr)
  54. dayDiff = 0 if reportTimeTmp.day - dateStrTmp.day >=0 else -1
  55. month = (reportTimeTmp.year - dateStrTmp.year) *12 + (reportTimeTmp.month-dateStrTmp.month)+dayDiff
  56. return month
  57. #转json
  58. def toJson(df):
  59. jsonStr = df.to_json(orient="index",force_ascii=False);
  60. # print(json)
  61. log4r.info(jsonStr)
  62. reult = formatJson(json.loads(jsonStr))[1:-1];
  63. return reult
  64. def formatJson(jsonObj):
  65. json_dicts = json.dumps(jsonObj, indent=4, ensure_ascii=False)
  66. return json_dicts;
  67. # 判断是否有表头
  68. def checkHeader(df,allHeaders):
  69. result = False
  70. headerList = df.loc[0, :].tolist() # 第0行为表头
  71. headerList1 = []
  72. if df.index.size>1:
  73. headerList1 = df.loc[1, :].tolist() # 第1行为表头
  74. for header in allHeaders: # 第0行或者1行为表头的话,代表不是数据,否则是数据
  75. if list(filter(None, header)) == list(filter(None, headerList)) or list(filter(None, header)) == list(
  76. filter(None, headerList1)):
  77. result = True;
  78. break;
  79. return result;
  80. #替换还款状态规则
  81. #1、2、3、4、5、6、7、D、Z、G、B Z/B=7
  82. def replacePayRcdStatus(df):
  83. if df.empty:
  84. return df;
  85. 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})
  86. return df;
  87. def doFilterCalc(self,dfx):
  88. dfx = dfx.replace('--', 0)
  89. return dfx;
  90. #开户数使用,出现逾期的情况
  91. def replacePayRcdStatusOverdue(df):
  92. if df.empty:
  93. return df;
  94. 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})
  95. return df;
  96. #最近一次逾期距今月份数
  97. def replacePayRcdStatusOverdueNew(df):
  98. if df.empty:
  99. return df;
  100. df['还款状态'] = df['还款状态'].map({"/":0,"*":0,"C":0,"N":0,"#":0,"M":0,"G":0,"B":0,"Z":0,"D":0,"":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7})
  101. return df;
  102. # 替换日期格式转换briefInfo
  103. def replaceDateCol(dfx):
  104. if not dfx.empty:
  105. dfx[1] = dfx[1].str.replace(".", '-') # 2018.09.02->>2018-09-02
  106. return dfx;
  107. # 替换日期格式转换briefInfo
  108. def replaceDateColIdx(dfx,idx):
  109. if not dfx.empty:
  110. dfx[idx] = dfx[idx].str.replace(".", '-') # 2018.09.02->>2018-09-02
  111. return dfx;
  112. # 科学计数法转换
  113. def replaceAmt(value):
  114. if str(value)=="" or str(value)=="--" or str(value)=="nan":
  115. # return value.replace(',', '')
  116. return 0.0
  117. else:
  118. return float(value.replace(',', ''))
  119. #转整型
  120. def toInt(value):
  121. if str(value)=="" or str(value)=="--" or str(value)=="nan":
  122. return 0
  123. else:
  124. return int(value)
  125. #替换日期
  126. def formatDate(str):
  127. return str.replace('.','-');
  128. def null2Zero(value):
  129. if value==None:
  130. return 0
  131. else:
  132. return toInt(value);
  133. def get_real_arr(arr):
  134. """
  135. 返回删除所有空值后的arr
  136. """
  137. arr_copy = copy.deepcopy(arr)
  138. arr_copy = list(filter(None, arr_copy))
  139. while '' in arr_copy:
  140. arr_copy.remove('')
  141. return arr_copy
  142. #获取报告期上月的2号
  143. def get_last_month_first_day_v2(reportTime):
  144. d = pd.to_datetime(reportTime)
  145. return str(datetime.date(d.year - (d.month==1), d.month - 1 or 12, 2))