熊猫索引日期时间格式未提供所需的输出Python3

时间:2018-07-25 06:19:44

标签: python python-3.x pandas

我尝试了以下方法:

df.index= pd.to_datetime(df.index,format="%Y.%m.%d %HH:%MM:%SS")
df.tail()

输出为:

                    open    high    low close
Time                    
2018-07-24 16:25:00 1.16963 1.16976 1.16952 1.16952
2018-07-24 16:26:00 1.16952 1.16952 1.16938 1.16939
2018-07-24 16:27:00 1.16939 1.16940 1.16896 1.16908
2018-07-24 16:28:00 1.16909 1.16929 1.16908 1.16929
2018-07-24 16:29:00 1.16930 1.16932 1.16919 1.16925

我期望数据帧的Time索引应采用以下输出所示的格式:

                    open    high    low close
Time                    
2018.07.24 16:25:00 1.16963 1.16976 1.16952 1.16952
2018.07.24 16:26:00 1.16952 1.16952 1.16938 1.16939
2018.07.24 16:27:00 1.16939 1.16940 1.16896 1.16908
2018.07.24 16:28:00 1.16909 1.16929 1.16908 1.16929
2018.07.24 16:29:00 1.16930 1.16932 1.16919 1.16925

请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:0)

我认为您只需要一个element.innerTextSet elements = html.getElementsByClassName("question-hyperlink") Dim erow As Long For Each element In elements erow = Worksheets("Exec").Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row Worksheets("Exec").Cells(erow, 1) = element.innerText Worksheets("Exec").Cells(erow, 2) = element.GetAttribute("href") 'this should give you the URL Next element H

M

S

如果要创建df = pd.DataFrame({'Time': ['2018.07.24 16:25:00', '2018.07.24 16:26:00', '2018.07.24 16:27:00', '2018.07.24 16:28:00', '2018.07.24 16:29:00'], 'open': [1.16963, 1.1695200000000001, 1.16939, 1.16909, 1.1693], 'high': [1.1697600000000001, 1.1695200000000001, 1.1694, 1.16929, 1.1693200000000001], 'low': [1.1695200000000001, 1.1693799999999999, 1.16896, 1.16908, 1.16919], 'close': [1.1695200000000001, 1.16939, 1.16908, 1.16929, 1.16925]}).set_index('Time')

print (df)
                        open     high      low    close
Time                                                   
2018.07.24 16:25:00  1.16963  1.16976  1.16952  1.16952
2018.07.24 16:26:00  1.16952  1.16952  1.16938  1.16939
2018.07.24 16:27:00  1.16939  1.16940  1.16896  1.16908
2018.07.24 16:28:00  1.16909  1.16929  1.16908  1.16929
2018.07.24 16:29:00  1.16930  1.16932  1.16919  1.16925

print (df.index)
Index(['2018.07.24 16:25:00', '2018.07.24 16:26:00', '2018.07.24 16:27:00',
       '2018.07.24 16:28:00', '2018.07.24 16:29:00'],
      dtype='object', name='Time')

但是如果要自定义索引格式,请使用DatetimeIndex.strftime,但是Datetimeindex会丢失,索引是字符串:

df.index= pd.to_datetime(df.index,format="%Y.%m.%d %H:%M:%S")

print (df)
                        open     high      low    close
Time                                                   
2018-07-24 16:25:00  1.16963  1.16976  1.16952  1.16952
2018-07-24 16:26:00  1.16952  1.16952  1.16938  1.16939
2018-07-24 16:27:00  1.16939  1.16940  1.16896  1.16908
2018-07-24 16:28:00  1.16909  1.16929  1.16908  1.16929
2018-07-24 16:29:00  1.16930  1.16932  1.16919  1.16925

print (df.index)
DatetimeIndex(['2018-07-24 16:25:00', '2018-07-24 16:26:00',
               '2018-07-24 16:27:00', '2018-07-24 16:28:00',
               '2018-07-24 16:29:00'],
              dtype='datetime64[ns]', name='Time', freq=None)