[英]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.