Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中从1个日期生成当前日历年月份和未来日历年月份_Python_Python 3.x_Pandas_Date_Datetime - Fatal编程技术网

在Python中从1个日期生成当前日历年月份和未来日历年月份

在Python中从1个日期生成当前日历年月份和未来日历年月份,python,python-3.x,pandas,date,datetime,Python,Python 3.x,Pandas,Date,Datetime,我有以下代码: from datetime import datetime import pandas as pd last_day_of_current_year = datetime.now().date().replace(month=12, day=31) with open(("mtn_mtx.txt").lower(), "r") as rfile: next(rfile) for line in rfile:

我有以下代码:

from datetime import datetime
import pandas as pd

last_day_of_current_year = datetime.now().date().replace(month=12, day=31)


with open(("mtn_mtx.txt").lower(), "r") as rfile:
    next(rfile)

    for line in rfile:
        line = line.rstrip('\n')
        line = line.upper()
        line = line.split('\t')

        firstcoupondate = (line[5])

        month_list = [i.strftime("%Y-%m-%d") for i in pd.date_range(start=firstcoupondate, end=last_day_of_current_year, freq='MS')]

        print(month_list)
这允许我从文件中输入日期(firstcoupondate),并填充从firstcoupondate到今年最后一天之间的未来月份。但是,我想修改此代码,使其只生成当前年份的所有12个月,而不是第一个Coupondate年份

例如,如果我从文件中输入以下firstcoupondate:“2020-02-05”,我的上述代码将生成以下列表:

['2020-03-01','2020-04-01','2020-05-01','2020-06-01','2020-07-01','2020-08-01','2020-09-01','2020-10-01','2020-12-01','2021-01-01','2021-01-01','2021-03-01','2021-04-01','2021-05-01','2021-06-01','2021-07-01','2021-08-01','2021-09-01','2021-10-01','2021-11-01','2021-12-01']

正如您所看到的,所有日期都显示了每个月的第一天,这是不正确的,而且我缺少了1月/2月的前两个日期(“2020-01-05”、“2020-02-05”),因此我在代码中输入的任何firstcoupondate都不会填充当前年份的前两个月日期,在“2020-02-05”中喂食时,我想要的输出而不是上面列出的日期应该是:

['2021-01-05','2021-02-05','2021-03-05','2021-04-05','2021-05-05','2021-06-05','2021-07-05','2021-08-05','2021-09-05','2021-10-05','2021-11-05','2021-12-05']

此外,我输入的一些firstcoupondate的未来年份为一年,因此如果我有日期“2025-04-12”,我希望随后填充与上述相同的(全部12个月),但对于我输入的firstcoupondate内的年份,例如firstcoupondate='2025-04-12',我希望生成以下月份列表:


['2025-01-12','2025-02-12','2025-03-12','2025-04-12','2025-05-12','2025-06-12','2025-07-12','2025-08-12','2025-09-12','2025-10-12','2025-11-12','2025-12-12']提供了一个可重复的例子。生成了我认为与tab delim文件类似的数据

关键部分

  • 定义函数
  • 在文件读取上下文中使用
  • 在上下文中使用
  • 熊猫产量 A. B C D E 耦合 全部CD 0 C B A. C C 2019-01-21 00:00:00 ['2021-03-21', '2021-04-21', '2021-05-21', '2021-06-21', '2021-07-21', '2021-08-21', '2021-09-21', '2021-10-21', '2021-11-21', '2021-12-21'] 1. B A. B A. C 2020-10-22 00:00:00 ['2021-03-22', '2021-04-22', '2021-05-22', '2021-06-22', '2021-07-22', '2021-08-22', '2021-09-22', '2021-10-22', '2021-11-22', '2021-12-22'] 2. B C C A. A. 2020-11-19 00:00:00 ['2021-03-19', '2021-04-19', '2021-05-19', '2021-06-19', '2021-07-19', '2021-08-19', '2021-09-19', '2021-10-19', '2021-11-19', '2021-12-19'] 3. B C C C A. 2023-12-22 00:00:00 ['2023-01-22', '2023-02-22', '2023-03-22', '2023-04-22', '2023-05-22', '2023-06-22', '2023-07-22', '2023-08-22', '2023-09-22', '2023-10-22', '2023-11-22', '2023-12-22'] 4. A. B A. A. B 2020-10-06 00:00:00 ['2021-03-06', '2021-04-06', '2021-05-06', '2021-06-06', '2021-07-06', '2021-08-06', '2021-09-06', '2021-10-06', '2021-11-06', '2021-12-06']
    你想从文件中得到什么?您感兴趣的第五代币的月日?谢谢,但还不是我所需要的,如果coupondate fed in的年份在过去或现在,我希望看到当前年份的所有12个月在2021年填充,但如果coupondate fed in是在未来,那么我希望看到该年份的所有12个月在2023年填充,例如2023年的所有12个月。过去/现在的coupondate输入:“2019-03-15”所需的输出示例:[“2021-01-15”、“2021-02-15”、“2021-04-15”、“2021-05-15”、“2021-06-15”、“2021-07-15”、“2021-08-15”、“2021-09-15”、“2021-10-15”、“2021-11-15”、“2021-12-15”]提前感谢您-如果我现在正确理解您的逻辑,我们已经更新了这已经很接近了,但需要显示所有12个月的产量,如图所示:coupondate(2019-01-21)-['2021-01-21','2021-02-21','2021-03-21','2021-04-21','2021-05-21','2021-06-21','2021-07-21','2021-08-21','2021-10-21','2021-11-21','2021-12-21']您为coupondate生产的产量(2019-01-21)输出列表“2021-01-21”、“2021-02-21”中缺少1月和2月。非常感谢您的帮助。
    def drng(d):n=pd.to_datetime(“now”)s=n.replace(month=1,day=1)如果dimport random
    import datetime as dt
    from pathlib import Path
    
    s = 5
    coupondates = [dt.date(2019 + random.randint(0,4), random.randint(1,12), random.randint(1,28)) for _ in range(s)]
    
    cd = pd.DataFrame({
        "A":np.random.choice(["A","B","C"], s),"B":np.random.choice(["A","B","C"], s),
        "C":np.random.choice(["A","B","C"], s),"D":np.random.choice(["A","B","C"], s),
        "E":np.random.choice(["A","B","C"], s),
        "coupondate": coupondates
                 })
    
    p = Path.cwd().joinpath("mtn_mtx.txt")
    
    ye = pd.to_datetime("now").replace(month=12, day=31)
    cd.to_csv(p, sep="\t", index=False)
    
    def drng(d):
        n = pd.to_datetime("now")
        s = n.replace(month=1, day=1) if d < n else d.replace(month=1, day=1)
        e = n.replace(month=12, day=31) if d<n else d.replace(month=12, day=31)
        return list((pd.date_range(s,e, freq="MS") + pd.DateOffset(days=d.day-1)).strftime("%Y-%m-%d"))
    
    
    
    # file solution
    with open(p) as f:
        next(f)
        for line in f:
            line = line.rstrip("\n").upper().split("\t")
            print(line[5], drng(pd.to_datetime(line[5])))
    
    # pandas solution
    df = pd.read_csv(p, sep="\t")
    df.coupondate = pd.to_datetime(df.coupondate)
    df = df.assign(allcd=df.coupondate.apply(drng))
    
    print(df.to_markdown())
    
    
    2019-01-21 ['2021-03-21', '2021-04-21', '2021-05-21', '2021-06-21', '2021-07-21', '2021-08-21', '2021-09-21', '2021-10-21', '2021-11-21', '2021-12-21']
    2020-10-22 ['2021-03-22', '2021-04-22', '2021-05-22', '2021-06-22', '2021-07-22', '2021-08-22', '2021-09-22', '2021-10-22', '2021-11-22', '2021-12-22']
    2020-11-19 ['2021-03-19', '2021-04-19', '2021-05-19', '2021-06-19', '2021-07-19', '2021-08-19', '2021-09-19', '2021-10-19', '2021-11-19', '2021-12-19']
    2023-12-22 ['2023-01-22', '2023-02-22', '2023-03-22', '2023-04-22', '2023-05-22', '2023-06-22', '2023-07-22', '2023-08-22', '2023-09-22', '2023-10-22', '2023-11-22', '2023-12-22']
    2020-10-06 ['2021-03-06', '2021-04-06', '2021-05-06', '2021-06-06', '2021-07-06', '2021-08-06', '2021-09-06', '2021-10-06', '2021-11-06', '2021-12-06']