2016-12-30 14 views
2

我想用數據框的一部分來標註我的情節。但是,時間00:00:00出現在所有行標籤中。由於我的數據每天都在頻率中,是否有一種乾淨的方法可以將它們刪除?我已經嘗試了標準化功能,但這並沒有消除時間;它只是零時間。一張圖中熊貓表格式的索引

下面是問題的樣子以及重現問題的示例代碼。 enter image description here

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
from pandas.tools.plotting import table 

# Setup of mock data 
date_range = pd.date_range('2014-01-01', '2015-01-01', freq='MS') 
df = pd.DataFrame({'Values': np.random.rand(0, 10, len(date_range))}, index=date_range) 

# The plotting of the table 
fig7 = plt.figure() 
ax10 = plt.subplot2grid((1, 1), (0, 0)) 
table(ax10, np.round(df.tail(5), 2), loc='center', colWidths=[0.1] * 2) 
fig7.show() 

回答

1

只需訪問DateTimeIndex這樣的.date的屬性,索引的每一個單獨的元素將在datetime.date格式來表示。

默認DateTimeIndex格式爲datetime.datetime即使您沒有明確定義您的索引,它也會自動定義。

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
from pandas.tools.plotting import table 

np.random.seed(42) 
# Setup of mock data 
date_range = pd.date_range('2014-01-01', '2015-01-01', freq='MS') 
df = pd.DataFrame({'Values': np.random.rand(len(date_range))}, date_range) 
df.index = df.index.date         # <------ only change here 

# The plotting of the table 
fig7 = plt.figure() 
ax10 = plt.subplot2grid((1, 1), (0, 0)) 
table(ax10, np.round(df.tail(5), 2), loc='center', colWidths=[0.1] * 2) 
fig7.show() 

enter image description here