質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
SQLite

SQLiteはリレーショナルデータベース管理システムの1つで、サーバーではなくライブラリとして使用されている。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

pandas

Pandasは、PythonでRにおけるデータフレームに似た型を持たせることができるライブラリです。 行列計算の負担が大幅に軽減されるため、Rで行っていた集計作業をPythonでも比較的簡単に行えます。 データ構造を変更したりデータ分析したりするときにも便利です。

Q&A

解決済

1回答

539閲覧

pandasのto_sqlメソッドでcomplex型のデータをデータベースに出力したい

sandyur

総合スコア25

SQLite

SQLiteはリレーショナルデータベース管理システムの1つで、サーバーではなくライブラリとして使用されている。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

pandas

Pandasは、PythonでRにおけるデータフレームに似た型を持たせることができるライブラリです。 行列計算の負担が大幅に軽減されるため、Rで行っていた集計作業をPythonでも比較的簡単に行えます。 データ構造を変更したりデータ分析したりするときにも便利です。

1グッド

1クリップ

投稿2018/02/13 06:29

編集2018/02/14 06:43

前提・実現したいこと

pandasのto_sqlメソッドでcomplex型のデータをデータベースに出力したいです。
以下にエラーメッセージを添付しています。
complex型がSQLで取り扱えないのであれば、実数と、虚数のjを抜かした整数値だけを保存する形にしようと思っています。

発生している問題・エラーメッセージ

ValueError Traceback (most recent call last) <ipython-input-1618-79da8563256b> in <module>() 1 #sqliteに保存 ----> 2 fftDataframe.to_sql('{table}'.format(table='tablename_new'), con_new, index=False, if_exists='replace') ~/Applications/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py in to_sql(self, name, con, flavor, schema, if_exists, index, index_label, chunksize, dtype) 1360 sql.to_sql(self, name, con, flavor=flavor, schema=schema, 1361 if_exists=if_exists, index=index, index_label=index_label, -> 1362 chunksize=chunksize, dtype=dtype) 1363 1364 def to_pickle(self, path, compression='infer'): ~/Applications/anaconda3/lib/python3.6/site-packages/pandas/io/sql.py in to_sql(frame, name, con, flavor, schema, if_exists, index, index_label, chunksize, dtype) 469 pandas_sql.to_sql(frame, name, if_exists=if_exists, index=index, 470 index_label=index_label, schema=schema, --> 471 chunksize=chunksize, dtype=dtype) 472 473 ~/Applications/anaconda3/lib/python3.6/site-packages/pandas/io/sql.py in to_sql(self, frame, name, if_exists, index, index_label, schema, chunksize, dtype) 1502 table = SQLiteTable(name, self, frame=frame, index=index, 1503 if_exists=if_exists, index_label=index_label, -> 1504 dtype=dtype) 1505 table.create() 1506 table.insert(chunksize) ~/Applications/anaconda3/lib/python3.6/site-packages/pandas/io/sql.py in __init__(self, *args, **kwargs) 1265 # (this is what sqlalchemy does) 1266 sqlite3.register_adapter(time, lambda _: _.strftime("%H:%M:%S.%f")) -> 1267 super(SQLiteTable, self).__init__(*args, **kwargs) 1268 1269 def sql_schema(self): ~/Applications/anaconda3/lib/python3.6/site-packages/pandas/io/sql.py in __init__(self, name, pandas_sql_engine, frame, index, if_exists, prefix, index_label, schema, keys, dtype) 563 if frame is not None: 564 # We want to initialize based on a dataframe --> 565 self.table = self._create_table_setup() 566 else: 567 # no data provided, read-only mode ~/Applications/anaconda3/lib/python3.6/site-packages/pandas/io/sql.py in _create_table_setup(self) 1301 """ 1302 column_names_and_types = \ -> 1303 self._get_column_names_and_types(self._sql_type_name) 1304 1305 pat = re.compile('\s+') ~/Applications/anaconda3/lib/python3.6/site-packages/pandas/io/sql.py in _get_column_names_and_types(self, dtype_mapper) 759 dtype_mapper(self.frame.iloc[:, i]), 760 False) --> 761 for i in range(len(self.frame.columns)) 762 ] 763 ~/Applications/anaconda3/lib/python3.6/site-packages/pandas/io/sql.py in <listcomp>(.0) 759 dtype_mapper(self.frame.iloc[:, i]), 760 False) --> 761 for i in range(len(self.frame.columns)) 762 ] 763 ~/Applications/anaconda3/lib/python3.6/site-packages/pandas/io/sql.py in _sql_type_name(self, col) 1356 1357 elif col_type == "complex": -> 1358 raise ValueError('Complex datatypes not supported') 1359 1360 if col_type not in _SQL_TYPES: ValueError: Complex datatypes not supported

該当のソースコード

#sqliteに保存 Dataframe.to_sql('{table}'.format(table='tablename_new'), con_new, index=False, if_exists='replace')

ご教示いただけるとうれしいです。
よろしくお願いします。

補足

先程pythonのドキュメントで、

SQLite はネイティブで TEXT、INTEGER、REAL、BLOB および NULL のみをサポートしています。その他のタイプを使用したい場合はあなた自身で追加しなければなりません。detect_types パラメーターおよび、register_converter() 関数でモジュールレベルで登録できるカスタム 変換関数 を使用することで簡単に追加できます。

と書いてあるのを発見しました。
この2つを使うのがいいのでしょうか??

magichan👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

実部と虚部を別々に抜き出してからやってはいかがですか。

python

1import numpy as np 2df['real-part'] = df['complex-number'].apply(np.real) 3df['imag-part'] = df['complex-number'].apply(np.imag)

投稿2018/02/14 08:22

mkgrei

総合スコア8560

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問