我想知道一条记录是否在pandas数据框中更新了日期。数据帧由几个列组成,其中对于A的每个值,我们都有几个值B的开始日期和结束日期。由于时间戳,我们可以知道是否有新记录或以前的记录已被修改。
我想知道的是,如何检查新记录的日期范围是否与其组中的其他记录(例如B1组(接近,如果它们的日期范围相似,则删除前一个记录,只更新新记录,但如果它没有可解释为新记录的通用范围。
例如,
输入数据帧:
A | B | 开始结束 | >时间戳|||
---|---|---|---|---|---|
A1 | B1 | 2021-05-10 00:00:00 | <2021-05-27>2021-05-15 00:00:00[/td> | ||
A1 | B1 | 2021-05-12 00:00:00 | 2021:05-30 00:00:00 | 2021-04-15 00:00:00[/td> | |
A1 | B1 | 2021-05-10 00:00:00 | 2021:05-12 00:00:00 | 2021-03-15 00:00:00[/td> | |
A1 | B2 | 2021-06-02 00:00:00 | 2021:06-04 00:00:00 | 2021-02-15 00:00:00[/td> | |
A2 | B3 | 2021-01-01 00:00:00 | 2022-01-01 00:00:00 | >2021-05-15 00:00:00 | |
A2 | B3 | 2021-07-15 00:00:00 | 2021:08-15 00:00:000 | >2021-04-15 00:00:00 | |
A2 | B4 | 2021-05-30 00:00:00 | 2021:06-15 00:00:00 | 2021-05-15 00:00:000 | |
A2 | B4 | 2021-06-02 00:00:00 | 2021:06-17 00:00:00 | 2021-04-15 00:00:00[/td> |
我不确定"关闭"日期范围的确切含义,所以这个答案与问题中列出的输出不完全匹配。
出于演示目的,我制作了一个名为data.csv
的csv文件,其中包含您问题中的数据
A,B,Start,End,Timestamp
A1,B1,2021-05-10 00:00:00,2021-05-27 00:00:00,2021-05-15 00:00:00
A1,B1,2021-05-12 00:00:00,2021-05-30 00:00:00,2021-04-15 00:00:00
A1,B1,2021-05-10 00:00:00,2021-05-12 00:00:00,2021-03-15 00:00:00
A1,B2,2021-06-02 00:00:00,2021-06-04 00:00:00,2021-02-15 00:00:00
A2,B3,2021-01-01 00:00:00,2022-01-01 00:00:00,2021-05-15 00:00:00
A2,B3,2021-07-15 00:00:00,2021-08-15 00:00:00,2021-04-15 00:00:00
A2,B4,2021-05-30 00:00:00,2021-06-15 00:00:00,2021-05-15 00:00:00
A2,B4,2021-06-02 00:00:00,2021-06-17 00:00:00,2021-04-15 00:00:00
一种方法可以是在B
列中比较每组的时间差异。我们将从您在问题中提到的一组开始,即B
列值等于"B1"
:
import pandas as pd
df = pd.read_csv("data.csv")
dff = df[df["B"] == "B1"]
>>> dff
A B ... End Timestamp
0 A1 B1 ... 2021-05-27 00:00:00 2021-05-15 00:00:00
1 A1 B1 ... 2021-05-30 00:00:00 2021-04-15 00:00:00
2 A1 B1 ... 2021-05-12 00:00:00 2021-03-15 00:00:00
# Difference in number of days between start and end date
>>> (pd.to_datetime(dff.End) - pd.to_datetime(dff.Start)).dt.days
0 17
1 18
2 2
dtype: int64
# How does each time difference compare to the time difference in the first row
>>> (pd.to_datetime(dff.End) - pd.to_datetime(dff.Start)).dt.days.diff().fillna(0)
0 0.0
1 1.0
2 -16.0
dtype: float64
# Filter where the number of days difference compared to the first row is less than 7
>>> abs((pd.to_datetime(dff.End) - pd.to_datetime(dff.Start)).dt.days.diff().fillna(0)) < 7
0 True
1 True
2 False
dtype: bool
# Filter dff based on earlier condition
>>> dff[abs((pd.to_datetime(dff.End) - pd.to_datetime(dff.Start)).dt.days.diff().fillna(0)) < 7]
A B Start End Timestamp
0 A1 B1 2021-05-10 00:00:00 2021-05-27 00:00:00 2021-05-15 00:00:00
1 A1 B1 2021-05-12 00:00:00 2021-05-30 00:00:00 2021-04-15 00:00:00
上面我们只比较了B
列的一组。要对所有组执行上面所做的操作,我们可以在B
列上使用groupby
。然后我们可以遍历每个组,并使用前面提到的过滤器过滤每个组。过滤完所有组后,这些过滤后的组可以包含在列表中并连接在一起。
df = pd.concat([
group[
abs(
(pd.to_datetime(group.End) - pd.to_datetime(group.Start))
.dt.days.diff()
.fillna(0)
)
< 7
]
for name, group in df.groupby("B")
])
>>> df
A B Start End Timestamp
0 A1 B1 2021-05-10 00:00:00 2021-05-27 00:00:00 2021-05-15 00:00:00
1 A1 B1 2021-05-12 00:00:00 2021-05-30 00:00:00 2021-04-15 00:00:00
3 A1 B2 2021-06-02 00:00:00 2021-06-04 00:00:00 2021-02-15 00:00:00
4 A2 B3 2021-01-01 00:00:00 2022-01-01 00:00:00 2021-05-15 00:00:00
6 A2 B4 2021-05-30 00:00:00 2021-06-15 00:00:00 2021-05-15 00:00:00
7 A2 B4 2021-06-02 00:00:00 2021-06-17 00:00:00 2021-04-15 00:00:00
根据你的需要调整亲密度。我用这里的天数来衡量,但你可以用不同的天数。您可以使用秒、微秒、纳秒等…查看Series
文档以获取更多示例。