#执行以下代码没有问题
ts_data_load= pd.read_csv(os.path.join(data_dir, "energy.csv"), parse_dates=["timestamp"] )
# Reindex the dataframe such that the dataframe has a record for every time point
# between the minimum and maximum timestamp in the time series. This helps to
# identify missing time periods in the data (there are none in this dataset).
ts_data_load.index = ts_data_load["timestamp"]
ts_data_load = ts_data_load .reindex( pd.date_range(min(energy["timestamp"]), max(energy["timestamp"]), freq="H") )
ts_data_load= ts_data_load .drop("timestamp", axis=1)
ts_data_load = load_data(data_dir)[["load"]]
ts_data_load.head()
#输出结果
load
2012-01-01 00:00:00 2,698.00
2012-01-01 01:00:00 2,558.00
2012-01-01 02:00:00 2,444.00
2012-01-01 03:00:00 2,402.00
2012-01-01 04:00:00 2,403.00
#执行下面这句代码,输出正常
ts_data_load.index
#输出结果
DatetimeIndex(['2012-01-01 00:00:00', '2012-01-01 01:00:00',
'2012-01-01 02:00:00', '2012-01-01 03:00:00',
'2012-01-01 04:00:00', '2012-01-01 05:00:00',
'2012-01-01 06:00:00', '2012-01-01 07:00:00',
'2012-01-01 08:00:00', '2012-01-01 09:00:00',
...
'2014-12-31 14:00:00', '2014-12-31 15:00:00',
'2014-12-31 16:00:00', '2014-12-31 17:00:00',
'2014-12-31 18:00:00', '2014-12-31 19:00:00',
'2014-12-31 20:00:00', '2014-12-31 21:00:00',
'2014-12-31 22:00:00', '2014-12-31 23:00:00'],
dtype='datetime64[ns]', length=26304, freq='H')
#如果执行下面这句代码,就报错
ts_data_load['2012-06-01']
#报错,请问这是什么原因??谢谢
KeyError Traceback (most recent call last)
File d:\ProgramData\anaconda3\Lib\site-packages\pandas\core\indexes\base.py:3653, in Index.get_loc(self, key)
3652 try:
-> 3653 return self._engine.get_loc(casted_key)
3654 except KeyError as err:
File d:\ProgramData\anaconda3\Lib\site-packages\pandas_libs\index.pyx:147, in pandas._libs.index.IndexEngine.get_loc()
File d:\ProgramData\anaconda3\Lib\site-packages\pandas_libs\index.pyx:176, in pandas._libs.index.IndexEngine.get_loc()
File pandas_libs\hashtable_class_helper.pxi:7080, in pandas._libs.hashtable.PyObjectHashTable.get_item()
File pandas_libs\hashtable_class_helper.pxi:7088, in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: '20120601'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
Cell In[33], line 1
----> 1 ts_data_load['20120601']
File d:\ProgramData\anaconda3\Lib\site-packages\pandas\core\frame.py:3761, in DataFrame.getitem(self, key)
3759 if self.columns.nlevels > 1:
3760 return self._getitem_multilevel(key)
-> 3761 indexer = self.columns.get_loc(key)
3762 if is_integer(indexer):
3763 indexer = [indexer]
File d:\ProgramData\anaconda3\Lib\site-packages\pandas\core\indexes\base.py:3655, in Index.get_loc(self, key)
3653 return self._engine.get_loc(casted_key)
3654 except KeyError as err:
-> 3655 raise KeyError(key) from err
3656 except TypeError:
3657 # If we have a listlike key, _check_indexing_error will raise
3658 # InvalidIndexError. Otherwise we fall through and re-raise
3659 # the TypeError.
3660 self._check_indexing_error(key)
KeyError: '20120601'