我想找出与第一行的时差,其中包括日期和时间,并以天数为单位返回。我已经编码了以下内容,但是收到了随后显示的错误。我无法理解KeyError消息。最初,当我运行代码时没有问题,但后来我不断收到该错误消息。请帮忙。
数据帧示例:
Date Time R1 R2 R3
0 2018-10-16 11:28:31 -3.580467 -6.799278 -3.960213
1 2018-10-16 11:43:31 -4.339966 -6.799278 -3.924049
2 2018-10-16 11:58:31 -3.399643 -6.184448 -3.887886
3 2018-10-16 12:13:31 -4.339966 -6.799278 -3.851715
4 2018-10-16 12:28:31 -3.725136 -6.726944 -6.003616
5 2018-10-16 12:43:31 -4.954788 -6.799278 -7.052437
6 2018-10-16 12:58:31 -3.851715 -6.184448 -7.016281
7 2018-10-16 13:13:31 -0.632912 -4.339966 -5.660034
8 2018-10-16 13:28:31 -0.018082 -3.743217 -5.587700
9 2018-10-16 13:43:31 -0.632912 -3.743217 -5.551537
代码:
import pandas as pd
tmp = pd.read_csv (' TMP .csv.') #sheet_name = 'TMP')
tmp ['Date'] = pd.to_datetime (tmp['Date'])
tmp ['Time'] = pd.to_datetime (tmp ['Time'],format = '%H:%M:%S').dt.time
tmp ['Timestamp'] =tmp.apply(lambda tmp: pd.datetime.combine(tmp['Date'], tmp['Time'],0))
initial = tmp ['Timestamp'][0]
for i in tmp ['Timestamp']:
tmp ['Time_Diff'] = tmp ['Timestamp'] - initial
tmp ['seconds'] = tmp ['Time_Diff'].dt.total_seconds()
返回:
runfile('C:/Users/Jamie/Documents/Python Scripts/Time_conversion.py', wdir='C:/Users/Jamie/Documents/Python Scripts')
Traceback (most recent call last):
File "<ipython-input-208-44b791ee4ee7>", line 1, in <module>
runfile('C:/Users/Jamie/Documents/Python Scripts/Time_conversion.py', wdir='C:/Users/Jamie/Documents/Python Scripts')
File "C:\Users\Jamie\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
execfile(filename, namespace)
File "C:\Users\Jamie\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Jamie/Documents/Python Scripts/Time_conversion.py", line 14, in <module>
tmp ['Timestamp'] =tmp.apply(lambda tmp: pd.datetime.combine(tmp['Date'], tmp['Time'],0))
File "C:\Users\Jamie\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py", line 6014, in apply
return op.get_result()
File "C:\Users\Jamie\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\apply.py", line 318, in get_result
return super(FrameRowApply, self).get_result()
File "C:\Users\Jamie\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\apply.py", line 142, in get_result
return self.apply_standard()
File "C:\Users\Jamie\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\apply.py", line 248, in apply_standard
self.apply_series_generator()
File "C:\Users\Jamie\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\apply.py", line 277, in apply_series_generator
results[i] = self.f(v)
File "C:/Users/Jamie/Documents/Python Scripts/Time_conversion.py", line 14, in <lambda>
tmp ['Timestamp'] =tmp.apply(lambda tmp: pd.datetime.combine(tmp['Date'], tmp['Time'],0))
File "C:\Users\Jamie\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py", line 767, in __getitem__
result = self.index.get_value(self, key)
File "C:\Users\Jamie\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 3118, in get_value
tz=getattr(series.dtype, 'tz', None))
File "pandas\_libs\index.pyx", line 106, in pandas._libs.index.IndexEngine.get_value
File "pandas\_libs\index.pyx", line 114, in pandas._libs.index.IndexEngine.get_value
File "pandas\_libs\index.pyx", line 164, in pandas._libs.index.IndexEngine.get_loc
KeyError: ('Date', 'occurred at index Date')