简体   繁体   English

Pandas 操作将 dtype: timedelta64[ns] 更改为 dtype: object

[英]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.

相关问题 Python,pandas,计算 RSI,dtype=timedelta64[ns] 和 in 之间的比较无效 - Python, pandas, compute RSI, Invalid comparison between dtype=timedelta64[ns] and in timedelta64[ns] -&gt; FutureWarning:不推荐传递 timedelta64-dtype 数据,将在未来版本中引发 TypeError - timedelta64[ns] -> FutureWarning: Passing timedelta64-dtype data is deprecated, will raise a TypeError in a future version 为什么我收到 TypeError:dtype datetime64[ns] 无法转换为 timedelta64[ns]? - why am i getting TypeError: dtype datetime64[ns] cannot be converted to timedelta64[ns]? dtype timedelta64[ns] 无法转换为 datetime64[ns] - dtype timedelta64[ns] cannot be converted to datetime64[ns] TypeError:dtype=timedelta64[ns] 和 int 之间的比较无效 - 无法减去时间数据 - TypeError: Invalid comparison between dtype=timedelta64[ns] and int - unable to subtract time data 熊猫timedelta64 [ns]计算 - Pandas timedelta64[ns] calculations pandas Timedelta 和 timedelta64[ns] 的区别? - The difference between pandas Timedelta and timedelta64[ns]? 将 timedelta64[ns] 列转换为 Python Pandas DataFrame 中的秒 - Convert timedelta64[ns] column to seconds in Python Pandas DataFrame Groupby进入数据帧的平均时间delta64 [ns] - Average of Groupby into a dataframe timedelta64[ns] 将 timedelta64[ns] 转换为十进制 - Converting timedelta64[ns] to decimal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM