python - 带有时间戳的熊猫数据框 - 仅提取时间
问题描述
如何仅从 pandas 数据框中表示为 unix 时间的时间字段值中提取时间。
即从 2021 年 8 月 29 日上午 11:22:22.942 的 1630236142.942 开始,我只想要 11:22:22.942
编辑:
pd.to_datetime(df['time'], unit='s').time()
正在产生错误AttributeError: 'Series' object has no attribute time
解决方案
你可以这样做:
from datetime import datetime
# this line converts the string object in Timestamp object
df['DateTime'] = [datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
for d in df["DateTime"]]
# this line extract time from Timestamp object
df['Time'] = [datetime.strptime(d, "%Y-%m-%d %H:%M:%S").time()
for d in df['DateTime']]
推荐阅读
- vue.js - Nuxt.js 中的“未定义窗口”
- dns - 公司如何获得顶级域名 TLD?例如。。某物
- java - Unicode 短信发送 (smpp)
- excel - 无法将文本转换为数字
- java - 如何在不影响 CSV 文件中的逗号的情况下替换字符串中的逗号?
- reactjs - 如何使用自定义挂钩在组件之间共享状态数据?
- python - Matplotlib:将不同的散点标记分组在同一个图例下
- mysql - 每天根据日期名称备份mysql数据库
- jquery - 使用 Jquery 3.3.1 完整版而不是 bootstrap 4.3 附带的 slim 版本会返回错误
- redhat - 如何构建 scl,示例是旧的并且 rpmlint 与文档相矛盾?