Search code examples
pythonpandastypeerroriterablenonetype

NoneType Error when trying to create new column from existing columns with Pandas on Jupyter Notebook


so I recently tried to start using Jupyter notebooks, as I find they are far more convenient than me keeping lengthy comments in my code files.

That being said to test out basic functionality I wanted to simulate moving averages. However, as the title says, I was unable to even create a new column using Pandas indexing method (which has worked elsewhere for me).

Here is the code I used:

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

That last line, is what generates the error ( TypeError: 'NoneType' object is not iterable) which originates from me calling fb['MA10'] since I did not run into any problems running the right hand side of the code. I am pretty stumped and would appreciate any feedback, I've posted the full Error Traceback below for whoever is interested.

EDIT I get an error just for typing fb['MA10'] = fb['Close'] whereas just fb['Close']=fb['Open'] does not yield a problem as both are existing columns, however I don't want to manually create a new column every time.

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

Solution

  • you need to handle your missing values, try

    fb['MA10'] = fb['Close'].fillna(0).rolling(10).mean()