您的位置:首页 > 编程语言 > Python开发

python 得到时间序列的每年、每月最后一天和第一天在序列中的位置

2018-03-30 16:52 375 查看
2018.03.30
大概的函数是这个样子,里面提到的问题,未来一一解决。持续更新。def getFirstLastDayMth(dateSeries):
#dateSeries is like 2018-03-09
#my date series is from 2017.1.1 till now 2018.03.30
#import pandas as pd, import numpy as np FIRST
datesList = pd.to_datetime(dateSeries).strftime("%Y%m%d")
#it is a ndarray
datesList = np.array([int(i) for i in datesList])
trimmedDate = datesList * (float(1)/float(100))#already rounded, have no idea why
#it is actually not rounded, it is just not showing on the variable explorer
#to look at, please print
#to get the last day of the month, divide with 100 and to get the last day of the year, divide with 10000
trimmedDate = np.around(trimmedDate)
lastDayofPeriod = trimmedDate[0:-1] - trimmedDate[1:]
#if from the same month, the difference is 0, and if there is new month, the difference will be nonzero
#the difference will not be -1 or 0, not only these two, but also some other questions
#the lenght of the lastDayofPeriod is 1 less than the datesList
#we have in total 27 months.
#last day of the last month will not appear as there is no new day for a new month
#regardless of whether or not the first day is the first day or the last day or the middle of the month
#the last day for the firth month will appear
#if we use add one method to get the first day, we will also get all the first days
#for the second untill the last month, we will not get the first day for the first month
#then we need to add the first day or last day for the first month and the last month
#what if the first day is the last day of the month, it also should be the first day of the month
#if the first day is not the last day of the month, the first day should be the first day of the first month
#what if the last day is the fist day of the month, then it should be also be the last day of the month
#if the first day and last day are both in the middle of the month, then we just need to add the first day
#as the first input for the first day list and then add the last day as the index of the lastdaylist
#so we need to first check the first day of the datesList, whether they are the first day
#and we check whether the last day of the datesList is the lastDay
#if anything of the two cases happen, then the lengh of the first day and last day will not be equal
#it arises a question: when any of these two cases happen, whether we need to add them as the first day and last day at the same time
#if we are going to calculate some periodical results, we need to make
4000
full use of the error
lastDayList = np.argwhere(lastDayofPeriod !=0)
#lastDayList = [i for i, j in enumerate(lastDayofPeriod) if j != 0]
#gives the same results
lastDayList = np.append(lastDayList, len(datesList))
return lastDayList
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: