我正试图访问我的Pandas系列中的最后一个元素(datetime)。下面我展示了类型,以示澄清。我是Python的新手,所以我很抱歉。
type(East.times)
pandas.core.series.Series
我能够通过做East.times[0]来索引第一个元素,其中的输出是
Timestamp('2017-09-01 00:00:00')
作为参考,East.times输出
0 2017-09-01 00:00:00
1 2017-09-01 00:05:00
2 2017-09-01 00:10:00
3 2017-09-01 00:15:00
4 2017-09-01 00:20:00
8635 2017-09-30 23:35:00
8636 2017-09-30 23:40:00
8637 2017-09-30 23:45:00
8638 2017-09-30 23:50:00
8639 2017-09-30 23:55:00
Name: times, Length: 8640, dtype: datetime64[ns]
我想通过做以下事情来索引最后一个元素
East.times[-1]
然而,我得到以下错误,我不明白为什么它不能工作。
KeyError Traceback (most recent call last)
<ipython-input-37-7203880275dc> in <module>
----> 1 East.times[-1]
~\Anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
1066 key = com.apply_if_callable(key, self)
1067 try:
-> 1068 result = self.index.get_value(self, key)
1070 if not is_scalar(result):
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
4728 k = self._convert_scalar_indexer(k, kind="getitem")
4729 try:
-> 4730 return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
4731 except KeyError as e1:
4732 if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()