为Python中的最后一个元素编制索引时出错
我正在尝试访问Pandas系列中的最后一个元素(datetime)。下面我将显示类型以供澄清。我是Python新手,所以我道歉为Python中的最后一个元素编制索引时出错,python,pandas,indexing,Python,Pandas,Indexing,我正在尝试访问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
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)
1069
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()
KeyError: -1
keyrerror回溯(最近一次调用)
在里面
---->1东区时报[-1]
~\Anaconda3\lib\site packages\pandas\core\series.py in\uuuuu getitem\uuuuuuuuuu(self,key)
1066 key=com.apply\u如果可调用(key,self)
1067尝试:
->1068结果=self.index.get_值(self,key)
1069
1070如果不是标量(结果):
获取值中的~\Anaconda3\lib\site packages\pandas\core\index\base.py(self、series、key)
4728 k=self.\u convert\u scalar\u indexer(k,kind=“getitem”)
4729尝试:
->4730返回self._engine.get_值(s,k,tz=getattr(series.dtype,“tz”,None))
4731除键错误为e1外:
4732如果len(self)>0且(self.holds_integer()或self.is_boolean()):
pandas\\u libs\index.pyx在pandas.\u libs.index.IndexEngine.get\u value()中
pandas\\u libs\index.pyx在pandas.\u libs.index.IndexEngine.get\u value()中
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.Int64HashTable.get\u item()中
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.Int64HashTable.get\u item()中
键错误:-1
您想在这里使用iloc
来获取基于行的索引
East.times.iloc[-1]