6

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/

s5s
  • 11,159
  • 21
  • 74
  • 121

1 Answers1

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():
    ...
wim
  • 338,267
  • 99
  • 616
  • 750