Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I'm trying to change a function that earlier filled missing years as timestamps (the data-type is 'int' now), to one that would treat the missing years as int , and replace them by fillna().


What I have tried:

def nons(row):
    for i in row[['name','year_of_release']]:
        if row['name']=="LEGO Harry Potter: Years 5-7":
            row['year_of_release'].astype(int).fillna(2012)
        else:
            continue
    return row['year_of_release']

gaming['year_of_release']=gaming.apply(nons,axis=1)

<pre>---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10020/2176792990.py in <module>
      7     return row['year_of_release']
      8 
----> 9 gaming['year_of_release']=gaming.apply(nons,axis=1)

~\anaconda3\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, raw, result_type, args, **kwargs)
   8738             kwargs=kwargs,
   8739         )
-> 8740         return op.apply()
   8741 
   8742     def applymap(

~\anaconda3\lib\site-packages\pandas\core\apply.py in apply(self)
    686             return self.apply_raw()
    687 
--> 688         return self.apply_standard()
    689 
    690     def agg(self):

~\anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self)
    810 
    811     def apply_standard(self):
--> 812         results, res_index = self.apply_series_generator()
    813 
    814         # wrap results

~\anaconda3\lib\site-packages\pandas\core\apply.py in apply_series_generator(self)
    826             for i, v in enumerate(series_gen):
    827                 # ignore SettingWithCopy here in case the user mutates
--> 828                 results[i] = self.f(v)
    829                 if isinstance(results[i], ABCSeries):
    830                     # If we have a view on v, we need to make a copy because

~\AppData\Local\Temp/ipykernel_10020/2176792990.py in nons(row)
      2     for i in row[['name','year_of_release']]:
      3         if row['name']=="LEGO Harry Potter: Years 5-7":
----> 4             row['year_of_release'].astype(int).fillna(2012)
      5         else:
      6             continue

AttributeError: 'NAType' object has no attribute 'astype'


Can i please get a little hand here ? Thank you!
Posted
Comments
Richard MacCutchan 11-Jul-22 4:39am    
The message suggest that row['year_of_release'] is not a valid reference to an object.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900