2016-12-30 50 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