如何修复TypeError:仅对DatetimeIndex有效

2024-03-29 08:05:54 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
1条回答
网友
1楼 · 发布于 2024-03-29 08:05:54

创建DatetimeIndex-首先使用^{}unit参数将CloseTime转换为日期时间,然后使用^{}

df.columns = df.columns.str.strip()

df['CloseTime'] = pd.to_datetime(df['CloseTime'], unit='s')
df = df.set_index('CloseTime')
print (df)
             OpenPrice  HighPrice   LowPrice   ClosePrice      Volume  \
CloseTime                                                               
2018-04-16   503.00000  535.01184  497.09033    533.72046  13180.7390   
2018-04-17   533.72046  538.00000  498.31665    511.78436   9553.3870   
2018-04-18   512.17940  521.49100  500.91320    503.00000   5921.6836   
2018-04-19   502.00000  527.33170  502.00000    524.60547   8402.2140   
2018-04-20   526.11760  570.14950  519.50580    568.43866  10088.1530   

                 Adj Volume  
CloseTime                    
2018-04-16        6791510.5  
2018-04-17        4888143.5  
2018-04-18        3020385.5  
2018-04-19        4344271.5  
2018-04-20        5501865.0  

相关问题 更多 >