尝试在 Jupyter Notebook 上使用 Pandas 从现有列创建新列时出现 NoneType 错误
NoneType Error when trying to create new column from existing columns with Pandas on Jupyter Notebook
所以我最近尝试开始使用 Jupyter 笔记本,因为我发现它们比我在代码文件中保留冗长的注释方便得多。
据说是为了测试基本功能,我想模拟移动平均线。但是,正如标题所说,我什至无法使用 Pandas 索引方法(在其他地方对我有用)创建新列。
这是我使用的代码:
import pandas as pd
from pandas_datareader import data as pdr
import matplotlib.pyplot as plt
from datetime import datetime
%matplotlib inline
fb = pdr.DataReader("FB","yahoo",datetime(2012,5,12),datetime(2020,5,25))
fb['MA10'] = fb['Close'].rolling(10).mean()
最后一行是产生错误(TypeError: 'NoneType' object is not iterable
)的原因,该错误源于我调用 fb['MA10']
,因为我没有 运行 遇到任何问题 运行代码的右侧。我很困惑,非常感谢任何反馈,我已经在下面发布了完整的错误回溯,供感兴趣的人使用。
编辑
我只是因为输入 fb['MA10'] = fb['Close']
而收到错误,而只是 fb['Close']=fb['Open']
不会产生问题,因为它们都是现有列,但是我不想每次都手动创建一个新列。
--------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-55-fa34c8084387> in <module>
1 fb = pdr.DataReader("FB","yahoo",datetime(2012,5,12),datetime(2020,5,25))
----> 2 type(fb['MA10'])
c:\users\robjr\appdata\local\programs\python\python38\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2777
2778 # Do we have a slicer (on rows)?
-> 2779 indexer = convert_to_index_sliceable(self, key)
2780 if indexer is not None:
2781 # either we have a slice or we have a string that can be converted
c:\users\robjr\appdata\local\programs\python\python38\lib\site-packages\pandas\core\indexing.py in convert_to_index_sliceable(obj, key)
2276 if idx._supports_partial_string_indexing:
2277 try:
-> 2278 return idx._get_string_slice(key)
2279 except (KeyError, ValueError, NotImplementedError):
2280 return None
c:\users\robjr\appdata\local\programs\python\python38\lib\site-packages\pandas\core\indexes\datetimes.py in _get_string_slice(self, key, use_lhs, use_rhs)
776 def _get_string_slice(self, key: str, use_lhs: bool = True, use_rhs: bool = True):
777 freq = getattr(self, "freqstr", getattr(self, "inferred_freq", None))
--> 778 _, parsed, reso = parsing.parse_time_string(key, freq)
779 loc = self._partial_date_slice(reso, parsed, use_lhs=use_lhs, use_rhs=use_rhs)
780 return loc
pandas\_libs\tslibs\parsing.pyx in pandas._libs.tslibs.parsing.parse_time_string()
pandas\_libs\tslibs\parsing.pyx in pandas._libs.tslibs.parsing.parse_datetime_string_with_reso()
pandas\_libs\tslibs\parsing.pyx in pandas._libs.tslibs.parsing.dateutil_parse()
TypeError: 'NoneType' object is not iterable
您需要处理缺失值,试试
fb['MA10'] = fb['Close'].fillna(0).rolling(10).mean()
所以我最近尝试开始使用 Jupyter 笔记本,因为我发现它们比我在代码文件中保留冗长的注释方便得多。
据说是为了测试基本功能,我想模拟移动平均线。但是,正如标题所说,我什至无法使用 Pandas 索引方法(在其他地方对我有用)创建新列。
这是我使用的代码:
import pandas as pd
from pandas_datareader import data as pdr
import matplotlib.pyplot as plt
from datetime import datetime
%matplotlib inline
fb = pdr.DataReader("FB","yahoo",datetime(2012,5,12),datetime(2020,5,25))
fb['MA10'] = fb['Close'].rolling(10).mean()
最后一行是产生错误(TypeError: 'NoneType' object is not iterable
)的原因,该错误源于我调用 fb['MA10']
,因为我没有 运行 遇到任何问题 运行代码的右侧。我很困惑,非常感谢任何反馈,我已经在下面发布了完整的错误回溯,供感兴趣的人使用。
编辑
我只是因为输入 fb['MA10'] = fb['Close']
而收到错误,而只是 fb['Close']=fb['Open']
不会产生问题,因为它们都是现有列,但是我不想每次都手动创建一个新列。
--------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-55-fa34c8084387> in <module>
1 fb = pdr.DataReader("FB","yahoo",datetime(2012,5,12),datetime(2020,5,25))
----> 2 type(fb['MA10'])
c:\users\robjr\appdata\local\programs\python\python38\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2777
2778 # Do we have a slicer (on rows)?
-> 2779 indexer = convert_to_index_sliceable(self, key)
2780 if indexer is not None:
2781 # either we have a slice or we have a string that can be converted
c:\users\robjr\appdata\local\programs\python\python38\lib\site-packages\pandas\core\indexing.py in convert_to_index_sliceable(obj, key)
2276 if idx._supports_partial_string_indexing:
2277 try:
-> 2278 return idx._get_string_slice(key)
2279 except (KeyError, ValueError, NotImplementedError):
2280 return None
c:\users\robjr\appdata\local\programs\python\python38\lib\site-packages\pandas\core\indexes\datetimes.py in _get_string_slice(self, key, use_lhs, use_rhs)
776 def _get_string_slice(self, key: str, use_lhs: bool = True, use_rhs: bool = True):
777 freq = getattr(self, "freqstr", getattr(self, "inferred_freq", None))
--> 778 _, parsed, reso = parsing.parse_time_string(key, freq)
779 loc = self._partial_date_slice(reso, parsed, use_lhs=use_lhs, use_rhs=use_rhs)
780 return loc
pandas\_libs\tslibs\parsing.pyx in pandas._libs.tslibs.parsing.parse_time_string()
pandas\_libs\tslibs\parsing.pyx in pandas._libs.tslibs.parsing.parse_datetime_string_with_reso()
pandas\_libs\tslibs\parsing.pyx in pandas._libs.tslibs.parsing.dateutil_parse()
TypeError: 'NoneType' object is not iterable
您需要处理缺失值,试试
fb['MA10'] = fb['Close'].fillna(0).rolling(10).mean()