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

本文介绍了如何使用Python处理时间序列数据,以获取每年、每月的第一天和最后一天在序列中的位置,后续会针对相关问题进行详细解答并持续更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值