简体   繁体   中英

Why am I getting a 'NoneType' type error on data that is not a 'NoneType'?

I have a series of functions that take a pandas dataframe, cleanse the data and then send it to sqlite db.

The following function is creating the error:

def send_to_db(df, event_name):

    print df.describe()

    table_names = {
        'Video Played': 'video_played',
        'Item Information Click': 'item_info_click',
        'Faved': 'faved',
        'Add to Cart': 'add_to_cart',
        'Tap to Replay': 'replay'
    }

    print table_names.get(event_name)

    con = db.connect('/Users/metersky/code/mikmak/vid_score/test.db')
    df.to_sql(table_names.get(event_name), con, flavor='sqlite', if_exists='append')
    con.close()

The error I get is TypeError: 'NoneType' object is not iterable

This is weird to me for two reasons:

1) print df.describe() gives me the proper pandas output, meaning the data is not None at that point in the function

2) The data gets sent to the sqlite database, which I verified. So this mean that the data isn't None there either.

Why am I getting this error and when is my data turning into None ?


Traceback:

Traceback (most recent call last):
  File "fetch_data.py", line 139, in <module>
    df, event_name = send_to_db(df, event_name)
TypeError: 'NoneType' object is not iterable

This is how I call the functions:

for event in event_types:
    print event
    df, event_name = get_data(start_date, end_date, event)

    print "*********" + event_name + " data retrieved"

    df, event_name = data_cleanse(df, event_name)

    print  "*********" + event_name + " data cleansed"

    df, event_name = send_to_db(df, event_name)

    print  "*********" + event_name + " data sent to db"

You are not returning anything from your function but are expecting a tuple of 2-items (df, event_name) :

When you call your function:

df, event_name = send_to_db(df, event_name)

You are expecting to get back a tuple of 2-items.

However your function implicitly returns None ( because there is no non-empty return statement )

You need to modify your function to add:

return df, event_name

Update: If you really don't need the return value(s) from your function then don't call your function with anything on the left-hand side of the assignment statement. This implies tuple unpacking from the result of your function. Just call your function like this:

send_to_db(df, event_name)

See: Python Packing and Unpacking and Tuples and Sequences where it says:

This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side. Sequence unpacking requires the list of variables on the left to have the same number of elements as the length of the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM