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

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

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

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

Q&A

0回答

273閲覧

ExcelまたはCSVファイルのデータをPythonで読み込んだquandlデータのデータフレームのような形式にできますでしょうか

yaga

総合スコア12

Python

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

0グッド

0クリップ

投稿2018/10/25 05:05

前提・実現したいこと 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です。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問