python 每周第一天和最后一天

后续详细。

def getFirstLastDayListWk(dateSeries):
    weekNum = pd.to_datetime(dateSeries).strftime("%W")
    weekNum = weekNum.astype(int)
    weekNum0 = np.argwhere(weekNum == 0)
    #assuming the first date is not 0 week, otherwise, it needs more
    for i in range(len(weekNum0)):
        weekNum[list(weekNum).index(0)] = weekNum[list(weekNum).index(0)-1]  
        #index(0)returns the location of the first 0 it appears
    #assuming that the #no assuming change them
    lastDayofPeriod = weekNum[0:-1] - weekNum[1:]
    # 0 -1 0r 51, 51 is the difference of the 52 and 1
    #nonono, it can give the result of -2, where there are holidays such as the 
    #spring festival and the national day. but they have one charactersitics in common
    #they are positive, not always 51, they can be 52, such as the year 2018
    lastDayList = np.argwhere(lastDayofPeriod!=0)
    #lastDayofPeriod[np.where((lastDayofPeriod!=0) & (lastDayofPeriod!=(-1)))]
    #np.where((lastDayofPeriod!=0) & (lastDayofPeriod!=(-1)))
    #np.argwhere(weekNum ==0) returns empty, although there is 
    #non-continuous weeks, not appearing here
    #not thinking of how to deal with the lastDayofPeriod, but thinking of the 
    #weekNum, solve the problem in the above line
    #change the weeknum of the week 0 to the weeknum of the last week in the last year
#    lastDayListYear = np.argwhere(lastDayofPeriod>0)
#    lastDayListYear = np.append(0,lastDayListYear)
#    #must give the value, otherwise no change
#    lastDayListYear = np.append(lastDayListYear,len(dateSeries)-1)
#    count
    firstDayList = lastDayList +1
    firstDayList = np.append(0, firstDayList)
    lastDayList = np.append(lastDayList, len(dateSeries)-1)
    #set(firstDayList).intersection(set(lastDayList))
    #returns the days when the first and last day is the same day for the period
    return (firstDayList, lastDayList)

猜你喜欢

转载自blog.csdn.net/henbile/article/details/79820872
今日推荐