Spinor8
Spinor8

Reputation: 1607

Formatting index of a pandas table in a plot

I am trying to annotate my plot with part of a dataframe. However, the time 00:00:00 is appearing in all the row labels. Is there a clean way to remove them since my data is daily in frequency? I have tried the normalize function but that doesn't remove the time; it just zeroes the time.

Here is what the issue looks like and the sample code to reproduce the issue. 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()

Upvotes: 2

Views: 879

Answers (1)

Nickil Maveli
Nickil Maveli

Reputation: 29721

Simply access the .date attribute of the DateTimeIndex so that every individual element of your index would be represented in datetime.date format.

The default DateTimeIndex format is datetime.datetime which gets defined automatically even if you didn't explicitly define your index that way before.

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

Upvotes: 1

Related Questions