python-3.x - 如何在 Python 中将字符串时间戳转换为日期时间对象
问题描述
我有一个带有时间戳字符串的 pandas 列,格式为“00:00:00.000”(小时、分钟、秒、微秒)。我想将它们转换为datetime
对象以在秒上工作。
例如,我在这里看到了许多类似的问题。我猜我应该使用strptime
,但我不知道如何使用。
解决方案
如果在 pandas 中将值转换为日期时间,还会添加一些默认值date
:to_datetime
df['col'] = pd.to_datetime(df['col'], format='%H:%M:%S.%f')
如果需要避免它通过以下方式将值转换为 timedeltas to_timedelta
:
df['col'] = pd.to_timedelta(df['col'])
推荐阅读
- javascript - 处理 MongoDB 查询结果中不存在的子文档的最佳方法
- macros - 在 Common Lisp 中获取宏的堆栈跟踪模拟
- laravel - 当点击进入时转到下一个元素 vuejs2
- python - Python,multiprocessing.pool 与 for 循环花费的时间大致相同
- node.js - 为什么节点子生成进程与父进程分离并开始独立运行?
- kotlin - Refcount Observable 不会在第二次订阅时发出
- python - Django 使用外键添加到数据库,同时仍显示来自其他模型的信息
- php - 删除 OceanWP 主题中产品页面标题下的子标题文本
- jquery - 带有附加数据的标签中声明的 jQuery 触发事件
- python - 无法从购物车页面-django url 错误访问我的在线商店的订单页面