0

Since pandas last update, the x axis is not reading the index as a date. Any clues on what changed? As an example, the following code (Source) creates a random df. The matplotlib part is exactly what I'm have been doing with my real dataset (dates in my data where made using time.strftime("%Y-%m-%d")):

import pandas as pd
import numpy as np
from datetime import datetime, timedelta
date_today = datetime.now()
days = pd.date_range(date_today, date_today + timedelta(7), freq='D')
np.random.seed(seed=1111)
data = np.random.randint(1, high=100, size=len(days))
df = pd.DataFrame({'test': days, 'col2': data})
df = df.set_index('test')
# creates graph:
import matplotlib.pyplot as plt
fig = plt.plot(df.index, df["col2"])
fig = plt.xticks(rotation=30), plt.legend(loc='best'), plt.xlabel("Weeks")
fig = plt.style.use(['bmh', 'seaborn-paper'])
fig = plt.title("Index", fontsize=14, fontweight='bold')
plt.show()

Graph

The resulting graph has the x axis in number format. Before updating, my graphs automatically had dates in the index (because the index is in date format).

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
egodial
  • 87
  • 1
  • 2
  • 9
  • When I run your code, I am gettting x-axis in the format of dates, YYYY-MM-DD. You my try resetting rcdefaults(). `import matplotlib as mpl` `mpl.rcdefaults()`. – Scott Boston Dec 27 '17 at 23:13

2 Answers2

1

Solution 1

Use pandas .plot on the dataframe:

import pandas as pd
import numpy as np
from datetime import datetime, timedelta
date_today = datetime.now()
days = pd.date_range(date_today, date_today + timedelta(7), freq='D')
np.random.seed(seed=1111)
data = np.random.randint(1, high=100, size=len(days))
df = pd.DataFrame({'test': days, 'col2': data})
df = df.set_index('test')
# creates graph:
import matplotlib.pyplot as plt
sub = df.plot()
fig = plt.xticks(rotation=30), plt.legend(loc='best'), plt.xlabel("Weeks")
fig = plt.style.use(['bmh', 'seaborn-paper'])
fig = plt.title("Index", fontsize=14, fontweight='bold') 

Solution 2

Convert them Python datetime objects:

fig = plt.plot(df.index.to_pydatetime(), df["col2"])

Result of both approaches

enter image description here

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • Both are elegant solutions! and worked. I'll stick with #2 because it does not add further lines to my code. Thank you! Edit: Thanks for the edits. – egodial Dec 28 '17 at 01:21
1

Pandas used to import the units handlers for datetime64, but as of 0.21 stopped (though it may be back for 0.22). The way to get the old behaviour without explicit conversion is

from pandas.tseries import converter as pdtc
pdtc.register()
Jody Klymak
  • 4,979
  • 2
  • 15
  • 31