简体   繁体   English

pd.merge 在函数内部无法正常工作,而在外部工作正常,TypeError 消息

[英]pd.merge not working inside a function while working fine outside, TypeError message

I created a function that would clean a dataframe before merging with another, and then I would use pd.merge(), it was working fine.我创建了一个函数,可以在与另一个合并之前清理一个数据帧,然后我会使用 pd.merge(),它工作正常。 I decided that I could just put the merge procedure into the function as well.我决定我也可以将合并过程放入函数中。 But suddenly the merge fuction doesn't work, instead it gives me the following error但是突然合并功能不起作用,而是给了我以下错误

 "TypeError: Can only merge Series or DataFrame objects, a <class 'NoneType'> was passed"

Why the second one doesn't work?为什么第二个不起作用?

This is the first approach, the one that worked这是第一种方法,有效的方法

    def prep_merge(dataframe):
        try:
            dataframe.drop(['PLAYER_ID','LEAGUE_ID','TEAM_ID','SEASON_ID',
                            'TEAM_ABBREVIATION','PLAYER_AGE','FG_PCT', 'FG3_PCT', 
                            'FT_PCT'], axis =1, inplace = True)
        except KeyError:
            dataframe.drop([ 'PLAYER_ID','LEAGUE_ID','Team_ID','FG_PCT', 'FG3_PCT', 
                            'FT_PCT'], axis =1, inplace = True)
        cols = dataframe.columns
        new_cols = []
        for c in cols:
            new_cols.append(c + "_PerG")
        return  dataframe.rename(columns=(dict(zip(cols,new_cols))), inplace= True)
    prep_merge(kb_PerG_SR)
    kb_SR = pd.merge(kb_totals_SR, kb_PerG_SR, left_index= True, right_index= True)

Here's the one with pd.merge inside the function:这是函数内部带有 pd.merge 的那个:

    def merge_stats(dataframe1,dataframe2):
        try:
            dataframe2.drop(['PLAYER_ID','LEAGUE_ID','TEAM_ID','SEASON_ID',
                            'TEAM_ABBREVIATION','PLAYER_AGE','FG_PCT', 'FG3_PCT', 
                            'FT_PCT'], axis =1, inplace = True)
        except KeyError:
            dataframe2.drop([ 'PLAYER_ID','LEAGUE_ID','Team_ID','FG_PCT', 'FG3_PCT', 
                            'FT_PCT'], axis =1, inplace = True)
        cols = dataframe2.columns
        new_cols = []
        for c in cols:
            new_cols.append(c + "_PerG")
        dataframe2 = dataframe2.rename(columns=(dict(zip(cols,new_cols))), inplace= True)
        return pd.merge(dataframe1,dataframe2, left_index= True, right_index= True)
    
    kb_SR = merge_stats(kb_totals_SR, kb_PerG_SR)

I've also tried storing the merged frame inside a variable and returning the variable instead of the direct pd.merge(), but as I expected it didn't worked我还尝试将合并的帧存储在一个变量中并返回该变量而不是直接的 pd.merge(),但正如我所料,它没有奏效

Full error message:完整的错误信息:

Traceback (most recent call last):

  

    File "C:\Users\guilh\OneDrive\Data Science\Python\KB\data gathering and clenaing.py", line 74, in <module>
        kb_SR = merge_stats(kb_totals_SR, kb_PerG_SR)
    
      File "C:\Users\guilh\OneDrive\Data Science\Python\KB\data gathering and clenaing.py", line 72, in merge_stats
        return pd.merge(dataframe1,dataframe2, left_index= True, right_index= True)
    
      File "C:\Users\guilh\anaconda3\lib\site-packages\pandas\core\reshape\merge.py", line 73, in merge
        op = _MergeOperation(
    
      File "C:\Users\guilh\anaconda3\lib\site-packages\pandas\core\reshape\merge.py", line 572, in __init__
        _right = _validate_operand(right)
    
      File "C:\Users\guilh\anaconda3\lib\site-packages\pandas\core\reshape\merge.py", line 2006, in _validate_operand
        raise TypeError(
    
    TypeError: Can only merge Series or DataFrame objects, a <class 'NoneType'> was passed

inplace=True means that rename will return None . inplace=True意味着rename将返回None This didn't effect your original code because you returned the result of the inplace rename , then didn't use the returned value.这不会影响您的原始代码,因为您返回了就地rename的结果,然后没有使用返回的值。 In the new code though, you attempt to reassign the None back into dataframe2 , which means that the None will get fed to merge , thus the error.但是,在新代码中,您尝试将None重新分配回dataframe2 ,这意味着None将被馈送到merge ,从而导致错误。

Just don't reassign it:只是不要重新分配它:

dataframe2.rename(columns=(dict(zip(cols, new_cols))), inplace=True)
return pd.merge(dataframe1, dataframe2, left_index=True, right_index=True)

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

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