[英]Why does pd.Timedelta modulo int work, but not plus int?
One can use modulo on Pandas Timedeltas with any number, but addition results in an unsupported error.可以在 Pandas Timedeltas 上使用任意数字的模数,但加法会导致不支持的错误。 The following example
下面的例子
import pandas as pd
pd.to_timedelta(7, "s") % 3
pd.to_timedelta(7, "s") + 3
results in结果是
TypeError Traceback (most recent call last)
<ipython-input-2-edd04d4a1971> in <module>
3 pd.to_timedelta(7, "s") % 3
4
----> 5 pd.to_timedelta(7, "s") + 3
TypeError: unsupported operand type(s) for +: 'Timedelta' and 'int'
Is there a deeper reason for this?这有更深层次的原因吗?
you can do it only if you show which part of datetime you want to use, for example:仅当您显示要使用的日期时间的哪一部分时才能执行此操作,例如:
time=pd.to_timedelta(7, "s")
print(time)
0 days 00:00:07
print(time.seconds%3)
1
You cannot divide using different types您不能使用不同的类型进行划分
Looking at the code for the Timedelta object , we can see that the __mod__
function just calls the __divmod__
function, and returns the second value;查看Timedelta 对象的代码,我们可以看到
__mod__
函数只是调用了__divmod__
函数,并返回了第二个值; which looks like the following:如下所示:
def __divmod__(self, other):
# Naive implementation, room for optimization
div = self // other
return div, self - div * other
So %
will work with anything that can divide a Timedelta
object - we can see what __floordiv__
accepts from the implementation in the same link.所以
%
可以处理任何可以分割Timedelta
对象的东西——我们可以看到__floordiv__
从同一个链接中的实现中接受了什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.