Dataframe.to_sql()无法插入列

时间:2019-09-12 15:54:05

标签: python pandas postgresql python-2.7 sqlalchemy

我创建了一个例程,该例程读取新数据,然后将其重写到另一个文件中。最后,所有数据最终都在一个数据帧中。

编辑:数据框实际上具有模型中显示的GMD和CMD列。

                     Current SS  Temps Stabilized  Delta T     T1 Set    T1 Read     T2 Set    T2 Read     T3 Set    T3 Read  Nafion Set   T Nafion  Inlet Set    T Inlet    OPC Set      T OPC   T Sample  Sample Flow  Sheath Flow  Sample Pressure  Laser Current  overflow  Baseline Mon  1st Stage Mon  Bin #  Bin 1  Bin 2  Bin 3   Bin 4  Bin 5  Bin 6  Bin 7  Bin 8  Bin 9  Bin 10  Bin 11  Bin 12  Bin 13  Bin 14  Bin 15  Bin 16  Bin 17  Bin 18  Bin 19  Bin 20  CCN Number Conc  Valve Set  Alarm Code  Alarm Sum
Datetime                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
2019-05-02 00:00:00         0.1               1.0    3.264  27.709753  27.683804  29.341753  29.099846  30.745274  30.540390   26.709753  26.765661  28.709753  28.766088  32.745274  32.870701  25.833349    45.147003   455.568115      1016.933960      78.315002       0.0      2.787700        0.56730    0.0    4.0    5.0   13.0   74.00    0.0   2.00   0.00   0.00    1.0    1.00     0.0    0.00     0.0    1.00     0.0     0.0     0.0     0.0     0.0     0.0       134.035156   2.840745         0.0        NaN
2019-05-02 00:00:01         0.1               1.0    3.264  27.709753  27.712530  29.341753  29.124981  30.745274  30.583268   26.709753  26.769858  28.709753  28.840857  32.745274  32.824543  25.821905    45.437126   458.366699      1016.158752      77.395000       0.0      2.789225        0.56730    0.0    1.0    8.0   28.0   92.25    3.5   1.75   1.25   0.75    1.0    0.25     2.0    0.25     0.0    0.25     0.0     0.0     0.0     0.0     0.0     0.0       111.956049   2.741862         0.0        NaN
2019-05-02 00:00:02         0.1               1.0    3.264  27.709753  27.726048  29.341753  29.148849  30.745274  30.581791   26.709753  26.774817  28.709753  28.824072  32.745274  32.797459  25.853186    45.054581   458.191650      1016.778931      78.257500       0.0      2.790140        0.56974    0.0    0.0    8.0   35.0   79.00    2.0   1.00   0.00   1.00    0.0    0.00     0.0    0.00     0.0    0.00     0.0     0.0     0.0     0.0     0.0     0.0       184.126541   2.800261         0.0        NaN
2019-05-02 00:00:03         0.1               1.0    3.264  27.709753  27.717598  29.341753  29.122658  30.745274  30.542925   26.709753  26.780920  28.709753  28.850012  32.745274  32.808140  25.815039    45.229172   457.666992      1016.951233      77.222504       0.0      2.790140        0.57096    0.0    0.0    3.0   30.0   78.00    2.0   0.00   1.00   1.00    0.0    1.00     0.0    1.00     0.0    0.00     0.0     0.0     0.0     0.0     0.0     0.0       153.451904   2.759360         0.0        NaN
2019-05-02 00:00:04         0.1               1.0    3.264  27.709753  27.706615  29.341753  29.122658  30.745274  30.542080   26.709753  26.788549  28.709753  28.790503  32.745274  32.817295  25.779943    44.972427   454.168701      1016.865051      77.107498       0.0      2.791360        0.57096    0.0    3.0    5.0   29.0  103.00    2.0   0.00   2.00   1.00    1.0    0.00     0.0    0.00     0.0    0.00     0.0     0.0     0.0     0.0     0.0     0.0       194.505920   2.812238         0.0        NaN
2019-05-02 00:00:05         0.1               1.0    3.264  27.709753  27.694786  29.341753  29.101536  30.745274  30.546305   26.709753  26.764135  28.709753  28.689795  32.745274  32.823399  25.845556    44.941620   459.765869      1016.916748      78.142502       0.0      2.791360        0.56974    0.0    1.0    8.0   30.0   83.00    2.0   3.00   0.00   0.00    0.0    1.00     1.0    0.00     0.0    0.00     0.0     0.0     0.0     0.0     0.0     0.0       173.712936   2.855573         0.0        NaN
2019-05-02 00:00:06         0.1               1.0    3.264  27.709753  27.694786  29.341753  29.086327  30.745274  30.539545   26.709753  26.811438  28.709753  28.758459  32.745274  32.763889  25.830297    45.475632   463.264160      1016.072632      78.372505       0.0      2.788920        0.57096    0.0    2.0    8.0   23.0   73.00    3.0   1.00   3.00   0.00    1.0    0.00     1.0    0.00     0.0    0.00     0.0     0.0     0.0     0.0     0.0     0.0       150.011292   2.797190         0.0        NaN
2019-05-02 00:00:07         0.1               1.0    3.264  27.709753  27.685493  29.341753  29.099846  30.745274  30.558132   26.709753  26.776342  28.709753  28.827124  32.745274  32.768467  25.828772    44.797852   454.168701      1016.865051      77.107498       0.0      2.790140        0.56608    0.0    1.0    6.0   43.0  101.00    4.0   4.00   1.00   1.00    2.0    2.00     2.0    0.00     0.0    0.00     0.0     0.0     0.0     0.0     0.0     0.0       223.349808   2.713870         0.0        NaN
2019-05-02 00:00:08         0.1               1.0    3.264  27.709753  27.704926  29.341753  29.109985  30.745274  30.568272   26.709753  26.762609  28.709753  28.770666  32.745274  32.769993  25.836401    45.136734   459.416016      1016.623901      77.222504       0.0      2.788920        0.56974    0.0    1.0    9.0   27.0   81.00    6.0   0.00   1.00   1.00    0.0    0.00     0.0    0.00     0.0    0.00     0.0     0.0     0.0     0.0     0.0     0.0       167.250229   2.809274         0.0        NaN
2019-05-02 00:00:09         0.1               1.0    3.264  27.709753  27.713375  29.341753  29.104071  30.745274  30.553909   26.709753  26.777868  28.709753  28.756933  32.745274  32.751682  25.821142    45.064850   455.043213      1016.778931      77.222504       0.0      2.791360        0.56608    0.0    1.0    5.0   25.0   66.00    3.0   0.00   0.00   0.00    0.0    0.00     0.0    0.00     1.0    0.00     0.0     0.0     0.0     0.0     0.0     0.0       135.635880   2.858126         0.0        NaN

我正在尝试使用以下模型将此数据追加到Postgresql数据库中的表中:

class ccn(Base):
    __tablename__ = 'ccn'
    #id = Column(Integer, Sequence('ccn_id_seq'),  primary_key=True, name='id')
    Datetime = Column(TIMESTAMP, primary_key=True)
    Current_ss = Column(Float, name='Current SS')
    Temps_Stabilized = Column(Float, name='Column Temperatue Stabilized')
    Delta_T = Column(Float, name='Delta T')
    T1_Set = Column(Float, name='T1 Set')
    T1_Read = Column(Float, name='T1 Read')
    T2_Set = Column(Float, name='T2 Set')
    T2_Read = Column(Float, name='T2 Read')
    T3_Set = Column(Float, name='T3 Set')
    T3_Read = Column(Float, name='T3 Read')
    Naftion_Set = Column(Float, name='Naftion Set')
    T_Naftion = Column(Float, name='T Naftion')
    Inlet_Set = Column(Float, name='Inlet Set')
    T_Inlet = Column(Float, name='T Inlet')
    OCP_Set = Column(Float, name='OCP Set')
    T_OCP = Column(Float, name='T OCP')
    T_Sample = Column(Float, name='T Sample')
    Sample_Flow = Column(Float, name='Sample Flow')
    Sheath_Flow = Column(Float, name='Sheath Flow')
    Sample_Pressure = Column(Float, name='Sample Pressure')
    Laser_Current = Column(Float, name='Laser Current')
    Overflow = Column(Float, name='Overflow')
    Baseline_Mon = Column(Float, name='Baseline Monitor')
    first_Stage_Mon = Column(Float, name='First Stage Monitor')
    Bin = Column(Float, name='Bin')
    Bin_1 = Column(Float, name='Bin 1')
    Bin_2 = Column(Float, name='Bin 2')
    Bin_3 = Column(Float, name='Bin 3')
    Bin_4 = Column(Float, name='Bin 4')
    Bin_5 = Column(Float, name='Bin 5')
    Bin_6 = Column(Float, name='Bin 6')
    Bin_7 = Column(Float, name='Bin 7')
    Bin_8 = Column(Float, name='Bin 8')
    Bin_9 = Column(Float, name='Bin 9')
    Bin_10 = Column(Float, name='Bin 10')
    Bin_11 = Column(Float, name='Bin 11')
    Bin_12 = Column(Float, name='Bin 12')
    Bin_13 = Column(Float, name='Bin 13')
    Bin_14 = Column(Float, name='Bin 14')
    Bin_15 = Column(Float, name='Bin 15')
    Bin_16 = Column(Float, name='Bin 16')
    Bin_17 = Column(Float, name='Bin 17')
    Bin_18 = Column(Float, name='Bin 18')
    Bin_19 = Column(Float, name='Bin 19')
    Bin_20 = Column(Float, name='Bin 20')
    CCN_Number_Conc = Column(Float, name='CCN Number Concentratrion')
    Valve_Set = Column(Float, name='Valve Set')
    Alarm_Code = Column(Float, name='Alarm Code')
    CMD = Column(Float, name='CMD')
    GMD = Column(Float, name='GMD')

问题是,这引发了一个错误,表明我无法插入“ Datetime”列,因为它已经存在,并且它也不允许我查询数据。

Traceback (most recent call last):
  File "C:/Users/ACAS/PycharmProjects/ACAS/Main/Tester/clean.py", line 12, in <module>
    pd.DataFrame.to_sql(x, 'ccn', engine, if_exists='append')
  File "C:\Python27\lib\site-packages\pandas\core\generic.py", line 2531, in to_sql
    dtype=dtype, method=method)
  File "C:\Python27\lib\site-packages\pandas\io\sql.py", line 460, in to_sql
    chunksize=chunksize, dtype=dtype, method=method)
  File "C:\Python27\lib\site-packages\pandas\io\sql.py", line 1174, in to_sql
    table.insert(chunksize, method=method)
  File "C:\Python27\lib\site-packages\pandas\io\sql.py", line 664, in insert
    keys, data_list = self.insert_data()
  File "C:\Python27\lib\site-packages\pandas\io\sql.py", line 619, in insert_data
    "duplicate name in index/columns: {0}".format(err))
ValueError: duplicate name in index/columns: cannot insert Datetime, already exists

我尝试从模型中删除“ Datetime”。我似乎找不到解决方法,也许我很天真,而且很容易做到。

0 个答案:

没有答案