question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DateOffset.offset vs DateOffset._offset

See original GitHub issue

tseries.offsets.DateOffset has a _offset attribute. Many subclasses do not have this attribute, but do have a offset attribute. The main difference in the two appears to be that _offset is ignored by __eq__ (see #17137).

The question: why two separate attributes? Is this intentional or coincidence?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jbrockmendelcommented, Aug 27, 2017

There are some inconsistencies in how offsets handles kwds that need to be cleaned up if we want offsets to be immutable. I have not found a way to clean them up that maintains backward compatibility in extremely weird corner cases. Take BusinessDay as an example:

class BusinessDay(SingleConstructorOffset):
    def __init__(self, n=1, normalize=False, **kwds):
        self.n = int(n)
        self.normalize = normalize
        self.kwds = kwds
        self.offset = kwds.get('offset', timedelta(0))

I’d like to get a green-light to change this behavior:

In [1]: from datetime import timedelta
In [2]: from pandas.tseries.offsets import *
In [3]: bd = BusinessDay()
In [4]: bd2 = BusinessDay(offset=timedelta(0))

In [5]: bd
Out[5]: <BusinessDay>

In [6]: bd == bd2
Out[6]: True

In [7]: bd.kwds
Out[7]: {}

In [8]: bd2.kwds
Out[8]: {'offset': datetime.timedelta(0)}

In [9]: bd - bd2
Out[9]: <0 * BusinessDays>

In [10]: bd2 - bd
Out[10]: <0 * BusinessDays>

In [11]: (bd - bd2).kwds
Out[11]: {}

In [12]: (bd2 - bd).kwds
Out[12]: {'offset': datetime.timedelta(0)}

Similar behavior shows up in the other BusinessFooOffsets, Week, and QuarterOffset. Many of the others will display unwanted behavior if users pass unsupported keyword args:

In [33]: sms = SemiMonthBegin(clark='kent')
In [34]: sms
Out[34]: <SemiMonthBegin: day_of_month=15, kwds={'clark': 'kent'}>

Best case I’d like to get rid of DateOffset.kwds altogether and explicitly specify the args/kwargs in the various __init__ methods (this will require patching a few other methods that check self.kwds, but that isn’t a big deal). That will break backward compat for any users who-- for some reason-- explicitly use that attribute.

If that is too big a break, the next-best option would be to

  1. add validation in __init__ that only supported kwargs have been passed (this is done in a few existing code paths). This will take care of the SemiMonthBegin(clark='kent') cases.

  2. ensure that default values for kwds are handled consistently. e.g. in the BusinessDay.__init__ example above, kwds.get('offset', timedelta(0)) would be changed to kwds.setdefault('offset', timedelta(0)).

0reactions
jrebackcommented, Sep 9, 2017

yep those look wrong

why don’t do a PR just to fix those

Read more comments on GitHub >

github_iconTop Results From Across the Web

pandas.tseries.offsets.DateOffset
Each offset specify a set of dates that conform to the DateOffset. For example, Bday defines this set to be the set of...
Read more >
Python | Pandas tseries.offsets.DateOffset - GeeksforGeeks
DateOffsets work as follows, each offset specify a set of dates that conform to the DateOffset. For example, Bday defines this set to...
Read more >
Pandas DateOffset, step back one day - Stack Overflow
You can check pandas.tseries.offsets.DateOffset : *kwds Temporal parameter that add to or replace the offset value. Parameters that add to the offset (like ......
Read more >
Converting between DateTime and DateTimeOffset
DateTimeOffset reflects a time's offset from UTC, but it doesn't reflect the actual time zone to which that offset belongs. For more information ......
Read more >
8 DateOffset objects — Pandas Doc - GitHub Pages
Offsets can be used with either a Series or DatetimeIndex to apply the offset to each element. In [29]: rng = pd.date_range( ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found