2017-12-11 29 views
1

これはなぜPython 2.7では動作するのですが、Python 3では動作しない理由は何ですか?私はpsycopg2を使用してテーブルに値を挿入していますが、カーソルは私のパンダのデータフレームにcolumn2で苦労しています。python 2とpython 3の違いは何ですか?

In [1]: df.info() 
<class 'pandas.core.frame.DataFrame'> 
RangeIndex: 11565 entries, 0 to 11564 
Data columns (total 3 columns): 
column1 11565 non-null object 
column2 11565 non-null int64 
column3 11565 non-null object 
dtypes: int64(1), object(2) 
memory usage: 271.1+ KB 

# Python 2.7 
cur = conn.cursor() 

# pass table values 
vals = [cur.mogrify("(%s, %s, %s)", (x, y, z)) for x, y, z in zip(df['column1'], df['column2'],df['column3'])] 
# Great! 

-

# Python 3.5 
cur = conn.cursor() 

# pass table values 
vals = [cur.mogrify("(%s, %s, %s)", (x, y, z)) for x, y, z in zip(df['column1'], df['column2'],df['column3'])] 

<ipython-input-2-ee4818a2eb52> in <listcomp>(.0) 
     1 # pass table values 
----> 2 vals = [cur.mogrify("(%s, %s, %s)", (x, y, z)) for x, y, z in zip(df['column1'], df['column2'],df['column3'])] 

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

これはpsycopg2自体または基礎となるPythonのバージョンの異なるバージョンを実行する必要がありますか?ありがとう。

答えて

2

私は関連するdiferenceがネイティブのPython型から内部型へのNumPyの自動マッピングにあると思います。 their docsから:

警告:タイプintは、もはや固定幅の整数型であるため、int_タイプは、ビルトインのPython 3の下intを継承しません。

どこかあなたのコードでは、numpyの結果から値を取得している、とPython 2の値は、ネイティブintのですが、それが何を知らなかったので、Pythonの3に、彼らはまだnumpy.int64 sでありそれらを変換する。私はあなたがそれをどのように修正するか分かりません。

+1

これは適切な回答のようです。 [Psycopg2はnumpyのint64型をサポートしていないようです](https://stackoverflow.com/questions/41449501/error-inserting-values-to-db-with-psycopg2-module)。 [関連するGitHubのページ](https://github.com/musically-ut/psycopg2_numpy_ext)も参照してください。 – Dan

関連する問題