freezegun
freezegun copied to clipboard
pd.Timestamp.now() not mocked
Code for replication:
import datetime
import pandas as pd
import freezegun
with freezegun.freeze_time("2011-01-01"):
print(pd.Timestamp.now())
print(datetime.datetime.now())
Expected output:
2011-01-01 00:00:00
2011-01-01 00:00:00
Actual output:
2019-05-20 17:14:03.781282
2011-01-01 00:00:00
Version information:
freezegun==0.3.11
pandas==0.24.1
Do you know if pandas relies on one of the python built-in datetime calls?
No, I have no knowledge of that.
I think they get it here:
https://github.com/pandas-dev/pandas/blob/18db7fba591585352f342f77d3aaf56d9828f7ed/pandas/_libs/tslibs/timestamps.pyx#L264
from cpython.datetime cimport (datetime,
PyTZInfo_Check, PyDateTime_IMPORT)
# ...<other code>...
@classmethod
def now(cls, tz=None):
"""
Timestamp.now(tz=None)
Return new Timestamp object representing current time local to
tz.
Parameters
----------
tz : str or timezone object, default None
Timezone to localize to
"""
if isinstance(tz, str):
tz = maybe_get_tz(tz)
return cls(datetime.now(tz))
+1 on this issue
This works in time-machine: https://pypi.org/project/time-machine/
(Built as an alternative to freezegun, history post)
@adamchainz you saved my bacon, how are you?
Hey, It would be cool this feature could be added to freezegun! Are you taking any Pull Requests ?
In principle yes. Mocking pandas seems like it would be another thing or should be opt in, due to performance implications.