If n is 0 then return the ltd mean; else return the n day mean
553981
def sma(arg, n):
    
    if n == 0:
        return pd.expanding_mean(arg)
    else:
        return pd.rolling_mean(arg, n, min_periods=n)
1083544
def roll_mean(input, window):
    ''''''
    nobs, i, j, sum_x = 0,0,0,0.
    N = len(input)

    if window > N:
        raise ValueError('Out of bound')

    output = np.ndarray(N-window+1,dtype=input.dtype)

    for val in input[:window]:
        if val == val:
            nobs += 1
            sum_x += val

    output[j] = NaN if not nobs else sum_x / nobs

    for val in input[window:]:
        prev = input[j]
        if prev == prev:
            sum_x -= prev
            nobs -= 1

        if val == val:
            nobs += 1
            sum_x += val

        j += 1
        output[j] = NaN if not nobs else sum_x / nobs

    return output
857988
def _get_wr(cls, df, n_days):
        
        n_days = int(n_days)
        ln = df['low'].rolling(min_periods=1, window=n_days,
                               center=False).min()

        hn = df['high'].rolling(min_periods=1, window=n_days,
                                center=False).max()
        column_name = 'wr_{}'.format(n_days)
        df[column_name] = (hn - df['close']) / (hn - ln) * 100
321633
def move_if_not_on_dow(original, replacement, dow_not_orig, dow_replacement):
    
    return lambda x: (
        (x.hdate.day == original and x.gdate.weekday() != dow_not_orig) or
        (x.hdate.day == replacement and x.gdate.weekday() == dow_replacement))
689728
def mean(values: Sequence[Union[int, float, None]]) -> Optional[float]:
    
    total = 0.0  # starting with "0.0" causes automatic conversion to float
    n = 0
    for x in values:
        if x is not None:
            total += x
            n += 1
    return total / n if n > 0 else None
1169625
def st_mtime(self):
        """"""
        mtime = self._st_mtime_ns / 1e9
        return mtime if self.use_float else int(mtime)
1070589
def next_monday_or_tuesday(dt):
    
    dow = dt.weekday()
    if dow == 5 or dow == 6:
        return dt + timedelta(2)
    elif dow == 0:
        return dt + timedelta(1)
    return dt
1070657
def stddev(values, meanval=None):  #from AI: A Modern Appproach
    """"""
    if meanval == None: meanval = mean(values)
    return math.sqrt( sum([(x - meanval)**2 for x in values]) / (len(values)-1) )
470529
def get_day():
    """"""
    day = datetime.datetime.today().weekday()
    if len(sys.argv) == 3:
        if sys.argv[2] == "mon":
            day = 0
        elif sys.argv[2] == "tue":
            day = 1
        elif sys.argv[2] == "wed":
            day = 2
        elif sys.argv[2] == "thur":
            day = 3
        elif sys.argv[2] == "fri":
            day = 4
        else:
            day = 5
    if day > 4:
        print("There is no information about the menu today.")
        exit(5)
    return day
1282370
def g(self):
        "'"
        if self.data.hour == 0:
            return 12
        if self.data.hour > 12:
            return self.data.hour - 12
        return self.data.hour
1196503
def next_working_day(count = 1)
      return self if count == 0
      negative = count < 0
      count = count.abs
      date = negative ? yesterday : tomorrow
      
      loop do
        count -= 1 if date.working_day?
        return date if count.zero? 
        
        date += (negative ? -1 : 1).day
      end
    end
1260699
def mean(name, num, minimum=0, maximum=0, ref=None):
    '''
    
    '''
    return calc(
        name=name,
        num=num,
        oper='mean',
        minimum=minimum,
        maximum=maximum,
        ref=ref
    )
1214683
def Mean(self):
        
        mu = 0.0
        for x, p in self.d.iteritems():
            mu += p * x
        return mu
1099136
def inc(self):
        """"""
        inc1 = self.get('INC', None)
        inc2 = self.get('INC2', None)
        if inc1 is None and inc2 is None:
            return None
        if inc2 is None:
            return inc1
        if inc1 is None:
            return -1 * inc2
        return (inc1 - inc2) / 2.0
579709
def size_in_days(self):
        
        unit, instant, length = self

        if unit == DAY:
            return length
        if unit in [MONTH, YEAR]:
            last_day = self.start.offset(length, unit).offset(-1, DAY)
            return (last_day.date - self.start.date).days + 1

        raise ValueError("Cannot calculate number of days in {0}".format(unit))
688968
def _inc(self, incunit, incval, reqdict, origdt, dt):
        '''
        
        '''
        if incunit == TimeUnit.YEAR:
            return dt.replace(year=dt.year + incval)
        if incunit == TimeUnit.MONTH:
            newyear = dt.year
            absmonth = dt.month + incval - 1
            newmonth = absmonth % 12 + 1
            newyear += absmonth // 12
            daysinmonth = calendar.monthrange(newyear, newmonth)[1]
            dayofmonthreq = reqdict.get(TimeUnit.DAYOFMONTH)
            if dayofmonthreq is not None:
                newday = _dayofmonth(dayofmonthreq, newmonth, newyear)
            else:
                newday = min(daysinmonth, dt.day)
            return dt.replace(day=newday, month=newmonth, year=newyear)
        if incunit == TimeUnit.DAY:
            return dt + datetime.timedelta(days=incval)
        if incunit == TimeUnit.DAYOFWEEK:
            # incval in this case means next day of week whose weekday matches incval (0-6)
            days = (6 + incval - dt.weekday()) % 7 + 1
            newdt = dt + datetime.timedelta(days=days)
            assert newdt.weekday() == incval
            return newdt
        if incunit == TimeUnit.HOUR:
            return dt + datetime.timedelta(hours=incval)
        if incunit == TimeUnit.MINUTE:
            return dt + datetime.timedelta(minutes=incval)
        else:
            assert 0, 'Invalid incunit'
306737
def var(series):
    
    if np.issubdtype(series.dtype, np.number):
        return series.var()
    else:
        return np.nan