LoginSignup
2
1

More than 5 years have passed since last update.

pythonのpandasでcsvから時系列データを読み込みたい

Last updated at Posted at 2019-02-03

ある時系列データを格納したcsvをpandasで処理していい感じのグラフを出したくなった.

csvを読み込みたい

まずは, csvを読み込む.

read.py
import pandas as pd
df = pd.read_csv('./raw_data.csv', index_col=['date'], parse_dates=['date'])

このとき, オプション指定でindex_col=['date']を選択することでdataFrameのindexをcsvの'date'カラムに指定できる.
csvで日付を格納しているdateカラムがstringとして扱われている. このままでは処理がしづらい上plotもできないので, parse_dates=['date']のオプションを指定することでstringをdateを扱うためのtimestampに変換している.

これらのオプション指定でカラム指定する際は
['指定したいカラム']
としなければならないので気をつけましょう.(3敗)

bad_example.py
df = pd.read_csv('./raw_data.csv', index_col='date', parse_dates='date')

結果

時系列データの扱いが楽になる!

参考

pandasで様々な日付フォーマットを取り扱う
Error occurring while retrieving a csv data file in python

2
1
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
2
1