質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 2.7

Python 2.7は2.xシリーズでは最後のメジャーバージョンです。Python3.1にある機能の多くが含まれています。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

1613閲覧

pythonによるデータ分析入門(オライリー)の本(p389~)についての質問

yutakao

総合スコア10

Python 2.7

Python 2.7は2.xシリーズでは最後のメジャーバージョンです。Python3.1にある機能の多くが含まれています。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2018/10/27 11:21

編集2018/10/28 09:21

お世話になります。
pythonによるデータ分析入門(オライリー)の本で
11.2グループの変換と分析の11.2.2分位分析(P389~)について質問です。

spyデータの入手
ーーーーーーーーーーーーーーー

data=web.DataReader("SPY","iex","2016-01-01")

使用しやすいデータに変える
ーーーーーーーーーーーーーーー

px=data["close"] returns=px.pct_change()

関数製作?
ーーーーーーーーーーーーーーー

def to_index(rets): index=(1+rets).cumprod() first_loc=max(index.notnull().argmax()-1,0) index.values[first_loc]=1 return index
def trend_signal(rets,lookback,lag): signal=rets.rolling(lookback,min_periods=lookback-5).sum() return signal.shift(lag)

この関数を使用して、毎週金曜日のモメンタム指標を使った取引をする投資戦略の作成とテストを行うことができます。
ーーーーーーーーーーーーーーー

signal=trend_signal(returns,100,3) trade_friday=signal.resample("W-FRI").mean().resample("B").ffill() trade_rets=trade_friday.shift(1)*returns to_index(trade_rets).plot()

とするとエラーがでる。
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'

TypeError Traceback (most recent call last)
<ipython-input-56-21cb90a97e71> in <module>()
2 signal=trend_signal(returns,100,3)
3 #signal.index=pd.to_datetime(signal.index)
----> 4 trade_friday=signal.resample("W-FRI").mean().resample("B").ffill()#.mean()の記載はないが記載しないと将来使えないよと注意が出る。
5 trade_rets=trade_friday.shift(1)*returns
6 #trade_rets.index=pd.to_object(trade_tets.index)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in resample(self, rule, how, axis, fill_method, closed, label, convention, kind, loffset, limit, base, on, level)
5520 axis=axis, kind=kind, loffset=loffset,
5521 convention=convention,
-> 5522 base=base, key=on, level=level)
5523 return _maybe_process_deprecations(r,
5524 how=how,

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\resample.py in resample(obj, kind, **kwds)
997 """ create a TimeGrouper and return our resampler """
998 tg = TimeGrouper(**kwds)
--> 999 return tg._get_resampler(obj, kind=kind)
1000
1001

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\resample.py in _get_resampler(self, obj, kind)
1114 raise TypeError("Only valid with DatetimeIndex, "
1115 "TimedeltaIndex or PeriodIndex, "
-> 1116 "but got an instance of %r" % type(ax).name)
1117
1118 def _get_grouper(self, obj, validate=True):

TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'

インデックスがobjectだからダメなのかと思い。

signal=trend_signal(returns,100,3) signal.index=pd.to_datetime(signal.index)#追加 trade_friday=signal.resample("W-FRI").mean().resample("B").ffill() trade_rets=trade_friday.shift(1)*returns to_index(trade_rets).plot()

とするとエラーがでる。
ValueError: Cannot add integral value to Timestamp without freq.

ValueError Traceback (most recent call last)
<ipython-input-57-eb0ae3dff6b9> in <module>()
7
8 #次に、この戦略のリターンをリターンインデックスに変換して、グラフを描きます。
----> 9 to_index(trade_rets).plot()
10
11 #分からないのでstop!

<ipython-input-49-03cae7f6c268> in to_index(rets)
5 def to_index(rets):
6 index=(1+rets).cumprod()
----> 7 first_loc=max(index.notnull().idxmax()-1,0)
8 index.values[first_loc]=1
9 return index

pandas/_libs/tslib.pyx in pandas._libs.tslib._Timestamp.sub()

pandas/_libs/tslib.pyx in pandas._libs.tslib._Timestamp.add()

ValueError: Cannot add integral value to Timestamp without freq.

まだまだ勉強不測で対処方が全く対処が分からないです。
分かる方がいらっしゃいましたら御教示いただきたく宜しくお願いたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ikedas

2018/10/28 07:43

出力されたエラーの全文を提示して下さい。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問