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

标签 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>

如您所见,添加一个零偏移量的结果以分钟为单位,而添加一个秒然后减去它以秒为单位。

为什么不一样?减法技巧可靠吗?

最佳答案

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/

相关文章:

python - 在python3中优雅地退出多进程

python - 如何用python获取选择区域坐标?

java - 将 python 与更快的语言混合以在 GAE 中进行优化

python - Pyspark - 多列聚合

python - 格式化 Pandas .groupby.size() 的输出

c# - DateTimeOffset,理解不清楚

ruby-on-rails - 带时区的Strptime

python - 如何在 Pandas 数据框中使用带有多个表达式的 str.contains()?

python - 从 excel 到 pandas df 的空单元格

QuickBooks Web 连接器 TimeModified 偏移量不考虑夏令时