Guided Project: Storytelling Data Visualization on Exchange Rates

Hello There there is an error which occurred while after start of my Guided Project: Storytelling Data Visualization on Exchange Rates at the second page was telling me to copy and paste the given code in to jupiter note book unfortunately the code has an error while i was trying to run the code i face with key error: ‘Time’ as well as i cannot rename the first columns title with time. if anyone there would help me with that i will be appreciated.

Kind Regards to DQ Team

Link : Learn data science with Python and R projects

exchange_rates.rename(columns={’[US dollar ]’: ‘US_dollar’,
‘Period\Unit:’: ‘Time’},
inplace=True)
exchange_rates[‘Time’] = pd.to_datetime(exchange_rates[‘Time’])
exchange_rates.sort_values(‘Time’, ignore_index=True,
inplace=True)

My Code:

What I expected to happend:

KeyErrorTraceback (most recent call last)
/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2524 try:
→ 2525 return self._engine.get_loc(key)
2526 except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ‘Time’

During handling of the above exception, another exception occurred:

KeyErrorTraceback (most recent call last)
in ()
2 ‘Period\Unit:’: ‘Time’},
3 inplace=True)
----> 4 exchange_rates[‘Time’] = pd.to_datetime(exchange_rates[‘Time’])
5 exchange_rates.sort_values(‘Time’, ignore_index=True,
6 inplace=True)

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/frame.py in getitem(self, key)
2137 return self._getitem_multilevel(key)
2138 else:
→ 2139 return self._getitem_column(key)
2140
2141 def _getitem_column(self, key):

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/frame.py in _getitem_column(self, key)
2144 # get column
2145 if self.columns.is_unique:
→ 2146 return self._get_item_cache(key)
2147
2148 # duplicate columns & possible reduce dimensionality

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
1840 res = cache.get(item)
1841 if res is None:
→ 1842 values = self._data.get(item)
1843 res = self._box_item_values(item, values)
1844 cache[item] = res

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/internals.py in get(self, item, fastpath)
3841
3842 if not isna(item):
→ 3843 loc = self.items.get_loc(item)
3844 else:
3845 indexer = np.arange(len(self.items))[isna(self.items)]

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2525 return self._engine.get_loc(key)
2526 except KeyError:
→ 2527 return self._engine.get_loc(self._maybe_cast_indexer(key))
2528
2529 indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ‘Time’

Basics.ipynb (32.2 KB)

Click here to view the jupyter notebook file in a new tab

Hi @mahmutagdas, the problem originates in how you read in the CSV file:

exchange_rates = pd.read_csv('euro-daily-hist_1999_2020.csv', sep = ';')

The CSV file is comma separated, but you used sep=';', which failed to read in the data set properly – see how weird the table looks:

Consequently, there’s no Time columns in your DataFrame, hence the KeyError.

1 Like