嗨,我正在尝试以某种格式获取sql查询输出,以保持文件状态。
我正在检查sql查询是否具有所有日期。如果未添加带有日期和零值的数据框,然后将其压缩为一。
df.iloc [0,1]打印值2018-07-02,但在签入if语句时,返回错误'IndexError:单个位置索引器越界':
请帮助。
import ibm_db
import ibm_db_dbi
import datetime
import pandas as pd
con = ibm_db.pconnect("DATABASE=####;HOSTNAME=####;PORT=####;PROTOCOL=TCPIP;UID=#####;PWD=#######;","","")
conn = ibm_db_dbi.Connection(con)
today = datetime.date.today()
today1 = today - datetime.timedelta(days = 1)
today2 = today1 - datetime.timedelta(days = 1)
today3 = today2 - datetime.timedelta(days = 1)
today4 = today3 - datetime.timedelta(days = 1)
today5 = today4 - datetime.timedelta(days = 1)
DT1 = today5.strftime("%d-%b-%Y")
DT2 = today4.strftime("%d-%b-%Y")
DT3 = today3.strftime("%d-%b-%Y")
DT4 = today2.strftime("%d-%b-%Y")
DT5 = today1.strftime("%d-%b-%Y")
sql1 = "select substr(load_date,0,10) load_date, count (distinct file_name) file_count, count(1) record_count from ###### where load_date BETWEEN '" + today5.strftime("%Y-%m-%d") +" 00:00:00' and '" + today.strftime("%Y-%m-%d") + " 23:59:59' group by substr(load_date,0,10) ORDER BY substr(load_date,0,10) WITH UR"
df1 = pd.read_sql(sql1, conn)
#print df1
df1t = df1.T
df = df1t
#print df1t[0]
#index_list = df1t[(df1t.iloc[0,0] == today2.strftime("%Y-%m-%d") )].index.tolist()
#print today2.strftime("%Y-%m-%d")
#print list(df1t)
df1t.columns = df1t.iloc[0]
df1t = df1t.drop(df1t.index[0])
df.rename(columns=df.iloc[0]).drop(df.index[0])
#print df
if df1t.iloc[0,0] != today5.strftime("%Y-%m-%d"):
print("Mismatch Found For " + today5.strftime("%Y-%m-%d"))
dfDT5 = pd.DataFrame([today5.strftime("%Y-%m-%d"),0,0], index=['LOAD_DATE', 'FILE_COUNT', 'RECORD_COUNT'])
dfDT5.columns = [today5.strftime("%Y-%m-%d")]
dfc = pd.concat([dfDT5, df], axis=1, sort=False)
print "-------------------"
print dfc
#print dfDT5
print "-------------------"
#df1t.add(dfDT5, fill_value=0)
print dfc.iloc[0,1]
print "-------------------"
if df1t.iloc[0,1] != today4.strftime("%Y-%m-%d"):
print("Mismatch Found For " + today4.strftime("%Y-%m-%d"))
我得到的错误如下:
Mismatch Found For 2018-06-27
-------------------
2018-06-27 2018-07-02
LOAD_DATE 2018-06-27 2018-07-02
FILE_COUNT 0 2
RECORD_COUNT 0 8100999
-------------------
2018-07-02
-------------------
Traceback (most recent call last):
File ".\AIR_CS5.py", line 113, in <module>
if df1t.iloc[0,1] != today4.strftime("%Y-%m-%d"):
File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 1472, in __getitem__
return self._getitem_tuple(key)
File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 2013, in _getitem_tuple
self._has_valid_tuple(tup)
File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 222, in _has_valid_tuple
self._validate_key(k, i)
File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 1957, in _validate_key
self._validate_integer(key, axis)
File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 2009, in _validate_integer
raise IndexError("single positional indexer is out-of-bounds")
IndexError: single positional indexer is out-of-bounds
答案 0 :(得分:0)
您的数据框df1t
只有一列。因此,切片df1t.iloc[0,1]
将失败,并显示IndexError
。
确保数据框具有逻辑所需的数据,或更改逻辑以容纳数据。
输出print dfc.iloc[0,1]
的行2018-07-02
不代表df1t
。事先打印df1t
可能会有所帮助,以了解您的工作情况。