Python 数据帧中的numpy datetime64

Python 数据帧中的numpy datetime64,python,datetime,pandas,Python,Datetime,Pandas,我有一个由3列组成的pandas数据帧,第一列是每小时一次的时间序列,数据类型为np.datetime64 import numpy as np import pandas as pd timeseries = np.arange(np.datetime64('20/11/2009'), np.datetime64('19/11/2010), np.timedelta64(1, 'h')) insolation =pd.DataFrame(columns=['datetime','GHI

我有一个由3列组成的
pandas数据帧
,第一列是每小时一次的时间序列,数据类型为
np.datetime64

import numpy as np
import pandas as pd    
timeseries = np.arange(np.datetime64('20/11/2009'), np.datetime64('19/11/2010), np.timedelta64(1, 'h'))
insolation =pd.DataFrame(columns=['datetime','GHI','DNI'])
insolation['datetime'] = timeseries
如果我
打印(日晒)
我得到以下输出:

                   datetime  GHI  DNI
0    2009-11-20 00:00:00  NaN  NaN
1    2009-11-20 01:00:00  NaN  NaN
2    2009-11-20 02:00:00  NaN  NaN
...                  ...  ...  ...
8734 2010-11-18 22:00:00  NaN  NaN
8735 2010-11-18 23:00:00  NaN  NaN
到目前为止,一切顺利。但是如果我使用这个datetime64对象:

datetime=np.datetime64('2009-11-20 00:00:00')
(似乎存在于数据帧中)引用数据帧:

insolation[datetime][GHI] =1.0
我得到一个错误:

KeyError: numpy.datetime64('2009-11-20T00:00:00')
完整的错误消息如下:

Traceback (most recent call last):
  File "C:\Users\z5044992\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\indexes\base.py", line 1945, in get_loc
    return self._engine.get_loc(key)
  File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)
  File "pandas\index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)
  File "pandas\hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12368)
  File "pandas\hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12322)
KeyError: numpy.datetime64('2009-11-20T00:00:00')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/PYTHONprojects/PY_EN_Models/Scripts/create_weatherfile.py", line 109, in <module>
    main()
  File "C:/PYTHONprojects/PY_EN_Models/Scripts/create_weatherfile.py", line 87, in main
    insolation[datetime][GHI] = irradience_array[gridrow,gridcol]
  File "C:\Users\z5044992\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1997, in __getitem__
    return self._getitem_column(key)
  File "C:\Users\z5044992\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2004, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Users\z5044992\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1350, in _get_item_cache
    values = self._data.get(item)
  File "C:\Users\z5044992\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\internals.py", line 3290, in get
    loc = self.items.get_loc(item)
  File "C:\Users\z5044992\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\indexes\base.py", line 1947, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)
  File "pandas\index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)
  File "pandas\hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12368)
  File "pandas\hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12322)
KeyError: numpy.datetime64('2009-11-20T00:00:00')

Process finished with exit code 1
回溯(最近一次呼叫最后一次):
文件“C:\Users\z5044992\AppData\Local\Continuum\Anaconda3\lib\site packages\pandas\index\base.py”,第1945行,在get\u loc中
返回发动机。获取位置(钥匙)
文件“pandas\index.pyx”,第137行,在pandas.index.IndexEngine.get_loc(pandas\index.c:4154)中
pandas.index.IndexEngine.get_loc(pandas\index.c:4018)中第159行的文件“pandas\index.pyx”
pandas.hashtable.PyObjectHashTable.get_项(pandas\hashtable.c:12368)中第675行的文件“pandas\hashtable.pyx”
pandas.hashtable.PyObjectHashTable.get_项(pandas\hashtable.c:12322)中第683行的文件“pandas\hashtable.pyx”
KeyError:numpy.datetime64('2009-11-20T00:00:00')
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“C:/PYTHONprojects/PY_EN_Models/Scripts/create_weatherfile.PY”,第109行,在
main()
文件“C:/PYTHONprojects/PY_EN_Models/Scripts/create_weatherfile.PY”,主目录第87行
日照[datetime][GHI]=辐照度_数组[gridrow,gridcol]
文件“C:\Users\z5044992\AppData\Local\Continuum\Anaconda3\lib\site packages\pandas\core\frame.py”,第1997行,在u getitem中__
返回self.\u getitem\u列(键)
文件“C:\Users\z5044992\AppData\Local\Continuum\Anaconda3\lib\site packages\pandas\core\frame.py”,第2004行,在_getitem_列中
返回self.\u获取\u项目\u缓存(密钥)
文件“C:\Users\z5044992\AppData\Local\Continuum\Anaconda3\lib\site packages\pandas\core\generic.py”,第1350行,在\u get\u item\u缓存中
values=self.\u data.get(项目)
get中的文件“C:\Users\z5044992\AppData\Local\Continuum\Anaconda3\lib\site packages\pandas\core\internals.py”,第3290行
loc=自身项目。获取loc(项目)
文件“C:\Users\z5044992\AppData\Local\Continuum\Anaconda3\lib\site packages\pandas\index\base.py”,第1947行,在get\u loc中
返回self.\u引擎。获取\u loc(self.\u可能\u cast\u索引器(键))
文件“pandas\index.pyx”,第137行,在pandas.index.IndexEngine.get_loc(pandas\index.c:4154)中
pandas.index.IndexEngine.get_loc(pandas\index.c:4018)中第159行的文件“pandas\index.pyx”
pandas.hashtable.PyObjectHashTable.get_项(pandas\hashtable.c:12368)中第675行的文件“pandas\hashtable.pyx”
pandas.hashtable.PyObjectHashTable.get_项(pandas\hashtable.c:12322)中第683行的文件“pandas\hashtable.pyx”
KeyError:numpy.datetime64('2009-11-20T00:00:00')
进程已完成,退出代码为1
使用
loc

insolation.loc[insolation.datetime.eq(datetime), 'GHI'] = 1.0

出了什么问题

insolation[datetime]
尝试获取由变量
datetime
定义的列
提示:不要使用与列名相同的变量名