【问题标题】:Why adding zero offset from initialisation is different from adding zero offset from substraction?为什么从初始化中添加零偏移与从减法中添加零偏移不同?
【发布时间】:2016-10-21 14:59:45
【问题描述】:
看例子:
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>
如您所见,添加零偏移量的结果是以分钟为单位,而添加秒然后减去它的结果是以秒为单位。
为什么不一样?减法技巧可靠吗?
【问题讨论】:
标签:
python
pandas
datetimeoffset
【解决方案1】:
ofs.Minute(1) + ofs.Second(1) 返回 second ofs 字符串表示形式。
然后你使用那个second ofs 并减去另一个second ofs,这通常会返回一个second ofs 字符串表示
作为一个例子,这样做会返回second,因为您没有先更改字符串表示的分钟数:
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>
【解决方案2】:
据我所知,1分钟有60秒
(ofs.Second(1) - ofs.Second(1)) == ofs.Second(0)
True
我会说,是的!很靠谱。
另外,请注意
ofs.Minute(1) + (ofs.Second(1) - ofs.Second(1))
<Minute>
那么字符串表示选择不是关联的。