2016-12-19 8 views
2

私のパンダのデータフレームDFがのpythonパンダ - インデックス日付変更

type(df.index) 

が与えるインデックスおよび列「A」を持っている

pandas.tseries.index.DatetimeIndex 
私は周りの日付に番号を切り替えるにはどうすればよい

、このように:

      A 
timestamp 
2015-03-05 13:51:00 71.000000 
2015-03-05 13:52:00 71.600000 
2015-03-05 13:53:00 72.500000 
2015-03-05 13:54:00 73.142857 
2015-03-05 13:55:00 77.625000 

このなり

      A 
timestamp 
03-05-2015 13:51:00 71.000000 
03-05-2015 13:52:00 71.600000 
03-05-2015 13:53:00 72.500000 
03-05-2015 13:54:00 73.142857 
03-05-2015 13:55:00 77.625000 
+1

あなたは算術演算を実行できないように有用ではない文字列に変換せずに日時の出力形式を変更することはできませんあなたがdatetimesでできるような文字列の操作 – EdChum

答えて

2

あなたはDatetimeIndex.strftimeを使用することができますが、その後、インデックス型ではない、日時の文字列です。

#maybe is necessary swap %m with %d if first is day not month 
df.index = df.index.strftime('%m-%d-%Y %H:%M:%S') 
print (df) 
          A 
03-05-2015 13:51:00 71.000000 
03-05-2015 13:52:00 71.600000 
03-05-2015 13:53:00 72.500000 
03-05-2015 13:54:00 73.142857 
03-05-2015 13:55:00 77.625000 

print (df.index) 
Index(['03-05-2015 13:51:00', '03-05-2015 13:52:00', '03-05-2015 13:53:00', 
     '03-05-2015 13:54:00', '03-05-2015 13:55:00'], 
     dtype='object') 

print (type(df.index[0])) 
<class 'str'> 
+0

ありがとうthats完璧! –

関連する問題