2017-08-29 64 views
1

我有一個數據框,其索引也包含小時:第二個:...但我只是想讓它包含年,月,日的日期。將DateTimeindex轉換爲僅包含年,小時和天不包含時間信息

索引是否可以仍然是DateTimeIndex但只包含年,月,日?

目前的指數看起來像:

my_index = DatetimeIndex(['2017-08-25', '2017-08-24', '2017-08-23', '2017-08-22', 
      '2017-08-21', '2017-08-20', '2017-08-19', '2017-08-18', 
      '2017-08-17', '2017-08-16', 
      ... 
      '2015-07-19', '2015-07-18', '2015-07-17', '2015-07-16', 
      '2015-07-15', '2015-07-14', '2015-07-13', '2015-07-12', 
      '2015-07-11', '2015-07-10'], 
      dtype='datetime64[ns]', length=778, freq=None) 

我可以這樣做:

only_date_index = [el.date() for el in my_index] 

不過,如果我想使用的重採樣功能的熊貓我的錯誤: 類型錯誤:只有有效與DatetimeIndex,TimedeltaIndex或PeriodIndex,但有一個'索引'的實例。

+1

嘗試'.normalize()'它將使次去午夜,我相信'DateTimeIndex'所需要的時間 – Deckerz

回答

5

您需要DatetimeIndex.floor

myIndex = df.index.floor('D') 

樣品:

rng = pd.date_range('2017-04-03 15:00:45', periods=10, freq='24T') 
df = pd.DataFrame({'a': range(10)}, index=rng) 
print (df) 
        a 
2017-04-03 15:00:45 0 
2017-04-03 15:24:45 1 
2017-04-03 15:48:45 2 
2017-04-03 16:12:45 3 
2017-04-03 16:36:45 4 
2017-04-03 17:00:45 5 
2017-04-03 17:24:45 6 
2017-04-03 17:48:45 7 
2017-04-03 18:12:45 8 
2017-04-03 18:36:45 9 

myIndex = df.index.floor('D') 
print (myIndex) 
DatetimeIndex(['2017-04-03', '2017-04-03', '2017-04-03', '2017-04-03', 
       '2017-04-03', '2017-04-03', '2017-04-03', '2017-04-03', 
       '2017-04-03', '2017-04-03'], 
       dtype='datetime64[ns]', freq=None) 

感謝另一種解決方案Deckerz - 使用DatetimeIndex.normalize

myIndex = df.index.normalize() 
print (myIndex) 
DatetimeIndex(['2017-04-03', '2017-04-03', '2017-04-03', '2017-04-03', 
       '2017-04-03', '2017-04-03', '2017-04-03', '2017-04-03', 
       '2017-04-03', '2017-04-03'], 
       dtype='datetime64[ns]', freq=None) 

時序

ix = pd.date_range('1970-01-01', '2200-01-15', freq='1H') 

print (len(ix)) 
2016481 

In [68]: %timeit (ix.normalize()) 
10 loops, best of 3: 178 ms per loop 

In [69]: %timeit (ix.floor('d')) 
10 loops, best of 3: 38.4 ms per loop 

#solution from Dror (https://stackoverflow.com/questions/45954497/in-pandas-group-by-date-from-datetimeindex) 
In [70]: %timeit pd.to_datetime(ix.date) 
1 loop, best of 3: 5.09 s per loop 
+0

'.normalize()'也做到這一點,是DateTimeIndex陣列的嵌入式功能,但不是單個實例 – Deckerz

相關問題