python 如何查找从 2019 年 12 月开始并在两个日期列之间向前推进的每个月的天数

标签 python pandas numpy dataframe

我有两个日期列“StartDate”和“EndDate”。我想找到 2019 年 12 月以来这两个日期之间每个月的天数,并忽略 2019 年之前的任何月份进行计算。每行的 StartDate 和 EndDate 可以跨越 2 年,并且月份重叠,并且 Date 列也可以为空。

示例数据:

df = {'Id': ['1','2','3','4','5','6','7', '8'],
      'Item': ['A','B','C','D','E','F','G', 'H'],
        'StartDate': ['2019-12-10', '2019-12-01', '2019-10-01', '2020-01-01', '2019-03-01','2019-03-01','2019-10-01', ''],
        'EndDate': ['2020-02-21' ,'2020-01-01','2020-08-31','2020-01-30','2019-12-31','2019-12-31','2020-08-31', '']
        }
df = pd.DataFrame(df,columns= ['Id', 'Item','StartDate','EndDate'])

预期运营成本:

1

以下解决方案部分有效。

df['StartDate'] = pd.to_datetime(df['StartDate'])
df['EndDate'] = pd.to_datetime(df['EndDate'])

def days_of_month(x):
    s = pd.date_range(*x, freq='D').to_series()
    return s.resample('M').count().rename(lambda x: x.month)

df1 = df[['StartDate', 'EndDate']].apply(days_of_month, axis=1).fillna(0)

df_final = df[['StartDate', 'EndDate']].join([df['StartDate'].dt.year.rename('Year'), df1])

最佳答案

试试这个:

df.join(df.dropna(axis=0,how='any')
         .apply(lambda x: pd.date_range(x['StartDate'],x['EndDate'], freq='D')
         .to_frame().resample('M').count().loc['2019-12-01':].unstack(), axis=1)[0].fillna(0))

输出:

 Id Item  StartDate    EndDate  2019-12-31 00:00:00  2020-01-31 00:00:00  2020-02-29 00:00:00  2020-03-31 00:00:00  2020-04-30 00:00:00  2020-05-31 00:00:00  2020-06-30 00:00:00  2020-07-31 00:00:00  2020-08-31 00:00:00
0  1    A 2019-12-10 2020-02-21                 22.0                 31.0                 21.0                  0.0                  0.0                  0.0                  0.0                  0.0                  0.0
1  2    B 2019-12-01 2020-01-01                 31.0                  1.0                  0.0                  0.0                  0.0                  0.0                  0.0                  0.0                  0.0
2  3    C 2019-10-01 2020-08-31                 31.0                 31.0                 29.0                 31.0                 30.0                 31.0                 30.0                 31.0                 31.0
3  4    D 2020-01-01 2020-01-30                  0.0                 30.0                  0.0                  0.0                  0.0                  0.0                  0.0                  0.0                  0.0
4  5    E 2019-03-01 2019-12-31                 31.0                  0.0                  0.0                  0.0                  0.0                  0.0                  0.0                  0.0                  0.0
5  6    F 2019-03-01 2019-12-31                 31.0                  0.0                  0.0                  0.0                  0.0                  0.0                  0.0                  0.0                  0.0
6  7    G 2019-10-01 2020-08-31                 31.0                 31.0                 29.0                 31.0                 30.0                 31.0                 30.0                 31.0                 31.0
7  8    H        NaT        NaT                  NaN                  NaN                  NaN                  NaN                  NaN                  NaN                  NaN                  NaN                  NaN

关于python 如何查找从 2019 年 12 月开始并在两个日期列之间向前推进的每个月的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59274898/

相关文章:

python - Django:如何创建排行榜

python - 如何使用 sklearn 提高决策树模型预测的准确性?

python - 在不知道名称的情况下导入文件

python - 为什么以下代码的执行时间不同(numpy 数组内部实际发生了什么)?

python - 如何用imshow()显示三个二维直方图的总和?

python - Glade 文件已打开但没有详细信息

python - 如何在 Odoo 12 中使用 Python XML-RPC 注册支付

python - Numpy:有效地将所有满足条件的像素设置为黑色

python - 如何在 Python 中检测一行的结尾

python - 想要比较两列中的字符串并在 python pandas 中的同一行中引入相等的值