Skip to content
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

Unable to change normalize property of objects from to_offset() #27728

Closed
villebro opened this issue Aug 3, 2019 · 2 comments
Closed

Unable to change normalize property of objects from to_offset() #27728

villebro opened this issue Aug 3, 2019 · 2 comments

Comments

@villebro
Copy link

villebro commented Aug 3, 2019

Code Sample, a copy-pastable example if possible

from datetime import date

from pandas.tseries.frequencies import to_offset
from pandas.tseries.offsets import DateOffset


dt = date.today()
offset = to_offset("52W")
print(f"type: {type(offset)}\ndict: {offset.__dict__}")
print(f"{dt} -> {dt + offset}")

try:
    offset.normalize = True
except AttributeError as ex:
    print(f"\n{ex}")

offset = DateOffset(normalize=True, **offset.kwds)
print(f"\ntype: {type(offset)}\ndict: {offset.__dict__}")
print(f"{dt} -> {dt + offset}")

Yields

type: <class 'pandas.tseries.offsets.Week'>
dict: {'n': 52, 'normalize': False, '_cache': {}, 'weekday': 6}
2019-08-03 -> 2020-07-26 00:00:00

DateOffset objects are immutable.

type: <class 'pandas.tseries.offsets.DateOffset'>
dict: {'n': 1, 'normalize': True, '_cache': {}, '_offset': relativedelta(weekday=SU), '_use_relativedelta': True, 'weekday': 6}
2019-08-03 -> 2019-08-04 00:00:00

Problem description

Since Pandas 0.24 the DateOffset objects are immutable. Previously it was possible to change the normalize property after receiving an object from to_offset(), but now there doesn't seem to be any mechanism for doing this. While it seems possible to create a new instance of DateOffset with normalize = True, the type of the object changes and doesn't work correctly.

What is the best way of getting an object from to_offset() with normalize set to True? From an end-user perspective, the best solution would probably be to add a normalize attribute to the function, like so:

def to_offset(freq, normalize=False):

Output of pd.show_versions()

INSTALLED VERSIONS

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

pandas : 0.25.0
numpy : 1.17.0
pytz : 2019.2
dateutil : 2.8.0
pip : 19.2.1
setuptools : 41.0.1
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
s3fs : None
scipy : None
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

@jbrockmendel
Copy link
Member

Try type(obj)(obj.n, normalize=True, **obj.kwds)

This can/should be better documented.

@villebro
Copy link
Author

villebro commented Aug 3, 2019

Thanks, worked like a charm! 👍 Can try to put through a doc PR if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants