2

I am using .replace and .shift method to increment/decrement an arrow date. However the behaviour is completely unexpected. See the python session below as example.

>>> import arrow
>>> ref = arrow.get('2019-08-01', 'YYYY-MM-DD')
>>> ref.weekday()
3
>>> ref.day
1
>>> ref.shift(days=1)
<Arrow [2019-08-02T00:00:00+00:00]>
>>> ref.weekday()
3
>>> ref.day
1
>>> ref
<Arrow [2019-08-01T00:00:00+00:00]>
>>> 

After I shifted the arrow by one day I would expect weekday and day properties to be incremented. However they stay the same. Any explanation to this?

Using replace does the same thing.

krisfremen
  • 177
  • 1
  • 2
  • 13
Noel
  • 3,749
  • 3
  • 22
  • 21

1 Answers1

3

Try this: ref = ref.shift(days=1)

shift doesn't update the object, it returns an updated object...

C:\Users\deanv>python
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import arrow
>>> ref = arrow.get('2019-08-01', 'YYYY-MM-DD')
>>> ref.weekday()
3
>>> ref.day
1
>>> ref = ref.shift(days=1)
>>> ref.weekday()
4
>>> ref.day
2
>>>
Dean Van Greunen
  • 5,060
  • 2
  • 14
  • 28
  • 1
    oh yeah! As simple as that. Thanks Dean. – Noel Sep 07 '19 at 14:15
  • 1
    This is a general pattern followed by all Python built-ins, and most third party libraries; methods *either* mutate in place and return `None` (or some object of a different type for stuff like `dict.pop`) *or* return a new object of the same type adjusted for the mutation. They never do both. – ShadowRanger Sep 07 '19 at 14:17