简体   繁体   English

为什么在不是“ NoneType”的数据上出现“ NoneType”类型错误?

[英]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. 我有一系列的功能,这些函数采用pandas数据框,清理数据,然后将其发送到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 我得到的错误是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 1) print df.describe()给我正确的熊猫输出,这意味着函数中的那个时刻数据不是None

2) The data gets sent to the sqlite database, which I verified. 2)数据被发送到我验证的sqlite数据库。 So this mean that the data isn't None there either. 因此,这意味着数据也不是None

Why am I getting this error and when is my data turning into None ? 为什么会出现此错误,我的数据何时变为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) : 没有从函数中返回任何东西,但是期望一个2项的元组(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. 您期望返回2个项目的元组。

However your function implicitly returns None ( because there is no non-empty return statement ) 但是,您的函数隐式返回None因为没有非空的return语句

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: 请参阅: Python打包和解压缩以及元组和序列 ,其中指出:

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. 注意,多重分配实际上只是元组打包和序列拆包的组合。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我得到 NoneType? - Why am I getting NoneType? 为什么我得到一个 NoneType? - Why am I getting a NoneType? 为什么会收到有关“ NoneType”的错误消息? - Why am I getting an error message about a “NoneType”? 为什么我会收到此错误? TypeError:iter() 返回了“NoneType”类型的非迭代器 - why am I getting this error? TypeError: iter() returned non-iterator of type 'NoneType' 为什么在第二次循环中出现NoneType错误? - Why am I getting a NoneType error in the second repetition of a loop? 为什么我收到 TypeError: &#39;NoneType&#39; 类型的对象没有 len() 错误(实际上是从我的代码的工作部分复制了代码) - Why am i getting TypeError: object of type 'NoneType' has no len() error (practically copied code from a working part of my code) 尝试进行二维随机游走 function,为什么会出现“+: 'float' 和 'NoneType' 不支持的操作数类型”错误? - Trying to make a 2-D random walk function, why am I getting "unsupported operand type(s) for +: 'float' and 'NoneType'" error? 为什么我在一个集合上使用 len function 得到“TypeError: object of type 'NoneType' has no len()”? - Why am I getting a "TypeError: object of type 'NoneType' has no len()" for this using the len function on a set? 为什么我得到这个 TypeError: unsupported operand type(s) for +: 'int' and 'NoneType' - Why I am getting this TypeError: unsupported operand type(s) for +: 'int' and 'NoneType' 当我尝试从网页上抓取数据时,为什么会收到Nonetype? - Why am I getting a Nonetype when I try to scrape data from webpages?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM