简体   繁体   English

TypeError:'type'对象在pandas DataFrame中没有属性'__getitem__'

[英]TypeError: 'type' object has no attribute '__getitem__' in pandas DataFrame

I was trying to analyse the play-by-play data of a basketball team What I did was to read a csv file into a DataFrame object. 我正在尝试分析篮球队的比赛数据,我所做的是将csv文件读取到DataFrame对象中。

I want to preserve the functionality of the DataFrame object while adding in new attributes to the existing object. 我想保留DataFrame对象的功能,同时在现有对象中添加新属性。 Thus I wrote a class called Basketball: 因此我写了一个叫篮球的课:

from data_math import *
import pandas as pd
class Basketball(pd.DataFrame):
    def __init__(self,*args,**kargs):
        pd.DataFrame.__init__(self,*args,**kargs)
        self.FGM = calculate_FGM(pd.DataFrame)
        self.FGA = calculate_FGA(pd.DateFrame)
        self.FGP = self.FGM / self.FGA
        self.M3  = calculate_3M(pd.DataFrame)
        self.A3  = calcualte_3A(pd.DataFrame)
        self.P3  = self.M3 / self.A3
        self.FTM = calcualte_FTM(pd.DataFrame)
        self.FTA = calculate_FTA(pd.DataFrame)
        self.FTP = self.FTM / self.FTA
    # self.P = score_calculate(pd.DataFrame)

I wrote another data_math.py file to help calculate the different attributes I wanted to include into the Basketball class. 我编写了另一个data_math.py文件,以帮助计算要包含在Basketball类中的不同属性。

from pandas import DataFrame

def score_calculate(df):
    df_pt_scored = df[((df['etype']=='shot') & (df['result']=='made'))]
    df_ft_scored = df[((df['etype']=='free throw') & (df['result']=='made'))]
    return df_pt_scored['points'].sum()+len(df_ft_scored.index)

def calculate_FGM(df):
    cond_pt = (df['etype']=='shots') & (df['results']=='made')
    cond_ft = (df['etype']=='freethrow') & (df['results']=='made')
    return len(df[cond_pt].index)+len(df[cond_ft].index)

def calculate_FGA(df):
    shot_cond= df['etype']=='shot'
    free_throw_cond = df['etype']=='free throw'
    return len(df[shot_cond].index)+len(df[free_throw_cond].index)

def calculate_3M(df):
    cond_3M= (df['etype']=='shot')&(df['type']=='3pt')&(df['result']=='made')
    return len(df[cond_3M].index)

def calcualte_3A(df):
    cond_3A = (df['etype']=='shot')&(df['type']=='3pt')
    return len(df[cond_3A].index)

def calculate_FTM(df):
    cond_FTM =(df['etype']=='free throw') & (df['result']=='made')
    return len(df[cond_FTM].index)

def calcualte_FTA(df):
    cond_FTA =(df['etype']=='free throw')
    return len(df[cond_FTA].index)

In the end I start my program from main.py which I hope would give me the correct output. 最后,我从main.py启动程序,希望可以给我正确的输出。 However while executing on this line: 但是,在此行上执行时:

team1= Basketball(tm1)

I received the following Traceback 我收到以下回溯

 Traceback (most recent call last):
  File "/Users/luoyicheng/Developer/STAR-Research/data_analysis/source code/main.py", line 20, in <module>
    team1= Basketball(tm1)
  File "/Users/luoyicheng/Developer/STAR-Research/data_analysis/source code/Basketball.py", line 6, in __init__
    self.FGM = calculate_FGM(pd.DataFrame)
  File "/Users/luoyicheng/Developer/STAR-Research/data_analysis/source code/data_math.py", line 9, in calculate_FGM
    cond_pt = (df['etype']=='shots') & (df['results']=='made')
TypeError: 'type' object has no attribute '__getitem__'

I am new to python programming and could not figure out why this error has occurred. 我是python编程的新手,无法弄清楚为什么会发生此错误。 To my understanding, this error means I am unable to use indexing feature of the DataFrame. 据我了解,此错误意味着我无法使用DataFrame的索引功能。 However, if I try to code in my main function similar things I am able to get the output I want. 但是,如果我尝试在主要函数中进行类似的编码,则可以得到所需的输出。 I am also not clear of how to extend the existing DataFrame class so that I can still access the methods in the DataFrame class while extending the team1 object to have attributes such as FGM, FGA, etc. 我也不清楚如何扩展现有的DataFrame类,以便在扩展team1对象以具有诸如FGM,FGA等属性的同时,仍可以访问DataFrame类中的方法。

The idea of extending this class is to allow me to pass any DataFrame object in the Basketball() so that I can have an object with extending attributes and methods. 扩展此类的想法是允许我在Basketball()中传递任何DataFrame对象,以便我可以拥有一个具有扩展属性和方法的对象。 I think I also lack an understanding of the use of init and self. 我想我也缺乏对init和self用法的理解。

Please don't blame for not describing the problem clearly as I am not familiar with all the terminology in OOP. 请不要怪我没有清楚地描述问题,因为我不熟悉OOP中的所有术语。

Thank you so much! 非常感谢!

You're passing each function pd.DataFrame which is of type type: 您正在传递类型为type的每个函数pd.DataFrame

In [11]: type(pd.DataFrame)
Out[11]: type

Hence the exception message. 因此,异常消息。

You mean to be passing self (which is of type DataFrame): 您的意思是传递self(类型为DataFrame):

self.FGM = calculate_FGM(pd.DataFrame)
...

should read: 应该读:

self.FGM = calculate_FGM(self)
...

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

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