0

I have two csv files from which I have created a single dataframe. Problem is first it adds data of 'Tatasteel' first and then data of 'Tatamotors'. What I want is to order them according to date. For example, first data of Tatasteel at 9:15 and then data of Tatamotors at 9:15 and so on.

Here is the code:

import pandas as pd
import datetime as dt
from datetime import timedelta
import os
import glob


exclude_days = [dt.datetime(2019, 1, 5), dt.datetime(2019, 1, 6), dt.datetime(2019, 1, 12), dt.datetime(2019, 1, 13), dt.datetime(2019, 1, 19), dt.datetime(2019, 1, 20),
dt.datetime(2019, 1, 26), dt.datetime(2019, 1, 27) ]
backtest_start = dt.datetime(2019, 1, 1)
backtest_end = dt.datetime(2019, 1, 2)

path = os.getcwd()
path = os.path.join(path,"2019/*")
dat=[]
data3 =[]
stock_list = glob.glob(path)
for stock in stock_list:
   rdata= pd.read_csv(stock, parse_dates=['Date'])
   dat.append(rdata)
data = pd.concat(dat, ignore_index=True)

   
curr_day = backtest_start

while curr_day < backtest_end:
   if curr_day not in exclude_days:
      day_start = dt.datetime(curr_day.year,curr_day.month,curr_day.day, 9, 15)
      day_end = dt.datetime(curr_day.year,curr_day.month,curr_day.day, 15, 15)
      data2 =  data[data['Date'].between(day_start,day_end)]
      data2 = data2.reset_index(drop=True)
      data2 = pd.DataFrame(data2)
      print(data2)         
        
  curr_day += timedelta(days=1)

Here is the output: enter image description here

0

1 Answer 1

1

Use sort_values:

df = df.sort_values('Date')

Or if you want the Date column as index:

df = df.set_index('Date').sort_index()
Sign up to request clarification or add additional context in comments.

1 Comment

How you know OP need Date like index? I think this solution should be converted to wiki.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.