Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why adding zero offset from initialisation is different from adding zero offset from substraction?

Look at the example:

In [1]: import pandas.tseries.offsets as ofs

In [2]: ofs.Minute(1) + ofs.Second(1)
Out[2]: <61 * Seconds>

In [3]: ofs.Second(0)
Out[3]: <0 * Seconds>

In [4]: ofs.Minute(1) + ofs.Second(0)
Out[4]: <Minute>

In [5]: 0*ofs.Second(1)
Out[5]: <0 * Seconds>

In [6]: ofs.Minute(1) + 0*ofs.Second(1)
Out[6]: <Minute>

In [7]: ofs.Minute(1) + ofs.Second(1) - ofs.Second(1)
Out[7]: <60 * Seconds>

As you can see, the result of adding a zero offset is in Minutes while adding a Second and then substract it is in Seconds.

Why is it different? Is the substraction trick reliable?

like image 583
PhML Avatar asked Jul 02 '26 20:07

PhML


2 Answers

ofs.Minute(1) + ofs.Second(1) returns a second ofs string representation.

then you use that second ofs and substract another second ofs which will normally return a second ofs string representation

as an example doing this would return second because you don't change the minute ofst string representation first:

ofs.Minute(1) + ofs.Second(1) - ofs.Second(1)
Out[35]: <60 * Seconds>

ofs.Minute(1) + (ofs.Second(1) - ofs.Second(1))
Out[36]: <Minute>
like image 100
Steven G Avatar answered Jul 05 '26 10:07

Steven G


As far as I've been taught, there are 60 seconds in 1 minute

(ofs.Second(1) - ofs.Second(1)) == ofs.Second(0)

True

I'd say, yes! It's reliable.


Also, notice

ofs.Minute(1) + (ofs.Second(1) - ofs.Second(1))

<Minute>

Then string representation choice is not associative.

like image 38
piRSquared Avatar answered Jul 05 '26 09:07

piRSquared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!