【问题标题】:Python Boolean mask using a dictionary使用字典的 Python 布尔掩码
【发布时间】:2018-04-26 00:17:58
【问题描述】:

我正在尝试在一天中的某个时间计算一个给定条件的值。矢量/系列是给定小时的净太阳辐射和收集数据的时间。当在白天获得该值时,我必须将该值乘以 0.1,而当在夜间获得该值时,我将该值乘以 0.5。问题是日光时间按月(甚至按周)显着变化,如下面的日光字典所示:

我正在尝试创建一个布尔掩码(在日光下),这将帮助我应用计算/乘法,而无需在“时间”向量中的每个值上使用 for 循环,并根据日光字典检查它,这就是我正在做:

def Ghr(time, Rn):
    #soil heatflux cal
    #time is a single vale of the time vector
    mon = time.strftime('%b') #strips the month

    #sunrise -sunset hours from 1st of the month (sr,ss) to end of the month
    #Jan-1st SR 8:17, Jan-31st SR 07:47
    #Jan-1st SS 16:03, Jan-31st SS 16:52

    daylight = {'Jan':('08:00', '16:25'),
            'Feb':('07:20', '17:20'),
            'Mar':('06:45', '18:40'),
            'Apr':('06:05', '20:05'),
            'May':('05:10', '20:55'),
            'Jun':('04:50', '21:25'),
            'Jul':('05:10', '21:15'),
            'Aug':('05:50', '20:30'),
            'Sep':('06:45', '19:25'),
            'Oct':('07:00', '17:30'),
            'Nov':('07:25', '16:15'),
            'Dec':('08:05', '16:00')}

    #strips the hour and minute from the daylight dictionary
    #then withindaylight is the boolean  after checking the
    #time the data was collected against the these stripped values

    daybegin = dt.strptime(daylight[mon][0], '%H:%M').time()
    dayend= dt.strptime(daylight[mon][1], '%H:%M').time()

    withindaylight = daybegin <= time.time() <= dayend


    #I want to apply the boolean mask such that it produces the following,
    #but returns a vector and not just a single value

    if withindaylight:
        return .1*Rn   #I want to return a series and not just a single value

    else:
        return .5*Rn

【问题讨论】:

    标签: python dictionary boolean time-series


    【解决方案1】:

    我的代码需要做几件事:

    1. 如何确保我的“时间”的每个实例都映射到正确的月份作为“日光”的键...

    2. 如何将元组提取到单独的列表中

    3. 将保存字符串的元组转换为 pd.Series 中的时间对象

    4. 转换为掩码并应用乘法

    我用以下代码解决了这些问题:

    def Ghr(time, Rn):
        """Soil Heat flux calculator"""
    
        #sunrise -sunset hours from 1st of the month (sr,ss) to end of the month
        #Jan-1st SR 8:17, Jan-31st SR 07:47
        #Jan-1st SS 16:03, Jan-31st SS 16:52  
    
        daylight = {'Jan':('08:00', '16:25'),
                'Feb':('07:20', '17:20'),
                'Mar':('06:45', '18:40'),
                'Apr':('06:05', '20:05'),
                'May':('05:10', '20:55'),
                'Jun':('04:50', '21:25'),
                'Jul':('05:10', '21:15'),
                'Aug':('05:50', '20:30'),
                'Sep':('06:45', '19:25'),
                'Oct':('07:00', '17:30'),
                'Nov':('07:25', '16:15'),
                'Dec':('08:05', '16:00')}
    
        #this maps the month of 'time' as to the dictionary daylight
        #then unzips the tuple to daystart and dayend lists which 
        #are then converted to pandas.Series objects for use in boolean mask
        daystart, dayend = zip(*time.dt.strftime('%b').map(daylight))
    
        #dt.strftime extracts the 'Mon' ('%b') from the DateTime object and
        #maps it to the dictionary and saves the morning and dusk hours respectively
    
        #conversion to pandas series
        daystart=pd.Series(pd.to_datetime(daystart).time)
        dayend=pd.Series(pd.to_datetime(dayend).time)
        t=pd.Series(time.dt.time)
        #the above converts the strings to datetime objects and saves it
        #to a pandas Series, t is just a to_Series conversion
    
    
        light_mask = t.between(daystart, dayend)
        #this mask will be used to designate which values get multiplied by
        #either .1 or .5
    
    
        #True values get multiplied by .1 (The inversion is because 
        #pd.Series.where() only changes the values which are False)
        Ghr = Rn.where(np.invert(light_mask), Rn*.1)
    
        #False values multiplied by *.5
        Ghr = Rn.where(light_mask, Ghr*.5)
    
        return Ghr
    

    【讨论】:

      猜你喜欢
      • 2015-06-23
      • 2019-04-05
      • 2013-05-17
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多