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')