Python 熊猫日期\u范围从结束日期到开始日期
在本文中,我试图使用Python生成一系列半年一次的日期Python 熊猫日期\u范围从结束日期到开始日期,python,datetime,pandas,Python,Datetime,Pandas,在本文中,我试图使用Python生成一系列半年一次的日期Pandas提供了一个函数来帮助实现这一点,但是我希望我的日期范围从结束日期开始,并向后迭代 例如,给定输入: start = datetime.datetime(2016 ,2, 8) end = datetime.datetime(2018 , 6, 1) pd.date_range(start, end, freq='6m') DatetimeIndex(['2016-02-29', '2016-08-31', '2017-02-2
Pandas
提供了一个函数来帮助实现这一点,但是我希望我的日期范围从结束日期开始,并向后迭代
例如,给定输入:
start = datetime.datetime(2016 ,2, 8)
end = datetime.datetime(2018 , 6, 1)
pd.date_range(start, end, freq='6m')
DatetimeIndex(['2016-02-29', '2016-08-31', '2017-02-28', '2017-08-31',
'2018-02-28'])
DatetimeIndex(['2016-02-08', '2016-06-01', '2016-12-01', '2017-06-01',
'2017-12-01', '2018-06-01'])
结果是:
start = datetime.datetime(2016 ,2, 8)
end = datetime.datetime(2018 , 6, 1)
pd.date_range(start, end, freq='6m')
DatetimeIndex(['2016-02-29', '2016-08-31', '2017-02-28', '2017-08-31',
'2018-02-28'])
DatetimeIndex(['2016-02-08', '2016-06-01', '2016-12-01', '2017-06-01',
'2017-12-01', '2018-06-01'])
如何生成以下内容:
start = datetime.datetime(2016 ,2, 8)
end = datetime.datetime(2018 , 6, 1)
pd.date_range(start, end, freq='6m')
DatetimeIndex(['2016-02-29', '2016-08-31', '2017-02-28', '2017-08-31',
'2018-02-28'])
DatetimeIndex(['2016-02-08', '2016-06-01', '2016-12-01', '2017-06-01',
'2017-12-01', '2018-06-01'])
这可以在没有熊猫的情况下完成,而使用datutil。然而,它可能比它应该做的更为复杂:
from datetime import date
import math
from dateutil.relativedelta import relativedelta
#set up key dates
start = date(2016 ,2, 8)
end = date(2018 , 6, 1)
#calculate date range and number of 6 month periods
daterange = end-start
periods = daterange.days *2//365
#calculate next date in sequence and check for year roll-over
next_date = date(start.year,math.ceil(start.month/6)*6,1)
if next_date < start: next_date = date(next_date.year+1,next_date.month,1)
#add the first two values to a list
arr = [start.isoformat(),next_date.isoformat()]
#calculate all subsequent dates using 'relativedelta'
for i in range(periods):
next_date = next_date+ relativedelta(months=+6)
arr.append(next_date.isoformat())
#display results
print(arr)
from datetime导入日期
输入数学
从dateutil.relativedelta导入relativedelta
#设定关键日期
开始=日期(2016年2月8日)
结束=日期(2018年6月1日)
#计算日期范围和6个月期间的数量
日期范围=结束-开始
期间=daterange.days*2//365
#按顺序计算下一个日期,并检查是否存在年度滚动
下一个日期=日期(起始年,数学单元(起始月/6)*6,1)
如果下一个日期<开始:下一个日期=日期(下一个日期.年+1,下一个日期.月,1)
#将前两个值添加到列表中
arr=[start.isoformat(),next_date.isoformat()]
#使用“relativedelta”计算所有后续日期
对于范围内的i(周期):
下一个日期=下一个日期+相对时间(月=+6)
arr.append(next_date.isoformat())
#显示结果
打印(arr)
使用更新的输出(来自您所做的编辑),您可以执行以下操作:
from pandas.tseries.offsets import DateOffset
end = datetime.datetime(2018 , 6, 1)
start = datetime.datetime(2016 ,2, 8)
#Get the range of months to cover
months = (end.year - start.year)*12 + end.month - start.month
#The frequency of periods
period = 6 # in months
pd.DatetimeIndex([end - DateOffset(months=e) for e in range(0, months, period)][::-1]).insert(0, start)
这是一个相当简洁的解决方案,尽管我没有比较运行时,所以我不确定它有多快
基本上,这只是将您需要的日期创建为列表,然后将其转换为日期时间索引。删除了我的初始答案。我想这里主要是偏移量,对吗?@Maximilian是的,这是另一种说法。我得到一个错误
TypeError:integer参数,在next\u date=
行上得到float
。@pyCthon下一个日期行是哪一行?第一个还是for循环中的那个?如果是后者,那么可能是您没有安装dateutil,我应该提到这一点。第一个是安装dateutil,我安装了'2.4.2'
啊,好的,尝试使用整数除法,即start.month//6