LoginSignup
9
5

More than 5 years have passed since last update.

pandasで「Initializing from file failed」というエラーが出てしまった時の対処法[anaconda]

Last updated at Posted at 2018-03-14

はじめに

「pythonによる機械学習入門」という本をコードを書きながら読んでいたら、エラーでつまづいたのでメモしておきます。

環境

anaconda 1.6.9
python 3.6.4

エラーが出た状況と原因

コードの内容を簡単に言うと、csvファイルからデータを読み取り図式化するものだったのですが書いてある通りに実行しても

OSError: Initializing from file failed

と表示されて実行完了しませんでした。そこで、このエラー名で検索するとファイルのパス名に日本語を含む場合に発生するエラーだとのこと。自分はフォルダ名には日本語を含んでいなかったのですが、確かに配布されているcsvファイルには「高松」という日本語が含まれていました。
Python3.6のpandasで「Initializing from file failed」が起きた場合の対策

対策

 この「高松」を「takamatsu」に変更して実行してみるとうまくいきました。この後も同様の問題が起こるのは困るので、原因と思われるpandasのアップデートを試みました。

 自分と同じ環境であればAnaconda Prompt上でpip install -U pandasと実行してpandasをアップデートしてみると日本語を含むファイルでも実行することができました。

pip install -U pandas
Requirement already up-to-date: pandas in c:\programdata\anaconda3\lib\site-packages
Collecting python-dateutil>=2 (from pandas)
  Downloading python_dateutil-2.7.0-py2.py3-none-any.whl (207kB)
    100% |████████████████████████████████| 215kB 1.8MB/s
Collecting pytz>=2011k (from pandas)
  Downloading pytz-2018.3-py2.py3-none-any.whl (509kB)
    100% |████████████████████████████████| 512kB 2.9MB/s
Collecting numpy>=1.9.0 (from pandas)
  Downloading numpy-1.14.2-cp36-none-win_amd64.whl (13.4MB)
    100% |████████████████████████████████| 13.4MB 2.3MB/s
Requirement already up-to-date: six>=1.5 in c:\programdata\anaconda3\lib\site-packages (from python-dateutil>=2->pandas)
Installing collected packages: python-dateutil, pytz, numpy
  Found existing installation: python-dateutil 2.6.1
    Uninstalling python-dateutil-2.6.1:
      Successfully uninstalled python-dateutil-2.6.1
  Found existing installation: pytz 2017.3
    Uninstalling pytz-2017.3:
      Successfully uninstalled pytz-2017.3
  Found existing installation: numpy 1.14.0
    Uninstalling numpy-1.14.0:
      Successfully uninstalled numpy-1.14.0
Successfully installed numpy-1.14.2 python-dateutil-2.7.0 pytz-2018.3

pandas自体はすでにアップデートされていると表示されましたが、numpyやdateutil,pytzなどがアップデートされて(2018/3/14時点)うまくいきました。
anacondaは2018/02ごろに入れたばかりのため、anacondaを利用している多くの人のpandasは自分と同じバージョンであると思うので、ぜひアップデートしてみてください。

9
5
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
9
5