验证熊猫中的日期时遇到问题

时间:2018-11-26 16:44:28

标签: python-3.x pandas

guid = str(uuid.uuid4())
username = config.username
dbpassword = config.dbpassword
dbhost = config.dburl
engine =  create_engine('mysql://%s:%s@%s/db?charset=utf8' %(username, dbpassword, dbhost), encoding="utf-8")
path = 'd:/anaconda3/lib/bin/'
jsonFiles = os.listdir(path)
maxSnapshotQuery = '''SELECT MAX(Snapshot) AS Snapshot FROM %s''' 

for file in jsonFiles:
    tableName = file.split(".")[0]
    snapshotCheck = pd.read_sql_query(maxSnapshotQuery %tableName, engine)
    print(snapshotCheck['Snapshot'].dt.date)
    if snapshotCheck['Snapshot'].dt.date == datetime.datetime.now().date():
        with open(path+file) as f :
         try:
             rawJson = json.load(f)
             rawJson['authorizations'][0]['id'] = guid
             print(f.name + ' Load Completed')
         except ValueError as error:
             print(error + ' ' + f.name + ' Check the JSON within')
             continue


        createSession()
        createJob(session)
        loadData(data, engine)

我试图确保当天的快照尚未拍摄并放入数据库中。如果它跳过了该加载并转到下一个(在代码的另一部分中发生),但是我似乎无法从if语句中获取数据帧中的日期并将其与当前日期匹配。 / p>

1 个答案:

答案 0 :(得分:0)

知道了

for file in jsonFiles:
    tableName = file.split(".")[0]
    snapshotDate = pd.read_sql_query(maxSnapshotQuery %tableName, engine, parse_dates=['Snapshot'])

if snapshotDate.Snapshot[0].date() != datetime.datetime.now().date() :
    with open(path+file) as f :
     try:
         rawJson = json.load(f)
         rawJson['authorizations'][0]['id'] = guid
         print(f.name + ' Load Completed')
     except:
         print(f.name + ' Check the JSON within')
         continue


    createSession()
    createJob(session)
    loadData(data, engine)
else:
    print('Data already loaded for today for ' + file)

像K,V对一样解析对象,然后对其应用日期。