[英]Pandas operation changes a dtype: timedelta64[ns] to a dtype: object
I am calculating time differences in a pandas data frame so that I can then sum these time differences.我正在计算 pandas 数据帧中的时间差,以便我可以对这些时间差求和。 Here is my full code for doing that:
这是我这样做的完整代码:
temp['ACCESS_TIME'] = pd.to_datetime(temp['ACCESS_TIME'])
temp = temp.sort_values(['ID','ACCESS_TIME'])
temp['TIME_DIFFERENCE'] = temp.groupby(['ID'])['ACCESS_TIME'].diff()
oneMin = temp.copy()
oneMin.loc[(oneMin.TIME_DIFFERENCE > '00:01:00'), 'TIME_DIFFERENCE'] = '00:01:00'
oneMin['DURATION'] = oneMin.groupby(['ID'])['TIME_DIFFERENCE'].transform('sum')
After the third line, a simple "temp['TIME_DIFFERENCE']" shows dtype: timedelta64[ns].在第三行之后,一个简单的“temp['TIME_DIFFERENCE']”显示dtype:timedelta64[ns]。 But after the 5th line, printing "oneMin['TIME_DIFFERENCE']" shows dtype: object.
但在第 5 行之后,打印 "oneMin['TIME_DIFFERENCE']" 显示 dtype: object。 This leads to an error when attempting line 6, which states: "unsupported operand type(s) for +: 'int' and 'Timedelta'".
这会在尝试第 6 行时导致错误,该行指出:“+ 的不支持的操作数类型:'int' 和 'Timedelta'”。 I don't really know what is going on.
我真的不知道发生了什么事。
The issue is that I want to cap time differences at 1 minute.问题是我想将时差限制在 1 分钟。 Before I was just removing time differences of 1 minutes with this line:
在我用这条线消除 1 分钟的时差之前:
oneMin = oneMin[oneMin['TIME_DIFFERENCE'] < '00:01:00']
this works completely fine.这完全正常。 But I do not want to remove the values over a minute, I want to cap them at one minute.
但我不想在一分钟内删除这些值,我想在一分钟内限制它们。
The problem is that you are setting some of the values of the column 'TIME_DIFFERENCE' to the string '00:01:00'.问题是您将“TIME_DIFFERENCE”列的一些值设置为字符串“00:01:00”。 What you probably want to do is replace the fifth line in your code with
您可能想要做的是将代码中的第五行替换为
oneMin.loc[(oneMin.TIME_DIFFERENCE > '00:01:00'), 'TIME_DIFFERENCE'] = pd.Timedelta('00:01:00')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.