Skip to content

REGR: DateOffset attributes update fails with no errors. #36940

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
3 tasks done
lgelmi opened this issue Oct 7, 2020 · 4 comments · Fixed by #36946
Closed
3 tasks done

REGR: DateOffset attributes update fails with no errors. #36940

lgelmi opened this issue Oct 7, 2020 · 4 comments · Fixed by #36946
Labels
Datetime Datetime data dtype Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@lgelmi
Copy link

lgelmi commented Oct 7, 2020

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

  • 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.

Code Sample

import pandas

base = pandas.Timestamp("2020-01-01")
offset = pandas.DateOffset(years=1)
print(base + offset)
>>> 2021-01-01 00:00:00
print(offset)
>>> <DateOffset: years=1>
offset.years = 5
print(base + offset)
>>> 2021-01-01 00:00:00
print(offset)
>>> <DateOffset: years=5>

Problem description

I need to dynamically change some of the DateOffset attributes and just setting them seems like the most intuitive thing to do.
And it seems to work: no error is raised and the attribute value is updated.
When actually using it, though, the original value is used.

Expected Output

This is obviously due to the way the DateOffset is implemented, as a wrapper for cpython timedelta and dateutils relativedelta.
The ideal outcome for this would be having the object update itself, but I can understand why the class is "static".
The way it is, it may be better to inhibit attribute assignation instead of failing silently.

Event better would be having some method to generate a new DateOffset with just a couple of fields changed.
Depending on the change I may even be able to make the PR myself, once the desired behavior is clarified. :D

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2a7d332
python : 3.7.7.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-47-generic
Version : #51~18.04.1-Ubuntu SMP Sat Sep 5 14:35:50 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.1.2
numpy : 1.19.2
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 49.2.0.post20200714
Cython : None
pytest : 6.1.1
hypothesis : None
sphinx : 3.1.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@lgelmi lgelmi added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 7, 2020
@dsaxton
Copy link
Member

dsaxton commented Oct 7, 2020

This assignment was raising an error in 1.0.5

[ins] In [1]: import pandas as pd

[ins] In [2]: offset = pd.DateOffset(years=1)

[ins] In [3]: offset.years = 5
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-26637ee8202a> in <module>
----> 1 offset.years = 5

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

AttributeError: DateOffset objects are immutable.

[ins] In [4]: pd.__version__
Out[4]: '1.0.5'

and looks like this exception was removed in #34167. @jbrockmendel should this still be raising?

@dsaxton dsaxton added Regression Functionality that used to work in a prior pandas version Datetime Datetime data dtype and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 7, 2020
@dsaxton dsaxton changed the title BUG: DateOffset attributes update fails with no errors. REGR: DateOffset attributes update fails with no errors. Oct 7, 2020
@dsaxton dsaxton added this to the 1.1.4 milestone Oct 7, 2020
@lgelmi
Copy link
Author

lgelmi commented Oct 8, 2020

Would you like me to also add a PR for an "update" method which takes a dictionary of fields to be replaced and returns a new updated DateOffset?
I need to write it anyway for my application! :D

@dsaxton
Copy link
Member

dsaxton commented Oct 8, 2020

Thanks for the suggestion, but I don't we'd need to add such a method to pandas since that's really the function of the constructor.

@lgelmi
Copy link
Author

lgelmi commented Oct 8, 2020

Well, it would just be a factory method that calls the constructor without the user needing to know about the low level details of DateOffset, as I had to.

It occurred to me I may have explained myself poorly:
the resulting DateOffset would have all field configured as the original one, except those passed to the update method that would instead be replaced.

ten_years = pandas.DateOffset(years=10)
ten_years_in_march = ten_years.update(month=3)
print(pandas.Timestamp("2020-10-10") + ten_years_in_march)
>>> 2030-03-10 00:00:00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants