4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pd.DataFrame: df.to_csv()で保存した複素数データをdf.read_csv()で読み込む方法

Posted at

解決に数時間かかったのでメモします。

問題点

端的にいうと、

  1. complex型でpd.DataFrameを作成
  2. df.to_csv()でcsv保存
  3. df.read_csv()でcsv読み込み
  4. 型を調べるとobject型になってる
  5. 色々頑張ったがうまくcomplex型に戻せない
    といった感じ、詳細は以下のissueを参照。
    https://github.com/pandas-dev/pandas/issues/9379

解決策

以下のissueでいい感じの解答。(上記issueの解答から飛べた)
https://stackoverflow.com/questions/16659818/how-to-read-complex-numbers-from-file-with-numpy

read_df_complex.py
import pandas as pd

df = pd.read_csv('data.csv')
df = df.apply(lambda col: col.apply(lambda val: complex(val.strip('()'))))

まとめ

pandasは複素数型に弱いらしい。あまり需要がないのかな。

余談

マークダウン記法いいね。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?