Skip to content
All library documents

Parsing Stock Price Dates to Fix Matplotlib Line Plot Artifacts

Article Quant Q&A · Author: RosG

Summary

The document explains a common cause of strange vertical lines in a stock-price plot: dates in a CSV may be interpreted incorrectly when loaded into a pandas DataFrame. In the example, the dates appear to have been read as the first day of each month, and one answer suggests using day-first parsing to match the source format. Another answer recommends enabling date parsing and format inference when reading the CSV.

The issue is illustrated by a contrast between an Excel plot that looks reasonable and a Python plot with unexplained vertical lines, using the same IBM price file. The responses attribute the discrepancy to date interpretation rather than to the plotting libraries themselves. The advice is specific to the reported date-format problem; the document provides no plot code, data sample, or confirmation of which suggestion resolved the issue, so the date format should be checked against the actual CSV before applying either approach.

Key ideas

  • Incorrectly parsed dates can make a stock-price line plot appear to contain unexplained vertical lines.
  • The example suggests checking whether dates intended as day-first have been read with the wrong convention.
  • Pandas date parsing and format inference are suggested ways to read date columns correctly.
  • The document does not include a sample file or confirm which proposed parsing change fixed the plot.

Tags

Full text
# Bad plots in python, good plots in excel


# Bad plots in python, good plots in excel












I am wondering if you could solve the mystery about why matplotlib / seaborn give me a line plot of the IBM stock price, which is terrible as you can see below, with some vertical lines that are of unknown origin, while excel gives me a very good and nice line plot . I am using the same .csv file in both cases, and the latest python/ matplotlib versions. The csv file has the IBM stock price, which I downloaded from the internet. What am I doing wrong ?

Below is the excel plot :

## Answer by Helin (score 2)

https://quant.stackexchange.com/a/63541

Looking at the dates in your `DataFrame` (they're all parsed as the first day of a month), it appears that you need to add `dayfirst=True` in your `read_csv` statement.

## Answer by Raj Nair (score 0)

https://quant.stackexchange.com/a/66574

while reading the file in pandas do this:

df = pd.read_csv('IBMclose.csv', parse_dates= ['Date'], infer_datetime_format= True)

Its just not reading the date right.

If True and parse_dates is enabled, pandas will attempt to infer the format of the datetime strings in the columns, and if it can be inferred, switch to a faster method of parsing them. In some cases this can increase the parsing speed by 5-10x.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.