Python pandas-datareaderでVIX(恐怖指数)取得

# ライブラリの読み込み
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import pandas_datareader.data as web
import pandas as pd

# fredからVixを取得する
vix = web.DataReader("VIXCLS", "fred", "1949/1/1")

FREDというのはアメリカにあるセントルイス中央銀行のデータベース。

pandas-datareaderは仕様上時々死ぬことがあるそうなので注意。

ネタ元

tkstock.site

Python 日付や時刻をYYMMDDhhmmssなどの形式に書式化する

ネタ元

atmarkit.itmedia.co.jp

# datetimeモジュールを使用
import datetime

t_delta = datetime.timedelta(hours=9)
JST = datetime.timezone(t_delta, 'JST')
now = datetime.datetime.now(JST)
print(repr(now))
# 出力例
#datetime.datetime(2021, 11, 4, 17, 37, 28, 114417, tzinfo=datetime.timezone
#(datetime.timedelta(seconds=32400), 'JST'))
print(now)  # 2021-11-04 17:37:28.114417+09:00

# YYYYMMDDhhmmss形式に書式化
d = now.strftime('%Y%m%d%H%M%S')
print(d)  # 20211104173728

d = f'{now:%Y%m%d%H%M%S}'  # f文字列
d = format(now, '%Y%m%d%H%M%S')  # format関数
d = '{:%Y%m%d%H%M%S}'.format(now)  # 文字列のformatメソッド
print(d)  # 20211104173728

# YYYY/MM/DD hh:mm:ss形式に書式化
d = now.strftime('%Y/%m/%d %H:%M:%S')
print(d)  # 2021/11/04 17:37:28

# MM/DD/YY hh:mm:ss形式に書式化
d = now.strftime('%x %X')
print(d)  # 11/04/21 17:37:28

# 日付のみを書式化
d = now.date().strftime('%Y/%m/%d')
print(d)  # 2021/11/04

# 時刻のみを書式化
t = now.time().strftime('%X')
print(t)  # 17:37:28

# 西暦を2桁に
d = now.strftime('%y/%m/%d %H:%M:%S')
print(d)  # 21/11/04 17:37:28

# 12時間制+AM/PM表示
d = now.strftime('%Y/%m/%d %I:%M(%p)')
print(d)  # 2021/11/04 05:11(PM)

# 曜日を含む日付
d = now.strftime('%Y年%m月%d日(%a)')
print(d)  # 2021年11月04日(Thu)

d_week = {'Sun': '日', 'Mon': '月', 'Tue': '火', 'Wed': '水',
          'Thu': '木', 'Fri': '金', 'Sat': '土'}
key = now.strftime('%a')
w = d_week[key]
d = now.strftime('%Y年%m月%d日') + f'({w})' #f'{now:%Y年%m月%d日}({w})'
print(d)  # 2021年11月04日(木)

d_week = '日月火水木金土日'  # インデックス0の'日'は使用されない
idx = now.strftime('%u')  # '%u'では月曜日がインデックス'1'となる
w = d_week[int(idx)]
d = now.strftime('%Y年%m月%d日') + f'({w})'
print(d)  # 2021年11月04日(木)

# タイムゾーン
d = now.strftime('%X%z(%Z)')
print(d)  # 17:37:28+0900(JST)

# timeモジュールを使用
import time

now = time.localtime()
print(now)
# 出力例
#time.struct_time(tm_year=2021, tm_mon=11, tm_mday=4, tm_hour=19, tm_min=40,
#tm_sec=1, tm_wday=3, tm_yday=308, tm_isdst=0)

d = time.strftime('%Y%m%d%H%M%S', now)  # YYYYMMDDhhmmssに書式化
d = time.strftime('%Y/%m/%d %H:%M:%S', now)  # YYYY/MM/DD hh:mm:ssに書式化
d = time.strftime('%x %X', now)  # MM/DD/YY hh:mm:ssに書式化
d = time.strftime('%y/%m/%d %H:%M:%S', now)  # YY/MM/DD hh:mm:ssに書式化
d = time.strftime('%Y/%m/%d %I:%M(%p)', now)  # 12時間制+AM/PM表示
d = time.strftime('%Y年%m月%d日(%a)', now)  # 曜日を含む日付

d_week = {'Sun': '日', 'Mon': '月', 'Tue': '火', 'Wed': '水',
          'Thu': '木', 'Fri': '金', 'Sat': '土'}
key = time.strftime('%a', now)
w = d_week[key]
d = time.strftime('%Y年%m月%d日', now) + f'({w})'
print(d)  # 2021年11月04日(木)

Pythonで文字を数値変換

Pythonで数字の文字列strを数値int, floatに変換

整数ならint()

print(int('100'))
print(type(int('100')))
# 100
# <class 'int'>

浮動小数点数に変換: float()

print(float('1.23'))
print(type(float('1.23')))
# 1.23
# <class 'float'>

2進数、8進数、16進数表記の文字列を数値に変換

print(int('100', 2))
print(int('100', 8))
print(int('100', 16))
# 4
# 64
# 256

ネタ元

note.nkmk.me

Python SQLite3 selectで一行を取得する(fetchone)

# coding: utf-8
import sqlite3

conn = sqlite3.connect(r'E:\dev\sqlite-test\test1.db')

c = conn.cursor()

c.execute("select * from syain")

list1 = c.fetchone()

print (list1) #(1, '鈴木', 'suzuki')
print (list1[0]) # 1
print (list1[1]) # 鈴木'
print (list1[2]) # suzuki

conn.close() 

ネタ元

itsakura.com



itertuplesを使う方法もある
itertuplesを使うと行毎にindexやカラム名、値をタプルで取得できる。

for t in df.itertuples():
    print(t)
>>
Pandas(Index=0, colA=1, colB='a', colC=5)
Pandas(Index=1, colA=2, colB='b', colC=6)
Pandas(Index=2, colA=3, colB='c', colC='e')
Pandas(Index=3, colA=4, colB='d', colC='f')