LIMITED TIME OFFER: 50% OFF OF PREMIUM WITH OUR ANNUAL PLAN (THAT'S $294 IN SAVINGS).
GET OFFER

Code is identical to solution but getting error- Cleaning and Preparing Data in Python, Part 8

Screen Link:

My Code:

test_data = ["1912", "1929", "1913-1923",
             "(1951)", "1994", "1934",
             "c. 1915", "1995", "c. 1912",
             "(1988)", "2002", "1957-1959",
             "c. 1955.", "c. 1970's", 
             "C. 1990-1999"]

bad_chars = ["(",")","c","C",".","s","'", " "]

def strip_characters(string):
    for char in bad_chars:
        string = string.replace(char,"")
    return string

stripped_test_data = ['1912', '1929', '1913-1923',
                      '1951', '1994', '1934',
                      '1915', '1995', '1912',
                      '1988', '2002', '1957-1959',
                      '1955', '1970', '1990-1999']

def process_date(date):
    if "-" in date:
        split_date = date.split("-")
        date_one = split_date[0]
        date_two = split_date[1]
        date = (int(date_one) + int(date_two)) / 2
        date = round(date)
    else:
        date = int(date)
    return date
        
processed_test_data = []
for x in stripped_test_data:
    data = process_date(x)
    processed_test_data.append(data)
    
tested_data = process_date(test_data)
print(tested_data)

for row in moma:
    date = row[6]
    date = strip_characters(date)
    date = process_date(date)
    row[6] = date

What I expected to happen:
My code is identical to the solution and I am getting the below error message. The error is on the else portion of the function. Any suggestions on what I’m doing wrong? I already restored to the initial code and pasted my code back into the window and that didn’t work.

What actually happened:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-b44c0fd16fbc> in <module>
     90     processed_test_data.append(data)
     91 
---> 92 tested_data = process_date(test_data)
     93 print(tested_data)
     94 

<ipython-input-1-b44c0fd16fbc> in process_date(date)
     82         # date = round(date)
     83     else:
---> 84         date = int(date)
     85     return date
     86 

TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'```

<!--Enter other details below: -->

Hi @anonymous10

Here in this line below, you are giving the whole list as the input to the function.

So in the function defined as def process_date(date), you are supplying the whole list in the place of date. That is why you’re getting the error. The function is written to handle one string at a time like you have done here.

for x in stripped_test_data:
    data = process_date(x)
    processed_test_data.append(data)

I hope you will be able to rectify the error now.

1 Like

Hi @jithins123,

I do get it. So what would be the alternative function handling the list in this case?