在函数Python中填充枚举

时间:2016-11-13 15:23:05

标签: python function pandas dataframe enums

不知怎的,我有问题将枚举填入我的函数中。不知何故,我收到以下错误消息:

  

第22行,在SetRevenueScenarios中       self.Scenarios [len(self.Scenarios.keys())] = data

     

属性错误:'市场'对象没有属性'场景'

代码的目标是将重复收入填入熊猫数据框。使用的函数应该填充一次收入,并用专用BU的枚举来识别它。

import csv
from enum import Enum
import pandas as pd

class EmpID(Enum):
    TVs = 1
    Camera = 2
    Computers = 3
    Jeans = 4
    Jacket = 5
    Meat = 6


class Market(object):

    def SetEmployerID(self, empID):
        self.empID = empID

    def SetRevenueScenarios(self, past_revenues, Enum):
        data = pd.Series(past_revenues)
        data = data.convert_objects(convert_numeric=True)
        self.Scenarios[len(self.Scenarios.keys())] = data               

    def GetEmployerID(self):
        return self.empID

    def GetRevenueScenarios(self, scenario_number):
        return self.Scenarios[scenario_number]

#---------------------------------------------------------------

def read_csv(filename):
    market_dict = dict()

    with open(filename, newline='') as csvfile:
        reader = csv.reader(csvfile, delimiter=';')       
        for column in reader:

            if column[0] == "EmpID":
                mark = Market()
                mark.SetEmployerID(column[1])

            if column[0] == "Area":
                if column[1] == "Electronic_TVs": 
                    mark.SetRevenueScenarios(column[1:],EmpID.TVs)

                market_dict[mark.GetEmployerID()] = mark


    return market_dict

#---------------------------------------------------------------

if __name__ == '__main__':
  market_list=read_csv('C:Filiale.TXT')

  for BM_id in market_list:
      print(market_list[BM_id].GetEmployerID())

输入文件如下:

EmpID;Martin
Area;Electronic_TVs
BU;TVs
Revenues;100;300;1000;4000;20000;300000
Area;Electronic_Computers
BU;Computers
Revenues;300;600;4000;8000;200000;3000000
Area;Clothes_Jeans
BU;Jeans
Revenues;10;30;10000;40000;100000;2000000
EmpID;James
Area;Food_Meat
BU;Meat
Revenues;100;300;10;40;20;30

1 个答案:

答案 0 :(得分:1)

该错误消息是正确的。您的Market类没有Scenarios属性,但您在此处尝试访问一个:

self.Scenarios[len(self.Scenarios.keys())]

你忘了在构造函数中定义它吗?