datetime.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:qutils 作者: Raychee 项目源码 文件源码
def ensure_timestamps(timestamps, func_get_latest_time=None, if_fail='ignore'):
    t_is_not_timestamp = [t is None or isinstance(t, (str, pd.Timedelta, timedelta))
                          for t in timestamps]
    if any(t_is_not_timestamp):
        if t_is_not_timestamp[-1]:
            last_timestamp = to_datetime(timestamps[-1], from_datetime=func_get_latest_time,
                                         if_invalid='return_none')
            if last_timestamp is None:
                if if_fail == 'ignore':
                    return []
                elif if_fail == 'raise':
                    raise ValueError('Cannot convert timestamps {!r}'.format(timestamps))
                else:
                    raise NotImplementedError('Unsupported handling method when fail: {}'
                                              .format(if_fail))
            timestamps[-1] = last_timestamp
        else:
            last_timestamp = timestamps[-1]
        timestamps[:-1] = [to_datetime(t, from_datetime=last_timestamp) if is_not_timestamp else t
                           for t, is_not_timestamp in zip(timestamps[:-1], t_is_not_timestamp[:-1])]
    # if all(t == timestamps[0] for t in timestamps[1:]):
    #     timestamps = [timestamps[0]]
    return timestamps
评论列表


问题


面经


文章

微信
公众号

扫码关注公众号