import consts; import numpy as np;# 近3月开户最高贷款本金 import time; import math; import logResult; log4r = logResult.logger import json; import copy import pandas as pd import datetime #某个日期离当前时间 def difDate(dateStr): return int(str(np.datetime64(np.datetime64(time.strftime("%Y-%m-%d")), "D") -np.datetime64(dateStr, "D")).split(" ")[0]); #某个日期离报告期天数 def difDateReportTime(reportTime,dateStr): return int(str(np.datetime64(reportTime, "D") -np.datetime64(dateStr, "D")).split(" ")[0]) # 最近几个月的日期 def getLastMonthDate(dateStr,month): lastMonthDateStr = dateStr.replace(".", "-") # 最近一个月 lastMonthDate = np.datetime64(lastMonthDateStr, "D") - np.timedelta64(30 * month, 'D') return str(lastMonthDate); #离当前时间月份 def difMonthNow(dateStr): return math.ceil(int(str(np.datetime64(time.strftime("%Y-%m-%d")) - np.datetime64(dateStr.replace('.', '-'), "D")).split(" ")[0]) / 30); #离报告时间月份 def difMonthReportTime(dateStr,reportTime): try: reportTimeTmp = np.datetime64(reportTime, "D"); dateStrTmp = np.datetime64(dateStr, "D"); except: print(dateStr+' 解析截止日期报错') month = int(str(reportTimeTmp - dateStrTmp ).split(" ")[0]) / 30; return math.ceil(month) #离报告时间月份向下 def difMonthReportTimeFloorNew(dateStr,reportTime): try: reportTimeTmp = np.datetime64(reportTime, "D"); dateStrTmp = np.datetime64(dateStr, "D"); except: print(dateStr+' 解析截止日期报错') month = int(str(reportTimeTmp - dateStrTmp ).split(" ")[0]) / 30; return math.floor(month) #向下取整 def difMonthReportTimeFloor(dateStr,reportTime): reportTimeTmp = pd.to_datetime(reportTime) dateStrTmp = pd.to_datetime(dateStr) month = (reportTimeTmp.year-dateStrTmp.year)*12+(reportTimeTmp.month-dateStrTmp.month) return month #向下取整-考虑天使 def difMonthReportTimeFloorHsz(dateStr,reportTime): reportTimeTmp = pd.to_datetime(reportTime) dateStrTmp = pd.to_datetime(dateStr) dayDiff = 0 if reportTimeTmp.day - dateStrTmp.day >=0 else -1 month = (reportTimeTmp.year - dateStrTmp.year) *12 + (reportTimeTmp.month-dateStrTmp.month)+dayDiff return month #转json def toJson(df): jsonStr = df.to_json(orient="index",force_ascii=False); # print(json) log4r.info(jsonStr) reult = formatJson(json.loads(jsonStr))[1:-1]; return reult def formatJson(jsonObj): json_dicts = json.dumps(jsonObj, indent=4, ensure_ascii=False) return json_dicts; # 判断是否有表头 def checkHeader(df,allHeaders): result = False headerList = df.loc[0, :].tolist() # 第0行为表头 headerList1 = [] if df.index.size>1: headerList1 = df.loc[1, :].tolist() # 第1行为表头 for header in allHeaders: # 第0行或者1行为表头的话,代表不是数据,否则是数据 if list(filter(None, header)) == list(filter(None, headerList)) or list(filter(None, header)) == list( filter(None, headerList1)): result = True; break; return result; #替换还款状态规则 #1、2、3、4、5、6、7、D、Z、G、B Z/B=7 def replacePayRcdStatus(df): if df.empty: return df; 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}) return df; def doFilterCalc(self,dfx): dfx = dfx.replace('--', 0) return dfx; #开户数使用,出现逾期的情况 def replacePayRcdStatusOverdue(df): if df.empty: return df; 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}) return df; #最近一次逾期距今月份数 def replacePayRcdStatusOverdueNew(df): if df.empty: return df; 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}) return df; # 替换日期格式转换briefInfo def replaceDateCol(dfx): if not dfx.empty: dfx[1] = dfx[1].str.replace(".", '-') # 2018.09.02->>2018-09-02 return dfx; # 替换日期格式转换briefInfo def replaceDateColIdx(dfx,idx): if not dfx.empty: dfx[idx] = dfx[idx].str.replace(".", '-') # 2018.09.02->>2018-09-02 return dfx; # 科学计数法转换 def replaceAmt(value): if str(value)=="" or str(value)=="--" or str(value)=="nan": # return value.replace(',', '') return 0.0 else: return float(value.replace(',', '')) #转整型 def toInt(value): if str(value)=="" or str(value)=="--" or str(value)=="nan": return 0 else: return int(value) #替换日期 def formatDate(str): return str.replace('.','-'); def null2Zero(value): if value==None: return 0 else: return toInt(value); def get_real_arr(arr): """ 返回删除所有空值后的arr """ arr_copy = copy.deepcopy(arr) arr_copy = list(filter(None, arr_copy)) while '' in arr_copy: arr_copy.remove('') return arr_copy #获取报告期上月的2号 def get_last_month_first_day_v2(reportTime): d = pd.to_datetime(reportTime) return str(datetime.date(d.year - (d.month==1), d.month - 1 or 12, 2))