看例子:
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>
如您所见,添加一个零偏移量的结果以分钟为单位,而添加一个秒然后减去它以秒为单位。
为什么不一样?减法技巧可靠吗?
最佳答案
ofs.Minute(1) + ofs.Second(1)
返回 second
ofs 字符串表示。
然后您使用那个 second
ofs 并减去另一个 second
ofs 这通常会返回 second
ofs 字符串表示
作为一个例子,这样做会返回 second
,因为您没有首先更改 st 字符串表示的分钟:
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>
关于python - 为什么从初始化中添加零偏移量与从减法中添加零偏移量不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40179892/