freezegun icon indicating copy to clipboard operation
freezegun copied to clipboard

pd.Timestamp.now() not mocked

Open jluttine opened this issue 6 years ago • 8 comments

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

jluttine avatar May 20 '19 14:05 jluttine

Do you know if pandas relies on one of the python built-in datetime calls?

spulec avatar Jul 09 '19 03:07 spulec

No, I have no knowledge of that.

jluttine avatar Jul 09 '19 08:07 jluttine

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))

Wim-De-Clercq avatar Sep 11 '19 09:09 Wim-De-Clercq

+1 on this issue

rileypeterson avatar Nov 27 '19 01:11 rileypeterson

This works in time-machine: https://pypi.org/project/time-machine/

(Built as an alternative to freezegun, history post)

adamchainz avatar Nov 28 '21 21:11 adamchainz

@adamchainz you saved my bacon, how are you?

jamespacileo avatar May 05 '22 11:05 jamespacileo

Hey, It would be cool this feature could be added to freezegun! Are you taking any Pull Requests ?

federicober avatar Jul 01 '22 08:07 federicober

In principle yes. Mocking pandas seems like it would be another thing or should be opt in, due to performance implications.

boxed avatar Jul 01 '22 10:07 boxed