看板 Python 關於我們 聯絡資訊
各位先進大家好: 期交所提供的原始台指數據中的成交時間格式為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://www.ptt.cc/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
lycantrope: 資料又沒提供年月日,電腦怎麼會知道年月日05/10 11:51
瞭解
leolarrel: 把int64的值轉成字串,然後兩個兩個切割,不難吧05/10 12:03
如果與日期合併再轉就可以了,接下來再分割為兩個column ※ 編輯: barusu (220.136.184.160 臺灣), 05/10/2022 12:16:18
lycantrope: pd.to_datetime會轉,不提供format預設是nanoseconds05/10 12:48
lycantrope: 還是要提供正確的年月日後用datetime做索引,之後才方05/10 12:48
lycantrope: 便用resample來分析時間序列05/10 12:49
感謝說明,先與日期合併,轉換格式,再分割就行了 ※ 編輯: barusu (220.136.184.160 臺灣), 05/10/2022 13:00:20
leolarrel: 日期函數運算很慢,格式化字串or f strings 運算較快 05/10 13:10
lycantrope: 存字串,之後分析還是要轉時間格式吧 05/10 13:23