前提・実現したいこと PyPortfolioOptを用いたポートフォリオの最適化
行に年度(時間軸)、列に銘柄を文字列で表して、Excelで表にしています。データ(株価に相当としています)はfloat型です。
発生している問題・エラーメッセージ
TypeError Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\ops.py in na_op(x, y)
1201 try:
-> 1202 result = expressions.evaluate(op, str_rep, x, y, **eval_kwargs)
1203 except TypeError:
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\expressions.py in evaluate(op, op_str, a, b, use_numexpr, **eval_kwargs)
203 if use_numexpr:
--> 204 return _evaluate(op, op_str, a, b, **eval_kwargs)
205 return _evaluate_standard(op, op_str, a, b)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\expressions.py in _evaluate_numexpr(op, op_str, a, b, truediv, reversed, **eval_kwargs)
118 if result is None:
--> 119 result = _evaluate_standard(op, op_str, a, b)
120
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\computation\expressions.py in _evaluate_standard(op, op_str, a, b, **eval_kwargs)
63 with np.errstate(all='ignore'):
---> 64 return op(a, b)
65
TypeError: unsupported operand type(s) for /: 'str' and 'float'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-84-b579b6ebf543> in <module>()
----> 1 expected_returns.mean_historical_return(df, frequency=252)
C:\ProgramData\Anaconda3\lib\site-packages\pypfopt\expected_returns.py in mean_historical_return(prices, frequency)
35 warnings.warn("prices are not in a dataframe", RuntimeWarning)
36 prices = pd.DataFrame(prices)
---> 37 daily_returns = prices.pct_change().dropna(how="all")
38 return daily_returns.mean() * frequency
39
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in pct_change(self, periods, fill_method, limit, freq, **kwargs)
6889
6890 rs = (data.div(data.shift(periods=periods, freq=freq, axis=axis,
-> 6891 **kwargs)) - 1)
6892 if freq is None:
6893 mask = isna(_values_from_object(self))
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\ops.py in f(self, other, axis, level, fill_value)
1258
1259 if isinstance(other, ABCDataFrame): # Another DataFrame
-> 1260 return self._combine_frame(other, na_op, fill_value, level)
1261 elif isinstance(other, ABCSeries):
1262 return self._combine_series(other, na_op, fill_value, axis, level)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in _combine_frame(self, other, func, fill_value, level, try_cast)
3923
3924 else:
-> 3925 result = _arith_op(this.values, other.values)
3926
3927 return self._constructor(result, index=new_index, columns=new_columns,
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in _arith_op(left, right)
3892 right[right_mask & mask] = fill_value
3893
-> 3894 return func(left, right)
3895
3896 if this._is_mixed_type or other._is_mixed_type:
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\ops.py in na_op(x, y)
1219 if np.prod(xrav.shape) and np.prod(yrav.shape):
1220 with np.errstate(all='ignore'):
-> 1221 result[mask] = op(xrav, yrav)
1222 elif hasattr(x, 'size'):
1223 result = np.empty(x.size, dtype=x.dtype)
TypeError: unsupported operand type(s) for /: 'str' and 'str'
該当のソースコード
expected_returns.mean_historical_return(df, frequency=252)
試したこと
def get_list_2d_all(sheet):
return [sheet.row_values(row) for row in range(sheet.nrows)]
df = get_list_2d_all(sheet)
pprint.pprint(df)
や
df = pd.read_csv("filename.csv").values.tolist()
print(df)
を試しました...
Python初心者で、ネット上にあるコードを参考にさせていただいております。
上記のエラーはdef get_list_2d_all(sheet):
return [sheet.row_values(row) for row in range(sheet.nrows)]
のときのものです。
補足情報(FW/ツールのバージョンなど)
言語はPython3、開発環境はjupyterです。
あなたの回答
tips
プレビュー