简体   繁体   中英

Why does plotting on pandas read_csv vs from_csv - behave differently?

I'm taking a introductory python class for financial analysis and learning to use pandas for the same time. I'm using Jupyter Notebook for all the code. The course uses the deprecated from_csv() function to read into a dataframe and shows how to plot the stock data nicely on the same time axis:

fb1 = pd.DataFrame.from_csv('data/FB.csv')
fb1.loc['2019-06-01':'2019-07-01', 'Close'].plot()
fb1.loc['2019-07-01':'2019-08-01', 'Close'].plot()
fb1.loc['2019-08-01':'2019-09-01', 'Close'].plot()

Plotting with DataFrame created by from_csv()

I have been trying to replicate the same results with the newer read_csv() function but it behaves differently. The plots are all squished on to the same time frame. Can some one explain to me what is the difference in the two implementations and how I can make the latter code exhibit the same behavior as the first segment?

fb = pd.read_csv('data/FB.csv')
fb.set_index('Date', inplace= True)
fb.loc['2019-06-01':'2019-07-01', 'Close'].plot()
fb.loc['2019-07-01':'2019-08-01', 'Close'].plot()
fb.loc['2019-08-01':'2019-09-01', 'Close'].plot()

Plotting with DataFrame created by read_csv()

Try adding index_col=0, parse_dates=True to your second attempt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM