Python error when calling column data from Pandas DataFrame
<p>I was practicing to import stock market data from Google Finance into a Pandas DataFrame:</p>

<pre><code>import pandas as pd
from pandas import Series

path = 'http://www.google.com/finance/historical?cid=542029859096076&amp;startdate=Sep+22%2C+2001&amp;enddate=Sep+20%2C+2016&amp;num=30&amp;ei=3HvhV4n3D8XGmAGp4q74Ag&amp;output=csv'
df = pd.read_csv(path)
</code></pre>

<p>So far so good, and df also shows the complete data set I need.</p>

<p>However, when calling particular columns, like</p>

<pre><code>df['Date']
</code></pre>

<p>Python shows the error codes below:</p>

<pre><code>Traceback (most recent call last):

  File "&lt;ipython-input-31-cb486dd31fbc&gt;", line 1, in &lt;module&gt;
    df['Date']

  File "/Users/Username/anaconda/lib/python3.5/site-packages/pandas/core/frame.py", line 1997, in __getitem__
    return self._getitem_column(key)

  File "/Users/Username/anaconda/lib/python3.5/site-packages/pandas/core/frame.py", line 2004, in _getitem_column
    return self._get_item_cache(key)

  File "/Users/Username/anaconda/lib/python3.5/site-packages/pandas/core/generic.py", line 1350, in _get_item_cache
    values = self._data.get(item)

  File "/Users/Username/anaconda/lib/python3.5/site-packages/pandas/core/internals.py", line 3290, in get
    loc = self.items.get_loc(item)

  File "/Users/Username/anaconda/lib/python3.5/site-packages/pandas/indexes/base.py", line 1947, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))

  File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)

  File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)

  File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)

  File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)

KeyError: 'Date'
</code></pre>

<p>On the other hand, other columns such as df['High'] turns out to be okay. Is there anyway I can fix this issue?</p>

<p>this CSV file contains <a href="http://stackoverflow.com/questions/17912307/u-ufeff-in-python-string">BOM (Byte Order Mark) signature</a>, so try it this way:</p>

<pre><code>df = pd.read_csv(path, encoding='utf-8-sig')
</code></pre>

<p>How one can easily identify this problem (thanks to <a href="http://stackoverflow.com/questions/39601251/python-error-when-calling-column-data-from-pandas-dataframe?noredirect=1#comment66511340_39601325">@jezrael's hint</a>):</p>

<pre><code>In [11]: print(df.columns.tolist())
['\ufeffDate', 'Open', 'High', 'Low', 'Close', 'Volume']
</code></pre>

<p>and pay attention at the first column</p>

<p><strong>NOTE:</strong> as <a href="http://stackoverflow.com/questions/39601251/python-error-when-calling-column-data-from-pandas-dataframe/39601325#comment66511256_39601251">@ayhan</a> has noticed, starting with version 0.19.0 <a href="https://pandas-docs.github.io/pandas-docs-travis/whatsnew.html" rel="nofollow">Pandas will take care of it automatically</a>:</p>

<p>Bug in pd.read_csv() which caused BOM files to be incorrectly parsed by not ignoring the BOM <a href="https://github.com/pydata/pandas/issues/4793" rel="nofollow">GH4793</a></p>

python pandas dataframe