0
votes

I have a csv file that has 7 columns ['Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume'] The thing is I tried to set a datetime index but it does not work may be because date and time are two separate columns.

Here is the code:

import pandas as pd

column_names = ['Date', 'Time', 'Open', 'High', 'Low','Close', 'Volume']

df = pd.read_csv(r"E:\Tutorial\EURUSD60.csv", header=None, names=column_names)

df['DateTime'] = pd.to_datetime(df['Date', 'Time'])

print(df.head())

Here is the error:

C:\Users\sydgo\Anaconda3\python.exe E:/Tutorial/language.py Traceback (most recent call last): File "C:\Users\sydgo\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2442, in get_loc return self._engine.get_loc(key) File "pandas_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: ('Date', 'Time')

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "E:/Tutorial/language.py", line 7, in df['DateTime'] = pd.to_datetime(df['Date', 'Time']) File "C:\Users\sydgo\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1964, in getitem return self._getitem_column(key) File "C:\Users\sydgo\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1971, in _getitem_column return self._get_item_cache(key) File "C:\Users\sydgo\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1645, in _get_item_cache values = self._data.get(item) File "C:\Users\sydgo\Anaconda3\lib\site-packages\pandas\core\internals.py", line 3590, in get loc = self.items.get_loc(item) File "C:\Users\sydgo\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2444, in get_loc return self._engine.get_loc(self._maybe_cast_indexer(key)) File "pandas_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: ('Date', 'Time')

1

1 Answers

1
votes

If you simplify your code, you'll see the error is right here:

df['Date', 'Time']

That's because you are indexing into the DataFrame once by two strings, but you want to index into it twice, by each of two strings. That is:

df[['Date', 'Time']]

Still, this may fail, because to_datetime expects strings, not pairs of strings:

pd.to_datetime(df['Date', 'Time'])

In which case try this:

pd.to_datetime(df.Date + ' ' + df.Time)