Pandas read_sql没有读取指定的日期

时间:2018-02-20 15:17:50

标签: python pandas

我尝试使用where语句中的指定日期在Python中过滤我的SQL代码。由于某种原因,它不会根据日期进行过滤,我不确定原因。想要在report_dt上进行过滤> ' 2018年1月1日'但结果输出是给出所有数据

import pandas as pd
startdate='2018-01-01'
test=pd.read_sql("""SELECT top 10 report_dt FROM db.table where report_dt>{} """.format(startdate) , connection)
print(test)


REPORT_DT
0  2014-02-15
1  2014-02-15
2  2014-02-15

3 个答案:

答案 0 :(得分:0)

$(window).on('hashchange', function(e) { // this handles links in same page scrollToAnchor(800); }).on('load', function(e){ scrollToAnchor(1300); // this handles links to different pages }); // here we handle animating to already active hash // as the hashchange will not be triggered $('a[href$="-st"]').on('click', function(e){ var hash = this.hash; // not supported everywhere if (hash === window.location.hash){ scrollToAnchor(800); } }); function scrollToAnchor(duration) { var hash = window.location.hash, cleanhash = hash.replace(/-st$/, ''); if (hash && (hash !== cleanhash)) { // it was indeed ending in -st $('html, body').animate({ scrollTop: $(cleanhash).offset().top }, duration); } } 是按照以下链接完成的。

sample Number

来源:http://community.teradata.com/t5/Database/Limit-rows/td-p/5238

答案 1 :(得分:0)

最好将参数而不是格式化的字符串传递给查询。试一试:

startdate = '2018-01-01'
test = pd.read_sql("SELECT top 10 report_dt FROM db.table where report_dt > ?", connection, params=(startdate,))

可以找到一个比我能给出的更好的解释here(与Teradata没有具体关系,但这些概念适用)。

答案 2 :(得分:0)

检查此链接,以在基础数据库上为其配置语法语法。 https://www.python.org/dev/peps/pep-0249/#id41-paramstyle部分。 Paramstyle取决于基础数据库配置。

例如,用于Teradata数据库-

**
import teradatasql
teradatasql.paramstyle
result- 'qmark'

or

import importlib

def get_paramstyle(conn):
   name = conn.__class__.__module__.split('.')[0]
   mod = importlib.import_module(name)
   return mod.paramstyle

  get_paramstyle(conn=DBconnection) # DBconnection is connection object to the 
  database you want to connect.

#Pass date parameters to pd.read_sql.
date_param = ['2020-04-17']
SQL_string =  " SELECT * FROM tablename WHERE tablename.load_date >= ? "
df = pd.read_sql(sql=SQL_string, con= DBconnection, 
index_col='load_date',parse_dates='load_date',params=(date_param))
相关问题