[英]Pandas index datetime formatting is not giving desired output Python3
I have tried the following: 我尝试了以下方法:
df.index= pd.to_datetime(df.index,format="%Y.%m.%d %HH:%MM:%SS")
df.tail()
The output was: 输出为:
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
I was expecting that the Time
index of the dataframe shoud be in the format as shown in the following output: 我期望数据帧的
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
Kindly, help me get through this. 请帮助我解决这个问题。
I think you need only one H
, M
and S
: 我认为您只需要一个
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')
If want create Datetimeindex
: 如果要创建
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)
But if want custom format of index, use DatetimeIndex.strftime
, but DatetimeIndex
is lost, indices are strings: 但是如果要自定义索引格式,请使用
DatetimeIndex.strftime
,但DatetimeIndex
丢失,索引为字符串:
df.index = df.index.strftime("%Y.%m.%d %H:%M:%S")
print (df)
open high low close
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.