Motivation: the code I have multiple spots where target date located. I've been manually change all of the target date to run the code. But you know, it would be nice if I can make this automated.

My trial 1. success: I was able to make target date functional via using timedelta. 2. error: however, when I insert it in the code, data seems not correct. For the example below, I set target date, '2019-03-29', but the data print out starting '2019-01-01'

Any insight will be very appreciable.

enter image description here

enter image description here

import datetime as dt
import pandas as pd
import as web
import csv
from datetime import datetime, timedelta

#data import
headers = ['ticker','per','date','time','open','high','low','close','vol']
df = pd.read_csv(r"C:\\Users\\x230\\Downloads\\NQ_190101_190531.csv", sep = ';', names=headers, skiprows=[0])
df['date_time'] = df['date'].map(str) + ' ' + df['time'].map(str)               #combine date and time data
df['date_time'] = pd.to_datetime(df['date_time'], format='%d/%m/%y %H:%M:%S')
df.set_index('date_time',inplace=True)                                          #set time back as index

date_start = '2019-03-28'
date_target = (datetime.strptime(date_start, '%Y-%m-%d') + timedelta(days=1)).strftime('%Y-%m-%d')

#selecting data range
df = df.loc[print(date_target,'01:01:00'):print(date_target,'22:59:00')]

1 Answers

mocs On

Simple solution found. As Chris mentioned, appending the strings fix the issue. See below. Thank you.

enter image description here