作者barusu (巴巴鲁斯)
看板Python
标题[问题] 金融数据处理 - 数字转时间
时间Tue May 10 11:44:06 2022
各位先进大家好:
期交所提供的原始台指数据中的成交时间格式为int64
例如150000,该怎麽转成15:00:00?
以下为我的代码
方法1:
input:
df['Time'] = pd.to_datetime(df['Time'], format = '%H%M%S')
output :
0 1900-01-01 15:00:00
1 1900-01-01 15:00:00
2 1900-01-01 15:00:00
3 1900-01-01 15:00:00
4 1900-01-01 15:00:00
前面多了1900-01-01
-------------------------------------------
方法2:
input:
df['Time'] = pd.to_datetime(df['Time']) #去掉format = '%H%M%S'
output:
0 1970-01-01 00:00:00.000150000
1 1970-01-01 00:00:00.000150000
2 1970-01-01 00:00:00.000150000
3 1970-01-01 00:00:00.000150000
4 1970-01-01 00:00:00.000150000
看起来离目标更远了
大致上google了几篇有关时间序列的文章,没有类似的问题
请问有人知道问题出在哪吗?
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.136.184.160 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1652154254.A.32E.html
※ 编辑: barusu (220.136.184.160 台湾), 05/10/2022 11:45:20
※ 编辑: barusu (220.136.184.160 台湾), 05/10/2022 11:47:28
1F:推 lycantrope: 资料又没提供年月日,电脑怎麽会知道年月日05/10 11:51
了解
2F:→ leolarrel: 把int64的值转成字串,然後两个两个切割,不难吧05/10 12:03
如果与日期合并再转就可以了,接下来再分割为两个column
※ 编辑: barusu (220.136.184.160 台湾), 05/10/2022 12:16:18
3F:→ lycantrope: pd.to_datetime会转,不提供format预设是nanoseconds05/10 12:48
4F:→ lycantrope: 还是要提供正确的年月日後用datetime做索引,之後才方05/10 12:48
5F:→ lycantrope: 便用resample来分析时间序列05/10 12:49
感谢说明,先与日期合并,转换格式,再分割就行了
※ 编辑: barusu (220.136.184.160 台湾), 05/10/2022 13:00:20
6F:→ leolarrel: 日期函数运算很慢,格式化字串or f strings 运算较快 05/10 13:10
7F:推 lycantrope: 存字串,之後分析还是要转时间格式吧 05/10 13:23