gpt4 book ai didi

python - Pandas 数据框中的 numpy datetime64

转载 作者:太空宇宙 更新时间:2023-11-04 00:44:14 29 4
gpt4 key购买 nike

我有一个由 3 列组成的 pandas DataFrame,第一列是 dtype 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

如果我print(insolation) 我得到这个输出:

                   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')

(它似乎确实存在于 DataFrame 中)以引用 DataFrame:

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

最佳答案

使用loc

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

出了什么问题?

insolation[datetime]

尝试获取由变量 datetime 定义的列。
提示:不要使用与您的列名称相同的变量名称。

关于python - Pandas 数据框中的 numpy datetime64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40501217/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com