time freezing doesn't work with pandas timestamps
Asked Answered
A

1

7

I've noticed this behaviour since at least 2015 and it hasn't changed since. When freezegun (or pytest-freezegun) is used to freeze time in a test, datetime.datetime.now() returns the frozen value while pd.Timestamp('now') and pd.to_datetime('now') do not. Is there a way around this?

For example: https://pypi.org/project/pytest-freezegun/

Abecedarium answered 15/10, 2019 at 15:58 Comment(2)
Hey s5s, did you ever find out how to circumvent this?Hinton
@Hinton Late answer postedTheisen
T
0

freezegun does not mock pandas methods. There is an open pull-request providing that functionality, but it has been sitting there for almost a year now, so I'm not hopeful for it. As an alternative, you could use the project time-machine which mocks at a lower level, in a way which will affect pandas.

The usage is similar, context-manager:

with time_machine.travel("2019-10-15 15:58:10Z"):
    datetime.now()
    pd.Timestamp("now")

Or decorator:

@time_machine.travel("2019-10-15 15:58:10Z")
def foo():
    ...
Theisen answered 20/10, 2022 at 2:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.