如果我在字符串中给出一个日期,例如start_date = '01-Feb-21',那么如何从这个输入中生成总共8个日期呢?
这八次约会是:
['01-Feb-21', # we get this from the input
'28-Feb-21',
'01-Jan-21',
'31-Jan-21',
'01-Dec-20',
'31-Dec-20',
'01-Nov-20',
'30-Nov-20']对应于start_date月份的开始和结束以及前三个月。
发布于 2022-01-12 12:39:21
您可以使用datetime来完成它,但是,获得一个月的最后一天是tricky。
pandas具有构建在datetime之上的良好的时间实用函数。
import pandas as pd
from pandas.tseries.offsets import MonthEnd
start_date = "01-Feb-21"
N = 8
fmt = '%d-%b-%y'
# get month starts
s = pd.date_range(end=start_date, periods=N//2, freq='MS').to_series()
out = (pd.concat([s.dt.strftime(fmt),
(s + MonthEnd()).dt.strftime(fmt)], # get month ends
axis=1)
.iloc[::-1] # reverse order
.to_numpy().ravel().tolist() # convert to flat list
# line above can also be replace with
# .stack().to_list()
)产出:
['01-Feb-21',
'28-Feb-21',
'01-Jan-21',
'31-Jan-21',
'01-Dec-20',
'31-Dec-20',
'01-Nov-20',
'30-Nov-20']发布于 2022-01-12 13:48:50
这个答案有点长,但它只使用datetime和calendar模块来获得预期的结果:
import calendar
from datetime import datetime, date
def generate_rolling_date(input_date, number_of_dates):
"""
Generate Rolling Date
:param input_date: Input Date (e.g. '01-Feb-21')
:param number_of_dates: Number of rolling date to generate
:return:
"""
# Convert input_date to datetime according to the format
parsed_date = datetime.strptime(input_date, "%d-%b-%y")
# Separate Day, Month and Year
parsed_day = parsed_date.day
parsed_month = parsed_date.month
parsed_year = parsed_date.year
# Initialize an empty output list
output = []
# using calendar module, gets the total number of days i.e. last day in parsed_month
_, num_days = calendar.monthrange(parsed_year, parsed_month)
# Check if the parsed_day is the last day of the month or not, according to that set the start_date flag
start_date = False if num_days == parsed_day else True
# Loop through the number_of_dates to generate the rolling dates
for i in range(number_of_dates):
# If the parsed_month value is 0, then decrease the parsed_year by 1 and set the parsed_month value to 12
if parsed_month == 0:
parsed_year -= 1
parsed_month = 12
# Get the total number of days i.e. last day in parsed_month
_, num_days = calendar.monthrange(parsed_year, parsed_month)
# If start_date, then get the first day of the month and set start_date to False,
# so we can get the last day of month in the next loop
if start_date:
# Get the first day of the month
selected_day = date(parsed_year, parsed_month, 1)
start_date = False
else:
# Get the last day of the month
selected_day = date(parsed_year, parsed_month, num_days)
# After getting the last day of the month, decrease the parsed_month by 1 to get the previous month
parsed_month -= 1
start_date = True
# Append selected_day to the output
output.append(date.strftime(selected_day, "%d-%b-%y"))
# Return the output
return output
print(generate_rolling_date("01-Feb-21", 8))它提供了以下输出:
['01-Feb-21',
'28-Feb-21',
'01-Jan-21',
'31-Jan-21',
'01-Dec-20',
'31-Dec-20',
'01-Nov-20',
'30-Nov-20']https://stackoverflow.com/questions/70681514
复制相似问题