T_T
T_T

Reputation: 1280

Python: error in plotting data created with `period_range` (pandas)

I have a problem in plotting time series data, created using pandas date_range and period_range. The former works, but the latter does not. To illustrate the problem, consider the following

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# random numbers
N = 100
z = np.random.randn(N)
ts = pd.DataFrame({'Y': z, 'X': np.cumsum(z)})

# 'date_range' is used
month_date = pd.date_range('1978-02', periods=N, freq='M')
df_date = ts.set_index(month_date)

# 'period_range' is used
month_period = pd.period_range('1978-02', periods=N, freq='M')
df_period = ts.set_index(month_period)

# plot
plt.plot(df_date);plt.show()
plt.plot(df_period);plt.show()

plt.plot(df_date) gives a nice figure, whereas plt.plot(df_period) generates the following error, which I do not understand:

ValueError: view limit minimum 0.0 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units<Figure size 432x288 with 1 Axes>

Is this an expected result? What am I missing here?

BTW, df_date.plot() and df_period.plot() both work fine, causing no problem.

Thanks in advance.

Upvotes: 3

Views: 1920

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339340

The indexes of the two dateframes are of different type:

print(type(df_date))    # pandas.core.indexes.datetimes.DatetimeIndex
print(type(df_period))  # pandas.core.indexes.period.PeriodIndex

Matplotlib does not know how to plot a PeriodIndex.

You may convert the PeriodIndex to a DatetimeIndex and plot that one instead,

plt.plot(df_period.to_timestamp())

Upvotes: 4

Related Questions