1
2

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 3 years have passed since last update.

pandasでシュッと祝日データをデータフレームにする

Posted at

TL;DR

from io import StringIO
import urllib.request

import pandas as pd

req = urllib.request.Request('https://holidays-jp.github.io/api/v1/date.csv')
with urllib.request.urlopen(req) as res:
    df_holiday = pd.read_csv(StringIO(res.read().decode()), header=None)

コード解説

祝日データを取得できる場所へgetリクエストを投げる

req = urllib.request.Request('https://holidays-jp.github.io/api/v1/date.csv')

シンプル いず ベスト

取得リクエストからデータを抽出する

with urllib.request.urlopen(req) as res:
    df_holiday = pd.read_csv(StringIO(res.read().decode()), header=None)

リクエストデータはバイナリデータなのでdecode関数でstringへ変換し、その入力をStringIOを使ってread_csv関数に流しています。

まとめ

シンプルに作るならこの方法。より詳細に祝日データが欲しい場合は jpholiday ライブラリを使うと幸せになれるかも。

参考記事

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?