时间冻结不适用于 pandas 时间戳

s5s*_*s5s 6 python pandas freezegun

我至少从 2015 年就注意到了这种行为,并且从那以后就没有改变。当使用 freezegun (或 pytest-freezegun)冻结测试中的时间时,datetime.datetime.now()返回冻结值 whilepd.Timestamp('now')pd.to_datetime('now')do not。有没有解决的办法?

例如:https: //pypi.org/project/pytest-freezegun/

wim*_*wim 0

freezegun不模拟 pandas 方法。有一个开放的拉取请求提供了该功能,但它已经在那里呆了将近一年了,所以我对此不抱希望。作为替代方案,您可以使用项目时间机器,它在较低级别上进行模拟,这会影响 pandas。

用法类似,context-manager:

with time_machine.travel("2019-10-15 15:58:10Z"):
    datetime.now()
    pd.Timestamp("now")
Run Code Online (Sandbox Code Playgroud)

或者装饰器:

@time_machine.travel("2019-10-15 15:58:10Z")
def foo():
    ...
Run Code Online (Sandbox Code Playgroud)