如何将np.int64转换为PandasSeries的python int64?

时间:2019-07-02 13:18:29

标签: python pandas postgresql numpy types

我尝试将pandas DataFrame中的数据插入PostgreSQL表中,

尝试插入的

表如下:

city_id date        forecast
5       29.05.2019  0
1       29.05.2019  0
151     29.05.2019  0
55      29.05.2019  0
...

类型:

  • city_id-numpy.int64
  • date-datetime.date
  • forecast-numpy.int64

以及将数据插入db的代码块

        with psycopg2.connect(f"host='{hostname}' \
                              dbname='{database}' \
                              user='{username}' \
                              password='{password}'") as connection:
            with connection.cursor() as cursor:
                connection.set_client_encoding('UTF8')
                for i in df_with_new_one.index:
                    date = df_with_new_one['date'][i]
                    city_id = df_with_new_one['city_id'][i]
                    value = df_with_new_one['forecast'][i]

                    cursor.execute("INSERT INTO forecast \
                    (city_id, computed_time, date, value) \
                    VALUES (%s, %s, %s, %s)", (city_id, now, date, value)) 

其中now被保存为datetime.datetime.now()的时间

然后我得到 ProgrammingError

    ProgrammingError: can't adapt type 'numpy.int64'

我检查了类型type(df_with_new_one['forecast'][0])类型为numpy.int64

所以我发现PostreSQL只能读取pythonic intfloat,而我尝试的第一件事是将np.int64转换为简单的int: >

  • tolist()
  • pd.to_numeric()
  • int() for ((int(city_id), now, date, int(value))
  • .astype(int)
  • .value.astype('int')

更新。

  • city_id = int(df_with_new_one['city_id'][i]) value = int(df_with_new_one['forecast'][i])

不幸的是,他们都不对我有用

尝试int()时出现另一个错误:

    TypeError: cannot convert the series to <class 'int'>

我找到的答案,但没有人帮助我

是否有任何其他方法来更改值的类型?

3 个答案:

答案 0 :(得分:0)

您可以使用numpy.ndarray.item()

type(np.arange(1)[0])
# numpy.int64
type(np.arange(1)[0].item())
# int

答案 1 :(得分:0)

首先,您的方法效率不高,因为您反复调用cursor.execute而不是调用cursor.executemany

但是当您传递个人价值观时,很容易及时地将它们转换:

            ...
            for i in df_with_new_one.index:
                date = df_with_new_one['date'][i]
                city_id = int(df_with_new_one['city_id'][i])
                value = int(df_with_new_one['forecast'][i])
            ...

答案 2 :(得分:0)

问题出在错误的索引编制中:

  • 第一个索引是从83到1161,在1161之后应该是1161,之后又是83,下一个值是83 + 1等。

因此,问题已通过.reset_index()

解决

df_with_new_one.reset_index(drop = True, inplace = True)

谢谢大家的回答!