' 不明白,pandas,datetime,google-colaboratory,timedelta"/>

首页 > 解决方案 > 类型错误:dtype '' 不明白

问题描述

我有两个日期,日期的差异决定了用户活跃了多少天。

df['days_active'] = df['last_login'] - df['first_login']

然后我对有效对象使用 datetime.timedelta days 方法,该方法在我更新到当前 panda 之前一直有效

df['days_active'] = df['days_active'].astype(dt.timedelta).map(lambda x: np.nan if pd.isnull(x) else x.days)

TypeError                                 Traceback (most recent call last)
<ipython-input-8-335b54b7b187> in <module>()
      1 df['days_active'] = df['last_login'] - df['first_login']
----> 2 df['days_active'] = df['days_active'].astype(dt.timedelta).map(lambda x: np.nan if pd.isnull(x) else x.days)
      3 df['weeks_active'] = df['days_active']/7
      4 df['weekly_min_avg'] = df['total_minutes']/df['weeks_active']

5 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
   5689             # else, only a single dtype is given
   5690             new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors,
-> 5691                                          **kwargs)
   5692             return self._constructor(new_data).__finalize__(self)
   5693 

/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
    529 
    530     def astype(self, dtype, **kwargs):
--> 531         return self.apply('astype', dtype=dtype, **kwargs)
    532 
    533     def convert(self, **kwargs):

/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
    393                                             copy=align_copy)
    394 
--> 395             applied = getattr(b, f)(**kwargs)
    396             result_blocks = _extend_blocks(applied, result_blocks)
    397 

/usr/local/lib/python3.6/dist-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
    532     def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs):
    533         return self._astype(dtype, copy=copy, errors=errors, values=values,
--> 534                             **kwargs)
    535 
    536     def _astype(self, dtype, copy=False, errors='raise', values=None,

/usr/local/lib/python3.6/dist-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
    593 
    594         # convert dtypes if needed
--> 595         dtype = pandas_dtype(dtype)
    596         # astype processing
    597         if is_dtype_equal(self.dtype, dtype):

/usr/local/lib/python3.6/dist-packages/pandas/core/dtypes/common.py in pandas_dtype(dtype)
   2027         return npdtype
   2028     elif npdtype.kind == 'O':
-> 2029         raise TypeError("dtype '{}' not understood".format(dtype))
   2030 
   2031     return npdtype

TypeError: dtype '<class 'datetime.timedelta'>' not understood

标签: pandasdatetimegoogle-colaboratorytimedelta

解决方案


感谢@root解决了这个问题。

改变

df['days_active'] = df['days_active'].astype(dt.timedelta).map(lambda x: np.nan if pd.isnull(x) else x.days)

df['days_active'] = df['days_active'].dt.days

应该解决问题


推荐阅读