Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MartinFalatic opened this issue Mar 14, 2019 · 3 comments
Closed
Labels
Bug Datetime Datetime data dtype Frequency DateOffsets Timezones Timezone data dtype

Comments

@MartinFalatic
Copy link

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
@mroeschke
Copy link
Member

Hey Marty :)

Looks like this change broke the prior behavior; it assumed all incoming subclasses would be Timestamps here.

be14333#diff-66d289312fd0b02e7721bf45fb281797R598

@mroeschke mroeschke added Bug Datetime Datetime data dtype Frequency DateOffsets Timezones Timezone data dtype labels Mar 14, 2019
@ArtificialQualia
Copy link
Contributor

I have a proposed fix for this, I'll submit a PR tomorrow

@ArtificialQualia
Copy link
Contributor

#25853 closes this @mroeschke

@jreback jreback closed this as completed Apr 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Frequency DateOffsets Timezones Timezone data dtype
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants