1

I am plotting some time-series with pandas dataframe and I ran into the problem of gaps on weekends. What can I do to remove gaps in the time-series plot?

date_concat = pd.to_datetime(pd.Series(df.index),infer_datetime_format=True)
pca_factors.index = date_concat
pca_colnames = ['Outright', 'Curve', 'Convexity']
pca_factors.columns = pca_colnames

fig,axes = plt.subplots(2)
pca_factors.Curve.plot(ax=axes[0]); axes[0].set_title('Curve')
pca_factors.Convexity.plot(ax=axes[1]); axes[1].set_title('Convexity'); plt.axhline(linewidth=2, color = 'g')
fig.tight_layout()
fig.savefig('convexity.png')

Partial plot below: enter image description here

Ideally, I would like the time-series to only show the weekdays and ignore weekends.

5
  • do you simply want to cut off data/rows for weekends? Commented Sep 20, 2016 at 10:29
  • Yes, but I don't specifically have data for the weekends... when I convert the dates to datetime format, it artificially created weekends... Commented Sep 20, 2016 at 10:32
  • I think it doesn't add any data, it simply tries to keep the ratio and simply connects point from Friday to point from Monday. Maybe you want a barplot? Commented Sep 20, 2016 at 10:37
  • Is there a way for it to connect the points between Friday and Monday while not showing the gap caused by the weekend? Commented Sep 20, 2016 at 10:40
  • you can try: pca_factors.reset_index().Convexity.plot() and then you would need somehow to put desired x-ticklabels manually... Commented Sep 20, 2016 at 10:42

1 Answer 1

1

To make MaxU's suggestion more explicit:

  • convert to datetime as you have done, but drop the weekends
  • reset the index and plot the data via this default Int64Index
  • change the x tick labels

Code:

date_concat = data_concat[date_concat.weekday < 5] # drop weekends
pca_factors = pca_factors.reset_index() # from MaxU's comment
pca_factors['Convexity'].plot()         # ^^^
plt.xticks(pca_factors.index, date_concat) # change the x tick labels
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.