How to convert daily time series data into weekly and monthly using pandas and python

While working with stock market data, sometime we would like to change our time window of reference. Generally daily prices are available at stock exchenges. Let us see how to conert daily prices into weekly and monthly prices.

You can download daily prices from NSE from this link. We will downoad daily prices for last 24 months. Here is the sample file with which we will work

15-06-2016-TO-14-06-2018HDFCBANKALLN

Please refer to below program to convert daily prices into weekly. Comments in the program will help you understand the logic behind each line.

 

Once you understand daily to weekly, only small modification is needed to convert this into monthly OHLC data. Here is the script

 

Here are the output files for your reference.

Monthly_OHLC Weekly_OHLC

Weekly_OHLC

I wasted some time to find ‘Open Price’ for weekly and monthly data. I tried some complex pandas queries and then realized same can be achieved by simply using aggregate function and Open Price:first.

Please do let me know your feedback.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.