改变我给出的系列

时间:2016-11-04 08:42:17

标签: python-2.7

from sqlalchemy import Column, Integer, String, Boolean, Float, DateTime,           ForeignKey
from sqlalchemy.orm import relationship, reconstructor

from app import utils
import datetime
import pandas

from base import Base
from series import Series


class ConstantSeries(Series):
__tablename__ = 'constantseries'

# ID Primary Key (linked to parent) 
id = Column(Integer, ForeignKey('series.id'), primary_key=True)

# The value of this series to be returned for all periods.
value = Column(Float)

__mapper_args__ = {'polymorphic_identity': 'constantseries'}

def GetData(self, scenario, periods):
    """Get data values for the specified periods.
    """
    return pandas.Series(data=[self.value]*len(periods), index=periods)

我已经获得了上面的代码,但我希望能够更改它,以便我可以根据具体情况设置一个具有不同值的系列。例如,如果它是工作日,则我的值为100,而周末的值为200

3 个答案:

答案 0 :(得分:0)

您能告诉我您是如何调用上述代码以及它当前返回的内容吗?

似乎GetData函数只返回一个数据结构,即它返回每个句点的Column结构,但没有实际数据。 这里没有显示的是如何填充和访问列中的实际数据。

您可以循环浏览期间并根据当天构建系列数据,例如,如果期间包含[' mon',' tue',' wed', '周四''周五''饱和''太阳']

def GetData(self, scenario, periods):
    mydata = {}
    for p in periods:
        if p in ['sat','sun']:
            e[p] = 200
        else:
            e[p] = 100

    return pandas.Series(mydata, index=periods)

然后调用GetData应该返回类似的东西 '周一' 100 '周二' 100 ... '饱和' 200 '阳光' 200

但那不是你想要的结构,我不认为它是如何使用Getdata功能的。

答案 1 :(得分:0)

def GetData(self, scenario, periods): 
    rtn = {}
    for timest, val in cap.GetData(base_scenario,utils.enumerate_periods(start,end,'H','CET')).iteritems():
        if timest.weekday_name in ['Saturday', 'Sunday']:
            rtn[timest.weekday_name] = (0.72 * val) 
            #0.46*2574 
        else:
            rtn[timest.weekday_name] = (1.0 * val) 
    return [rtn] 

答案 2 :(得分:0)

#Define ConstantSeries class
#I have made no changes here, just what you already had
class ConstantSeries(Series):
    __tablename__ = 'constantseries'

    # ID Primary Key (linked to parent) 
    id = Column(Integer, ForeignKey('series.id'), primary_key=True)

    # The value of this series to be returned for all periods.
    value = Column(Float)

    __mapper_args__ = {'polymorphic_identity': 'constantseries'}

    def GetData(self, scenario, periods):
        """Get data values for the specified periods.
        """
        return pandas.Series(data=[self.value]*len(periods), index=periods) 
#End of class definition


#Define new special Cap2Series class
class Cap2Series(Series):
    #I'm not sure how tablename is used so be aware, might need to put constantseries as before
    __tablename__ = 'cap2series'

    # ID Primary Key (linked to parent) 
    id = Column(Integer, ForeignKey('series.id'), primary_key=True)

    # The value of this series to be returned for all periods.
    value = Column(Float)

    #Same as above, I'm not sure how this is used so be aware,  might need to put constantseries as before
    __mapper_args__ = {'polymorphic_identity': 'cap2series'}


    #Define GetData method of our new special class
    def GetData(self, scenario, periods):

        #instantiate new ConstantSeries instance called cap when you call GetData
        cap = (ConstantSeries(value=self.value))

        rtn = {} 
        for timest, val in cap.GetData(scenario, periods).iteritems():
            if timest.weekday_name in ['Saturday', 'Sunday']: 
                rtn[timest.weekday_name] = (0.72 * val) 
                #0.46*2574              
            else: 
                rtn[timest.weekday_name] = (1.0 * val)            
        return pandas.Series(data=rtn, index=periods)
#End of class definition


#Instantiate new Cap2Series instance called cap2
cap2 = Cap2Series(1647)

#Call GetData method of cap2 instance
cap2.GetData(base_scenario, utils.enumerate_periods(start,end,'H','CET')).plot()


#Is this something like what you're trying to do?