Python 为什么从初始化添加零偏移与从减法添加零偏移不同?
看看这个例子:Python 为什么从初始化添加零偏移与从减法添加零偏移不同?,python,pandas,datetimeoffset,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.S
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>
[1]中的:导入pandas.tseries.offset作为ofs
在[2]中:ofs.分(1)+ofs.秒(1)
出[2]:
在[3]中:秒(0)
出[3]:
在[4]中:ofs.分(1)+ofs.秒(0)
出[4]:
[5]:0*ofs.秒(1)
出[5]:
在[6]中:ofs.分(1)+0*ofs.秒(1)
出[6]:
在[7]中:ofs.分(1)+ofs.秒(1)-ofs.秒(1)
出[7]:
如您所见,添加零偏移量的结果以分钟为单位,而添加秒偏移量的结果以秒为单位
为什么不同?减法技巧可靠吗?据我所知,一分钟内有60秒
(ofs.Second(1) - ofs.Second(1)) == ofs.Second(0)
True
我会说,是的!这是可靠的
另外,请注意
ofs.Minute(1) + (ofs.Second(1) - ofs.Second(1))
<Minute>
ofs.分(1)+(ofs.秒(1)-ofs.秒(1))
那么字符串表示选择是非关联的。ofs.Minute(1)+ofs.Second(1)
返回一个Second
ofs字符串表示
然后,使用该second
ofs并减去另一个second
ofs,该ofs通常将返回一个second
ofs字符串表示形式
例如,执行此操作将返回second
,因为您不会首先更改分钟ofst字符串表示:
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>
ofs.Minute(1)+ofs.Second(1)-ofs.Second(1)
出[35]:
ofs分(1)+(ofs秒(1)-ofs秒(1))
出[36]: