2

I defined a function z. It works when I pass a list, but when I pass a series(even after converting to list) it returns wrong answers. My input argument to the function z has to be a series. How to resolve this?

list1 = [np.nan, 14975, 98121] series1 = pd.Series([np.nan,14975,98121]) z(series1.tolist()) ['0', '0', '0'] z(list1) ['0', '1', '98121'] 

My z function is,

def z(each): zipcode_list = [] for i in each: try: if zipcodes.is_real(str(i)): zip_code = str(i) else: zip_code = str(1) except: zip_code = str(0) zipcode_list.append(zip_code) return zipcodes 
4
  • they all return zeros to me, there's no function is_real for lists, so they all should evaluate to zip_code = str(0) Commented Aug 1, 2019 at 21:50
  • Did you try importing 'zipcodes' module, without that it will print the exception always. Commented Aug 1, 2019 at 21:54
  • there's no zipcodes module on the anaconda base envirronment, if there's one import called zipcodes in your code then rename your variable zipcodes inside you z function Commented Aug 1, 2019 at 21:56
  • Thanks, changed it Commented Aug 1, 2019 at 21:57

1 Answer 1

1

Although pandas does correctly return a list (see https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.tolist.html) they take the safe route and turn the values in the Series to floats and not ints. Thus when you try to get the zipcode of a float, it errors.

You can see this by running the following:

import pandas as pd import numpy as np import zipcodes list1 = [np.nan, 14975, 98121] series1 = pd.Series([np.nan,14975,98121]) def z(each): zipcode_list = [] for i in each: print(i, type(i)) try: if zipcodes.is_real(str(i)): zip_code = str(i) else: zip_code = str(1) except Exception: zip_code = str(0) zipcode_list.append(zip_code) return zipcode_list print(z(series1.tolist())) print(z(list1)) 

Output:

nan <class 'float'> 14975.0 <class 'float'> import pandas as pd 98121.0 <class 'float'> ['0', '0', '0'] nan <class 'float'> 14975 <class 'int'> 98121 <class 'int'> ['0', '1', '98121'] 

Changing the code to convert the list to ints before passing it into z will fix your problem. See:

import pandas as pd import numpy as np import zipcodes list1 = [np.nan, 14975, 98121] series1 = pd.Series([np.nan,14975,98121]) def z(each): zipcode_list = [] for i in each: try: if zipcodes.is_real(str(int(i))): zip_code = str(int(i)) else: zip_code = str(1) except Exception: zip_code = str(0) zipcode_list.append(zip_code) return zipcode_list print(z(series1.tolist())) # ['0', '1', '98121'] print(z(list1)) # ['0', '1', '98121'] 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but the 'zipcode' is a module that has is_real() which returns true or false. Without that module it will always print the exception.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.