Skip to content

Pandas 0.24.x throws an error when using freezegun with CustomBusinessDay #25734

Closed
@MartinFalatic

Description

@MartinFalatic

I originally opened an issue with Freezegun:
spulec/freezegun#284

However, this appears to be a new quirk of Pandas 0.24's CustomBusinessDay class and its use of pandas._libs.tslibs.conversion.localize_pydatetime(). Other offset functions don't seem to be affected, for the ones I've tried.

Everything worked just fine with Pandas 0.23.4 and earlier, with the latest freezegun-0.3.11.

Code Sample, a copy-pastable example if possible

Test setup: Python 2.7 and 3.6 on OS X using freezegun-0.3.11 with pandas-0.24.2

import pytz
import pandas
from datetime import datetime
from pandas.tseries.offsets import (CustomBusinessDay, BusinessDay)
from freezegun import freeze_time

LOCAL_TZ = pytz.timezone('America/Los_Angeles')

@freeze_time(LOCAL_TZ.localize(datetime(2019, 3, 15, 0, 0, 0)))
def these_work():
    pandas.Timestamp(datetime.now(LOCAL_TZ))
    pandas.Timestamp(datetime.now(LOCAL_TZ)) + BusinessDay(n=1)

@freeze_time(LOCAL_TZ.localize(datetime(2019, 3, 15, 0, 0, 0)))
def this_fails():
    pandas.Timestamp(datetime.now(LOCAL_TZ)) + CustomBusinessDay(n=1)

these_work()
this_fails()

Problem description

Failure mode:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-452eb6a5f720> in <module>
     17
     18 these_work()
---> 19 this_fails()

~/.virtualenvs/python3/lib/python3.6/site-packages/freezegun/api.py in wrapper(*args, **kwargs)
    656                     result = func(time_factory, *args, **kwargs)
    657                 else:
--> 658                     result = func(*args, **kwargs)
    659             return result
    660         functools.update_wrapper(wrapper, func)

<ipython-input-2-452eb6a5f720> in this_fails()
     14 @freeze_time(LOCAL_TZ.localize(datetime(2019, 3, 15, 0, 0, 0)))
     15 def this_fails():
---> 16     pandas.Timestamp(datetime.now(LOCAL_TZ)) + CustomBusinessDay(n=1)
     17
     18 these_work()

pandas/_libs/tslibs/offsets.pyx in pandas._libs.tslibs.offsets.BaseOffset.__radd__()

pandas/_libs/tslibs/offsets.pyx in pandas._libs.tslibs.offsets._BaseOffset.__add__()

~/.virtualenvs/python3/lib/python3.6/site-packages/pandas/tseries/offsets.py in wrapper(self, other)
     70
     71             if self._adjust_dst:
---> 72                 result = conversion.localize_pydatetime(result, tz)
     73
     74             result = Timestamp(result)

pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.localize_pydatetime()

pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.localize_pydatetime()

AttributeError: 'FakeDatetime' object has no attribute 'tz_localize'

Expected Output

All of these should work equally well, including CustomBusinessDay.

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.8.final.0
python-bits: 64
OS: Darwin
OS-release: 18.2.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.24.2
pytest: None
pip: 19.0.3
setuptools: 40.6.3
Cython: 0.29.3
numpy: 1.16.0
scipy: 1.2.0
pyarrow: None
xarray: None
IPython: 7.2.0
sphinx: 1.8.3
patsy: 0.5.1
dateutil: 2.7.5
pytz: 2018.9
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: 4.3.2
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions