使用 pandas 处理来自 sql 的数据帧时内核死机

Kernel dies when using pandas to work on dataframe from sql

当我尝试使用使用 sql 查询获得的数据更改数据帧时,我的内核死机了。

我减少了数据量,增加了 RAM 并注释掉了导致内核崩溃的代码。

    python
    import pandas as pd
    import sqlalchemy
    sql_str = """some sql query limit 100""" 
    df = pd.read_sql(sql_str, engine)   
    list=[
    '1st name','2nd name'] 
    for i in range(0,len(list)):
        df.columns.values[i]=list[i]
    #naming the columns of my df
    df=df[df["tradedate"]<'01-01-2017']
    #this is a line of code that sometimes crashes the kernel
    df['strike']=pd.to_numeric(df["strike"],errors='coerce')
    #another one

我希望有一个可以使用的干净数据帧,但内核死机了,我收到 windows 错误。

如果有人能在这里帮助我,我将不胜感激!

尝试使用 chunksize 参数:

df = pd.read_sql(sql_str, engine, chunksize=20) #try different values

我找到了问题的解决方案,以防有人随机发现这个问题:

问题出在我命名列的方式上。

for i in range(0,len(list)):
      df.columns.values[i]=list[i]

不起作用。相反,我使用:

df.columns=list

现在内核不再死机了。