2017-09-25 15 views
1

我一直在使用轉換對象的最新型指數

clipdf = pd.read_clipboard() 

      A   B   C   D F 
2013-01-01 0.000000 0.000000 -1.509059 5 NaN 
2013-01-02 1.212112 -0.173215 0.119209 5 1.0 
2013-01-03 -0.861849 -2.104569 -0.494929 5 2.0 
2013-01-04 0.721555 -0.706771 -1.039575 5 3.0 
2013-01-05 -0.424972 0.567020 0.276232 5 4.0 
2013-01-06 -0.673690 0.113648 -1.478427 5 5.0 

讀取剪貼板中的一些數據,但我意識到,指數型對象

Index(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', 
     '2013-01-06'], 
     dtype='object') 

的...而不是類型datetime64

DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', 
       '2013-01-05', '2013-01-06'], 
       dtype='datetime64[ns]', freq='D') 
  1. 是很重要的是我的指標轉換爲一個Datetime64類型?
  2. 我該如何做到這一點?

問候

回答

1

我認爲你需要參數parse_dates=TrueDatetimeIndex

clipdf = pd.read_clipboard(parse_dates=True) 
print (clipdf.index) 

DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', 
       '2013-01-05', '2013-01-06'], 
       dtype='datetime64[ns]', freq=None) 

或者:

clipdf = pd.read_clipboard() 
clipdf.index = pd.to_datetime(clipdf.index) 
#alternative 
#clipdf.index = pd.DatetimeIndex(clipdf.index) 
print(clipdf.index) 
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04', 
       '2013-01-05', '2013-01-06'], 
       dtype='datetime64[ns]', freq=None) 

是很重要的是我將索引轉換爲Datetime64類型?

我認爲這取決於你的需要。但顯然是的,特別是如果使用像resample這樣的函數。性能應該更好。