用日期范围填充行的 Pythonic 方法

标签 python python-3.x pandas date dataframe

我正在处理一个问题陈述,它要求我填写缺失日期的行(即 pandas 数据框列中两个日期之间的日期)。请看下面的例子。我将 Pandas 用于我当前的方法(如下所述)。

输入数据示例(大约有 25000 行):

A  | B  | C  | Date1    | Date2
a1 | b1 | c1 | 1Jan1990 | 15Aug1990 <- this row should be repeated for all dates between the two dates
.......................
a3 | b3 | c3 | 11May1986 | 11May1986 <- this row should NOT be repeated. Just 1 entry since both dates are same.
.......................
a5 | b5 | c5 | 1Dec1984 | 31Dec2017 <- this row should be repeated for all dates between the two dates
..........................
..........................

预期输出:

A  | B  | C  | Month    | Year
a1 | b1 | c1 | 1        | 1990  <- Since date 1 column for this row was Jan 1990
a1 | b1 | c1 | 2        | 1990    
.......................
.......................
a1 | b1 | c1 | 7        | 1990  
a1 | b1 | c1 | 8        | 1990  <- Since date 2 column for this row was Aug 1990
..........................
a3 | b3 | c3 | 5        | 1986  <- only 1 row since two dates in input dataframe were same for this row.
...........................
a5 | b5 | c5 | 12       | 1984 <- since date 1 column for this row was Dec 1984
a5 | b5 | c5 | 1        | 1985 
..........................
..........................
a5 | b5 | c5 | 11       | 2017 
a5 | b5 | c5 | 12       | 2017 <- Since date 2 column for this row was Dec 2017

我知道实现此目的的更传统方法(我目前的方法):

  • 遍历每一行。
  • 获取两个日期列之间的天数差异。
  • 如果两列中的日期相同,则只在输出数据框中包含该月和年的一行
  • 如果日期不同(diff > 0),则获取每个日期差异行的所有(月、年)组合并附加到新数据框

由于输入数据有大约 25000 行,我相信输出数据会非常非常大,所以我正在寻找更多的Pythonic 方式来实现这个(如果可能并且比迭代方法更快)!

最佳答案

在我看来,这里使用的最佳工具是 PeriodIndex(用于生成日期之间的月份和年份)。

但是,PeriodIndex 一次只能对一行进行操作。所以,如果我们要去 要使用 PeriodIndex,每一行都必须单独处理。那 不幸的是意味着循环遍历 数据框:

import pandas as pd
df = pd.DataFrame([('a1','b1','c1','1Jan1990','15Aug1990'),
                   ('a3','b3','c3','11May1986','11May1986'),
                   ('a5','b5','c5','1Dec1984','31Dec2017')],
                  columns=['A','B','C','Date1','Date2'])

result = [] 
for tup in df.itertuples():
    index = pd.PeriodIndex(start=tup.Date1, end=tup.Date2, freq='M')
    new_df = pd.DataFrame([(tup.A, tup.B, tup.C)], index=index)
    new_df['Month'] = new_df.index.month
    new_df['Year'] = new_df.index.year
    result.append(new_df)
result = pd.concat(result, axis=0)
print(result)

产量

          0   1   2  Month  Year
1990-01  a1  b1  c1      1  1990    <--- Beginning of row 1
1990-02  a1  b1  c1      2  1990
1990-03  a1  b1  c1      3  1990
1990-04  a1  b1  c1      4  1990
1990-05  a1  b1  c1      5  1990
1990-06  a1  b1  c1      6  1990
1990-07  a1  b1  c1      7  1990
1990-08  a1  b1  c1      8  1990    <--- End of row 1
1986-05  a3  b3  c3      5  1986    <--- Beginning and End of row 2
1984-12  a5  b5  c5     12  1984    <--- Beginning row 3
1985-01  a5  b5  c5      1  1985
1985-02  a5  b5  c5      2  1985
1985-03  a5  b5  c5      3  1985
1985-04  a5  b5  c5      4  1985
...      ..  ..  ..    ...   ...
2017-09  a5  b5  c5      9  2017
2017-10  a5  b5  c5     10  2017
2017-11  a5  b5  c5     11  2017
2017-12  a5  b5  c5     12  2017    <--- End of row 3

[406 rows x 5 columns]

请注意,您可能真的不需要定义 MonthYear

new_df['Month'] = new_df.index.month
new_df['Year'] = new_df.index.year

因为您已经有了 PeriodIndex,这使得计算月份和年份变得非常容易。

关于用日期范围填充行的 Pythonic 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53780270/

相关文章:

python - 如何使用 pandas 旋转分组对象

python - python 中的复合函数 - 对偶组合

python - 将 while 循环添加到 try 和 except 语句

将数据写入csv的Python程序取决于列是否存在

python - 如何在距它 x 距离处找到最接近给定数字的 n 个数字?

Python:变长元组

python - 在 Pandas 中舍入一列

python - 导入 Numexpr Python 时如何修复错误

python - 剧本提前结束?

python - 如何将 QComboBox 中的文本设置为居中对齐而不使其在 PyQt 中可编辑