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).

1
  • 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(). Commented Dec 27, 2017 at 23:13

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.