为什么从初始化中添加零偏移量与从减法中添加零偏移量不同?

时间:2016-10-21 14:59:46

标签: python pandas datetimeoffset

看一下例子:

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>

正如您所看到的,添加零偏移的结果是以分钟为单位,同时添加秒然后减去它是以秒为单位。

为什么会有所不同?减法技巧可靠吗?

2 个答案:

答案 0 :(得分:2)

ofs.Minute(1) + ofs.Second(1)返回second个字符串表示。

然后你使用那​​个second ofs并减去另外second个,这通常会返回second个字符串表示

作为示例执行此操作会返回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>

答案 1 :(得分:1)

据我所教,1分钟内有60秒

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

True

我说,是的!它很可靠。

另外,请注意

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

<Minute>

然后字符串表示选择不是关联的。

相关问题