TypeError:dtype'<class'datetime.timedelta'=“”>'无法理解

时间:2019-05-03 17:58:51

标签: pandas datetime google-colaboratory timedelta

我有两个日期,日期的不同决定了用户活跃的天数。

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

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

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

1 个答案:

答案 0 :(得分:0)

感谢@ root解决此问题。

更改

<?php
header("Content-Type: application/json");

require('db/db.php');
session_start();

    $saverawText = $_POST['rawText'];
    $saveConvertedText = $_POST['convertedText'];

    $ins_query="insert into cmn_data (`rawtext`,`convertedtext`) values ('$saverawText','$saveConvertedText')";
    $result = mysqli_query($con, $ins_query);

?>

收件人

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

应该解决问题