使用pd.read_csv()
之后,我得到下面的数据。但在第一列中,时间格式是错误的。你能告诉我怎么改吗?谢谢。
数据如下,第一列应该是2017/4/10 9:25:00,而不是42835.39236。
datetime open high low close
0 42835.39236 20.72 20.72 20.72 20.72
1 42835.39583 20.72 20.72 20.67 20.67
2 42835.39653 20.66 20.67 20.62 20.63
3 42835.39722 20.63 20.65 20.59 20.59
4 42835.39792 20.59 20.59 20.52 20.52
最佳答案
要获得正确的日期,请使用:
import datetime as dt
df['datetime'] = pd.TimedeltaIndex(df['datetime'], unit='d') + dt.datetime(1899, 12, 30)
将产生:
datetime open high low close
0 2017-04-10 09:24:59.904 20.72 20.72 20.72 20.72
1 2017-04-10 09:29:59.712 20.72 20.72 20.67 20.67
2 2017-04-10 09:31:00.192 20.66 20.67 20.62 20.63
3 2017-04-10 09:31:59.808 20.63 20.65 20.59 20.59
4 2017-04-10 09:33:00.288 20.59 20.59 20.52 20.52
编辑
要将
datetime
分成date
和time
使用:df['date'] = df['datetime'].dt.date
df['time'] = df['datetime'].dt.ceil('min').dt.time
df
# datetime open high low close date time
#0 2017-04-10 09:24:59.904 20.72 20.72 20.72 20.72 2017-04-10 09:25:00
#1 2017-04-10 09:29:59.712 20.72 20.72 20.67 20.67 2017-04-10 09:30:00
#2 2017-04-10 09:31:00.192 20.66 20.67 20.62 20.63 2017-04-10 09:32:00
#3 2017-04-10 09:31:59.808 20.63 20.65 20.59 20.59 2017-04-10 09:32:00
#4 2017-04-10 09:33:00.288 20.59 20.59 20.52 20.52 2017-04-10 09:34:00