可以使用pandas / sqlalchemy将数组插入sql数据库吗? (postgres的)

时间:2017-05-05 23:22:32

标签: postgresql pandas sqlalchemy

以下内容:

engine = sqlalchemy.create_engine(url)

df = pd.DataFrame({
    "eid": [1,2],
    "f_i": [123, 1231],
    "f_i_arr": [[123], [0]],
    "f_53": ["2013/12/1","2013/12/1",],
    "f_53a": [["2013/12/1"], ["2013/12/1"],],
})

with engine.connect() as con:
    con.execute("""
        DROP TABLE IF EXISTS public.test;
        CREATE TABLE public.test
        (
          eid integer NOT NULL,
          f_i INTEGER NULL,
          f_i_arr INTEGER NULL,
          f_53 DATE NULL,
          f_53a DATE[] NULL,
           PRIMARY KEY(eid)
        );;
        """)
    df.to_sql("test", con, if_exists='append')

如果我尝试仅插入列" f_53" (date)它成功了。

如果我尝试添加列" f_53a" (a date[])它失败了:

                                            ^
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) column "f_53a" is of type date[] but expression is of type text[]
LINE 1: ..._53, f_53a, f_i, f_i_arr) VALUES (1, '2013/12/1', ARRAY['201...
                                                             ^
HINT:  You will need to rewrite or cast the expression.
 [SQL: 'INSERT INTO test (eid, f_53, f_53a, f_i, f_i_arr) VALUES (%(eid)s, %(f_53)s, %(f_53a)s, %(f_i)s, %(f_i_arr)s)'] [parameters: ({'f_53': '2013/12/1', 'f_53a': ['2013/12/1', '2013/12/1'], 'f_i_arr': [123], 'eid': 1, 'f_i': 123}, {'f_53': '2013/12/1', 'f_53a': ['2013/12/1', '2013/12/1'], 'f_i_arr': [0], 'eid': 2, 'f_i': 1231})]

2 个答案:

答案 0 :(得分:2)

是 - 可以将数据框中的[][][]类型插入到数据框的postgres中。

与可以由sql正确解析的平面DATE类型不同,DATE[]DATE[][]需要首先转换为datetime对象。像这样。

with engine.connect() as con:
    con.execute("""
        DROP TABLE IF EXISTS public.test;
        CREATE TABLE public.test
        (
          eid integer NOT NULL,
          f_i INTEGER NULL,
          f_ia INTEGER[] NULL,
          f_iaa INTEGER[][] NULL,
          f_d DATE NULL,
          f_da DATE[] NULL,
          f_daa DATE[][] NULL,
          PRIMARY KEY(eid)
        );
        """)


    d = pd.to_datetime("2013/12/1")
    i = 99
    df = pd.DataFrame({
        "eid": [1,2],

        "f_i": [i,i],
        "f_ia": [None, [i,i]],
        "f_iaa": [[[i,i],[i,i]], None],

        "f_d": [d,d],
        "f_da": [[d,d],None],
        "f_daa": [[[d,d],[d,d]],None],
    })
    df.to_sql("test", con, if_exists='append', index=None)

答案 1 :(得分:1)

我已经明确提到了dtypes,它对我来说对postgres有用。

//sample code

import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.dialects import postgresql
 df.to_sql('mytable',pgConn, if_exists='append', index=False,  dtype={'datetime': sqlalchemy.TIMESTAMP(), 'cur_c':postgresql.ARRAY(sqlalchemy.types.REAL),
        'volt_c':postgresql.ARRAY(sqlalchemy.types.REAL)
        })