将日期附加到 table 名称并迭代创建数据框
Append date to table name and create data frame iteratively
我的目标是从 redshift 视图查询数据,其中视图名称由 monthdt 附加,如 view_name_202009 并在 13 个月前逐月查询此视图,创建数据框并执行转换。在创建第一个数据帧(df1) 后,应将前几个月的转换数据附加到 df1.
循序渐进:
- 查询当前月份数据框
- 创建数据框 (df1)
- 执行转换
- 日期递减到上个月(重复直到 13 个月前)
下面是我编写的用于创建 13 个月的 sql 语句的函数,但我未能将它们传递给 read_sql squetnailly 并创建数据框。
def last_n_months(num_of_months, start_date=datetime.now(), include_curr=True):
f = "%Y%m"
curr = datetime.now()
if include_curr:
yield curr.strftime(f)
for num in range(num_of_months):
curr = curr.replace(day=1) - timedelta(days=1)
sql = """select col1, col2,
from schema.view_name"""+str(curr.strftime(f))+""" where col1 in ('01','02','1ST','2ND')"""
# df = pd.read_sql(sql, con)
yield sql
m_lst = [sql for sql in last_n_months(13, include_curr=False)]
df = pd.DataFrame()
for i in m_lst:
df = df.append(pd.read_sql(m_lst, con))
我打印了 m_lst 如下所示:
["select col1, col2 from schema.view_name_202010 where col1 in ('01','02','1ST','2ND')",["select col1, col2 from schema.view_name_202009 where col1 in ('01','02','1ST','2ND')",["select col1, col2 from schema.view_name_202008 where col1 in ('01','02','1ST','2ND')"]
我收到如下错误:
pandas.io.sql.DatabaseError: Execution failed on sql : argument 1 must be a string or unicode object: got list instead
谁能帮我解决我做错的地方,是否有更好的方法?
更改下面的代码
df = pd.DataFrame()
for i in m_lst:
df = df.append(pd.read_sql(i, con))
我的目标是从 redshift 视图查询数据,其中视图名称由 monthdt 附加,如 view_name_202009 并在 13 个月前逐月查询此视图,创建数据框并执行转换。在创建第一个数据帧(df1) 后,应将前几个月的转换数据附加到 df1.
循序渐进:
- 查询当前月份数据框
- 创建数据框 (df1)
- 执行转换
- 日期递减到上个月(重复直到 13 个月前)
下面是我编写的用于创建 13 个月的 sql 语句的函数,但我未能将它们传递给 read_sql squetnailly 并创建数据框。
def last_n_months(num_of_months, start_date=datetime.now(), include_curr=True):
f = "%Y%m"
curr = datetime.now()
if include_curr:
yield curr.strftime(f)
for num in range(num_of_months):
curr = curr.replace(day=1) - timedelta(days=1)
sql = """select col1, col2,
from schema.view_name"""+str(curr.strftime(f))+""" where col1 in ('01','02','1ST','2ND')"""
# df = pd.read_sql(sql, con)
yield sql
m_lst = [sql for sql in last_n_months(13, include_curr=False)]
df = pd.DataFrame()
for i in m_lst:
df = df.append(pd.read_sql(m_lst, con))
我打印了 m_lst 如下所示:
["select col1, col2 from schema.view_name_202010 where col1 in ('01','02','1ST','2ND')",["select col1, col2 from schema.view_name_202009 where col1 in ('01','02','1ST','2ND')",["select col1, col2 from schema.view_name_202008 where col1 in ('01','02','1ST','2ND')"]
我收到如下错误:
pandas.io.sql.DatabaseError: Execution failed on sql : argument 1 must be a string or unicode object: got list instead
谁能帮我解决我做错的地方,是否有更好的方法?
更改下面的代码
df = pd.DataFrame()
for i in m_lst:
df = df.append(pd.read_sql(i, con))