如何向时间戳添加均匀的随机噪声?

How do I add uniform random noise to a timestamp?

我正在尝试通过向 datetime 值添加均匀的随机噪声来掩盖 datetime 值。

我目前使用 Cape Python 模块为我的数据添加噪声。但是,我想开发自己的自定义函数,类似于下面由 Cape Python 提供的函数。

!pip install cape-privacy
from cape_privacy.pandas.transformations import *
import datetime

s = pd.Series([datetime.date(year=2020, month=2, day=15)])
perturb = DatePerturbation(frequency="MONTH", min=-10, max=10)
perturb(s)
# Returns 2019-07-20

有什么方法可以将噪声(在 minmax 之间)添加到 DAYMONTHYEAR 或它们的组合前面提到的,给定一个日期时间值并使其看起来可信?

# Input
2021-09-23

# Expected Output when noise is added to DAY between -10 and 10
2021-09-20

我不认识海角Python,所以这可能不对...

这是一种方法:

from datetime import date, timedelta
from random import randint

def date_pertubation(d, attribs, minimum, maximum):
    if isinstance(attribs, str):
        attribs = [attribs]
    attribs = [attrib.casefold() for attrib in attribs]
    year = d.year
    if "year" in attribs:
        year += randint(minimum, maximum)
    month = d.month - 1
    if "month" in attribs:
        month += randint(minimum, maximum)
        year_delta, month = divmod(month, 12)
        year += year_delta
    month += 1
    day_delta = d.day - 1
    if "day" in attribs:
        day_delta += randint(minimum, maximum)
    
    return date(year, month, 1) + timedelta(days=day_delta)

这个

d = date(year=2020, month=2, day=15)
for _ in range(5):
    print(date_pertubation(d, "DAY", -20, 20).strftime("%Y-%m-%d"))
for _ in range(5):
    print(date_pertubation(d, "YEAR", -3, 3).strftime("%Y-%m-%d"))
for _ in range(5):
    print(date_pertubation(d, ["YEAR", "MONTH", "DAY"], -3, 3).strftime("%Y-%m-%d"))

会产生类似

的东西
2020-02-11
2020-02-09
2020-01-29
2020-02-29
2020-03-01

2022-02-15
2022-02-15
2020-02-15
2017-02-15
2017-02-15

2016-12-12
2016-12-14
2021-01-13
2019-11-15
2021-05-14