Skip to content

BUG: DateOffset pickle bug when months=12 #34511

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
1 task
jjbarton opened this issue Jun 1, 2020 · 2 comments · Fixed by #35258
Closed
1 task

BUG: DateOffset pickle bug when months=12 #34511

jjbarton opened this issue Jun 1, 2020 · 2 comments · Fixed by #35258
Labels
Bug Frequency DateOffsets IO Pickle read_pickle, to_pickle
Milestone

Comments

@jjbarton
Copy link

jjbarton commented Jun 1, 2020

  • [ x] I have checked that this issue has not already been reported.

  • [ x] I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pickle
import pandas as pd

offset = pd.DateOffset(months=12)

pickle.dump(offset, open("/tmp/o.p", "wb"))
loaded = pickle.load(open("/tmp/o.p", "rb"))
print(loaded)

# <DateOffset: months=12, years=1>

Problem description

If you pickle a pandas DateOffset instance with months=12, and then unpickle it, you have a DateOffset(months=12, years=1) instance (effectively a 2 year date offset object.

Arguably one shouldn't create a 12 month date offset in preference to a 1 year offset, but this is still bad behaviour imho.

Expected Output

print(loaded)

<DateOffset: months=12>

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.3.final.0
python-bits : 64
OS : Darwin
OS-release : 18.7.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.0.3
numpy : 1.18.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.4.0.post20200518
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@jjbarton jjbarton added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 1, 2020
@fujiaxiang
Copy link
Member

Tested on latest master:

>>> pd.__version__
'1.1.0.dev0+1730.geac62cf26'

>>> offset = pd.DateOffset(months=12)
>>> pickle.dump(offset, open("test_date_offset.p", "wb"))
>>> loaded = pickle.load(open("test_date_offset.p", "rb"))

# The two objects appears different after __str__
>>> loaded
<DateOffset: months=12, years=1>
>>> offset
<DateOffset: months=12>

# They are actually equivalent when put in calculations
>>> pd.to_datetime('2020-01-01') + offset
Timestamp('2021-01-01 00:00:00')
>>> pd.to_datetime('2020-01-01') + loaded
Timestamp('2021-01-01 00:00:00')

We can see although these two objects appears different, but their values are equivalent in computation.

However, if we explicitly set months=12, years=1, we get a different value.

>>> d1 = pd.DateOffset(months=12)
>>> d2 = pd.DateOffset(months=12, years=1)
>>> d1
<DateOffset: months=12>
>>> d2
<DateOffset: months=12, years=1>
>>> pd.to_datetime('2020-01-01') + d1
Timestamp('2021-01-01 00:00:00')
>>> pd.to_datetime('2020-01-01') + d2
Timestamp('2022-01-01 00:00:00')

This is the weird part.

>>> offset == loaded
False
>>> loaded == d2
True
>>> pd.to_datetime('2020-01-01') + loaded == pd.to_datetime('2020-01-01') + d2
False

Here's a look at their attributes:

>>> offset.months
12
>>> loaded.months
12

# The original object did not have `years` attribute, but the loaded one does
>>> offset.years
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'DateOffset' object has no attribute 'years'
>>> loaded.years
1

>>> d2.months
12
>>> d2.years
1

Looks like we have some problems with the internal representations of DateOffset object. It feels like there are two separate sets of representations that are not consistent. One set is used to do the computations (i.e apply the offsets on datetime like objects). Another set is used to print and to determine if 2 objects are "equal".

@fujiaxiang fujiaxiang added Frequency DateOffsets IO Pickle read_pickle, to_pickle and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 1, 2020
@fujiaxiang
Copy link
Member

@jjbarton Would you be interested to have a look and PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Frequency DateOffsets IO Pickle read_pickle, to_pickle
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants