Python Forum
Read CSV error: python KeyError: 'Time'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read CSV error: python KeyError: 'Time'
#1
have the following csv structure file. Time 1 2 3 01/01/2010 00:00 274,16 576,44 1523,9

I tried to convert time to timestamp but I cannot plot the data as I get the following error:

Error:
File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'Time'
data=pd.read_csv('test.csv', delimiter='delimiter', header='infer')
data.head()
data['Timestamp']=pd.to_datetime(data['Time'])
plt.figure(num=1, figsize=(10,5))
data.Timestamp.plot() #Plotting the newly added timestamp column
plt.xlabel('Reading Count')
plt.ylabel('Date')
plt.show()
I am wondering if anybody could help me to plot some data from the excel source provided in the following Github link: https://github.com/charlicruz/Energy-Demand
Reply
#2
Please here is attached the error:
1 #Converting time to timestamp ( a datetime object)
----> 2 data['Timestamp']=pd.to_datetime(data['Time'])
3
4 # Ensuring there are no temporal gaps

---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~/opt/anaconda3/envs/python3b/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2645 try:
-> 2646 return self._engine.get_loc(key)
2647 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/index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type()

KeyError: 'Time'

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last)
<ipython-input-9-9b4c885821b2> in <module>
1 #Converting time to timestamp ( a datetime object)
----> 2 data['Timestamp']=pd.to_datetime(data['Time'])
3
4 # Ensuring there are no temporal gaps
5 plt.figure(num=1, figsize=(10,5))

~/opt/anaconda3/envs/python3b/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
2798 if self.columns.nlevels > 1:
2799 return self._getitem_multilevel(key)
-> 2800 indexer = self.columns.get_loc(key)
2801 if is_integer(indexer):
2802 indexer = [indexer]

~/opt/anaconda3/envs/python3b/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2646 return self._engine.get_loc(key)
2647 except KeyError:
-> 2648 return self._engine.get_loc(self._maybe_cast_indexer(key))
2649 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
2650 if indexer.ndim > 1 or indexer.size > 1:

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

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

pandas/_libs/index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type()

KeyError: 'Time'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting a date time string read from a csv file DosAtPython 5 1,253 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  Plotting by Time, error mansoorahs 1 727 May-16-2023, 09:46 AM
Last Post: Larz60+
  Read nested data from JSON - Getting an error marlonbown 5 1,358 Nov-23-2022, 03:51 PM
Last Post: snippsat
  How to change UTC time to local time in Python DataFrame? SamKnight 2 1,600 Jul-28-2022, 08:23 AM
Last Post: Pedroski55
  How to read rainfall time series and insert missing data points MadsM 4 2,168 Jan-06-2022, 10:39 AM
Last Post: amdi40
  Error When Trying To Read CSV File kaega2 3 5,527 Dec-03-2021, 09:10 PM
Last Post: kaega2
  Is there a way to read the time between clicks? penahuse 1 2,491 Jan-30-2021, 10:46 PM
Last Post: Larz60+
  how to change the range of read CSV file every time python file runs greenpine 6 4,460 Dec-08-2020, 10:11 PM
Last Post: greenpine
  Time Limit Exceeded error loves 5 3,143 Dec-03-2020, 07:15 AM
Last Post: Sofia_Grace
  How to read CSV file one row at the time in a range and some delay in between greenpine 2 4,740 Nov-20-2020, 02:26 PM
Last Post: greenpine

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020