파이썬 오류 SyntaxError: positional argument follows keyword argument 이걸 어떻게 수정할 수 있을까요?

조회수 25092회

파이썬을 활용한 금융분석이라는 책의 예제를 따라하고 있었는데요. 난데없이 제목과 같은 오류가 떴습니다. 예제는 구글의 변동성을 계산하는 예제인데요. matplotlib라는 것도 임포트했고 지금 몇번을 봐도 예제대로 따라 했는데 오류가 나와서 난감합니다. 원래는 구글의 주가와 연간 변동성이 떠야 하거든요 ㅠ. 프로그래밍 고수님들의 조언을 구합니다.

무슨 인자가 키워드 인자를 따른다 뭐 그런 소리 같은데 저 같은 초보에게는 넘사벽이네요.

SyntaxError: positional argument follows keyword argument

이미지

이미지

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#p44"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import pandas_datareader.data as web\n",
    "import matplotlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2014-04-08</th>\n",
       "      <td>542.60</td>\n",
       "      <td>555.00</td>\n",
       "      <td>541.61</td>\n",
       "      <td>554.90</td>\n",
       "      <td>3152406</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-04-09</th>\n",
       "      <td>559.62</td>\n",
       "      <td>565.37</td>\n",
       "      <td>552.95</td>\n",
       "      <td>564.14</td>\n",
       "      <td>3324742</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-04-10</th>\n",
       "      <td>565.00</td>\n",
       "      <td>565.00</td>\n",
       "      <td>539.90</td>\n",
       "      <td>540.95</td>\n",
       "      <td>4027743</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-04-11</th>\n",
       "      <td>532.55</td>\n",
       "      <td>540.00</td>\n",
       "      <td>526.53</td>\n",
       "      <td>530.60</td>\n",
       "      <td>3916171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-04-14</th>\n",
       "      <td>538.25</td>\n",
       "      <td>544.10</td>\n",
       "      <td>529.56</td>\n",
       "      <td>532.52</td>\n",
       "      <td>2568020</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Open    High     Low   Close   Volume\n",
       "Date                                               \n",
       "2014-04-08  542.60  555.00  541.61  554.90  3152406\n",
       "2014-04-09  559.62  565.37  552.95  564.14  3324742\n",
       "2014-04-10  565.00  565.00  539.90  540.95  4027743\n",
       "2014-04-11  532.55  540.00  526.53  530.60  3916171\n",
       "2014-04-14  538.25  544.10  529.56  532.52  2568020"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "goog = web.DataReader('GOOG', data_source='google', start='3/14/2009', end ='4/14/2014')\n",
    "goog.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "goog['Log_Ret']=np.log(goog['Close'] / goog['Close'].shift(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "goog['Volatility'] = pd.rolling_std(goog['Log_Ret'], window=252) * np.sqrt(252)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (<ipython-input-15-a5875be68fbd>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-15-a5875be68fbd>\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m    goog[['Close', 'Volatility']].plot(subplots=True, color='blue',figsize(8,6))\u001b[0m\n\u001b[0m                                                                  ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "%matplotlib.inline\n",
    "goog[['Close', 'Volatility']].plot(subplots=True, color='blue',figsize(8,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}

1 답변

  • plot이라는 함수를 호출하실때 인자를 넣는 순서가 잘못되었습니다.

    함수를 정의할 때 아래 func와 같이 기본값을 설정할 수 있는데요. 다음과 같은 이유에서 에러가 발생한 것 같습니다. plot함수의 정의를 살펴보시는게 좋겠습니다.

    def func(a, b, c=3,d=4):
        print(a,b,c,d)
    
    func(1,2)   # 1 2 3 4이라고 출력. 값을 입력하지 않으면 c는 자동으로 3,d는 자동으로 4
    func(1,2,d=5,c=4) # 1 2 4 5라고 출력
    func(1,2,d=5,c=4) # 1 2 4 5라고 출력
    func(d=5,c=4,1,2) # 에러 발생. d와 c를 명시해서 값을 지정하려면 a,b와 같은 Positional argument뒤에 c,d와 같은 keyword argument가 와야 합니다.
    

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)