publish_to_trend = {} # 从上传到登榜天数的集合
df_video_first = df_video_full.reset_index().drop_duplicates('视频ID',keep ='first').set_index('视频ID') # 登榜日期B
diff_first = (df_video_first['热榜日期']).astype('datetime64[ns]')-df_video_first['上传日期'].astype('datetime64[ns]') #B-A
diff_first = diff_first.reset_index() # 重置索引
diff_first.columns = ['视频ID','从上传到登榜天数'] # 设置字段名
# 求从上传到登榜天数的集合
for i, row in diff_first.iterrows():
publish_to_trend[row['视频ID']] = row['从上传到登榜天数'].days
df_video_last = df_video # 最后在榜日期
diff_last = df_video['热榜日期'].astype('datetime64[ns]')-df_video['上传日期'].astype('datetime64[ns]') #C-A
diff_last = diff_last.reset_index() # 重置索引
diff_last.columns = ['视频ID','从上传到下榜天数'] # 设置字段名
df_video = df_video.reset_index() # 重置索引
df_video.insert(4,'从上传到下榜天数', diff_last['从上传到下榜天数'].astype('timedelta64[D]').astype('int64'))#“从上传到下榜天数”字段
df_video.insert(4,'从上传到登榜天数', df_video['视频ID'].map(publish_to_trend)) # 用map() API 添加“从上传到登榜天数”字段
df_video.insert(4, '在榜天数', 0) # 在榜天数字段
df_video['在榜天数'] = (df_video['从上传到下榜天数']-df_video['从上传到登榜天数'])+1 # 求在榜天数
df_video.set_index('视频ID')[['从上传到登榜天数','在榜天数']].sort_values('在榜天数',ascending=False).head() #排序
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[15], line 25 21 diff_last.columns = ['视频ID','从上传到下榜天数'] # 设置字段名 23 df_video = df_video.reset_index() # 重置索引 ---> 25 df_video.insert(4,'从上传到下榜天数', diff_last['从上传到下榜天数'].astype('timedelta64[D]').astype('int64'))#“从上传到下榜天数”字段 27 df_video.insert(4,'从上传到登榜天数', df_video['视频ID'].map(publish_to_trend)) # 用map() API 添加“从上传到登榜天数”字段 29 df_video.insert(4, '在榜天数', 0) # 在榜天数字段 File ~\anaconda3\Lib\site-packages\pandas\core\generic.py:6534, in NDFrame.astype(self, dtype, copy, errors) 6530 results = [ser.astype(dtype, copy=copy) for _, ser in self.items()] 6532 else: 6533 # else, only a single dtype is given -> 6534 new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors) 6535 res = self._constructor_from_mgr(new_data, axes=new_data.axes) 6536 return res.__finalize__(self, method="astype") File ~\anaconda3\Lib\site-packages\pandas\core\internals\managers.py:414, in BaseBlockManager.astype(self, dtype, copy, errors) 411 elif using_copy_on_write(): 412 copy = False --> 414 return self.apply( 415 "astype", 416 dtype=dtype, 417 copy=copy, 418 errors=errors, 419 using_cow=using_copy_on_write(), 420 ) File ~\anaconda3\Lib\site-packages\pandas\core\internals\managers.py:354, in BaseBlockManager.apply(self, f, align_keys, **kwargs) 352 applied = b.apply(f, **kwargs) 353 else: --> 354 applied = getattr(b, f)(**kwargs) 355 result_blocks = extend_blocks(applied, result_blocks) 357 out = type(self).from_blocks(result_blocks, self.axes) File ~\anaconda3\Lib\site-packages\pandas\core\internals\blocks.py:616, in Block.astype(self, dtype, copy, errors, using_cow) 596 """ 597 Coerce to the new dtype. 598 (...) 612 Block 613 """ 614 values = self.values --> 616 new_values = astype_array_safe(values, dtype, copy=copy, errors=errors) 618 new_values = maybe_coerce_values(new_values) 620 refs = None File ~\anaconda3\Lib\site-packages\pandas\core\dtypes\astype.py:238, in astype_array_safe(values, dtype, copy, errors) 235 dtype = dtype.numpy_dtype 237 try: --> 238 new_values = astype_array(values, dtype, copy=copy) 239 except (ValueError, TypeError): 240 # e.g. _astype_nansafe can fail on object-dtype of strings 241 # trying to convert to float 242 if errors == "ignore": File ~\anaconda3\Lib\site-packages\pandas\core\dtypes\astype.py:180, in astype_array(values, dtype, copy) 176 return values 178 if not isinstance(values, np.ndarray): 179 # i.e. ExtensionArray --> 180 values = values.astype(dtype, copy=copy) 182 else: 183 values = _astype_nansafe(values, dtype, copy=copy) File ~\anaconda3\Lib\site-packages\pandas\core\arrays\timedeltas.py:380, in TimedeltaArray.astype(self, dtype, copy) 376 return type(self)._simple_new( 377 res_values, dtype=res_values.dtype, freq=self.freq 378 ) 379 else: --> 380 raise ValueError( 381 f"Cannot convert from {self.dtype} to {dtype}. " 382 "Supported resolutions are 's', 'ms', 'us', 'ns'" 383 ) 385 return dtl.DatetimeLikeArrayMixin.astype(self, dtype, copy=copy) ValueError: Cannot convert from timedelta64[ns] to timedelta64[D]. Supported resolutions are 's', 'ms', 'us', 'ns'