Skip to content
Advertisement

Setting Time with interval of 1 minute

I have a dataset which comprises of minutely data for 2 stocks over 3 months. I have to create date in the first column and time (with interval of 1 minute) in the next column for 3 months. I am attaching the snap of 1 such data set. Kindly help me to solve this problem.

Data Format

Advertisement

Answer

-Create 3 month range numpy array of dates and time with minute frequency

date_rng = pd.date_range(start='1/1/2021', end='3/31/2021', freq='min')

-Isolate dates

date = date_rng.date

-Isolate times

time = date_rng.time

-Create pandas dataframe with 2 columns (date and time)

pd.DataFrame({'date': date, 'time': time})

-Then simply concat the new dataframe with your existing dataframe on the column axis.

***** Remove Saturday and Sunday *****

You could remove weekends by creating a column with weekend day names and then taking a slice of the dataframe exluding Saturday and Sunday:

date_rng = pd.date_range(start='1/1/2021', end='3/31/2021', freq='min')
date = date_rng.date
time = date_rng.time
day = date_rng.day_name()
df = pd.DataFrame({'date': date, 'time': time, 'day': day})

Remove Sat and Sun with this code:

sat = df.day != 'Saturday'
sun = df.day != 'Sunday'
df = df[sat & sun]

As for Holidays, you could use the same method but you would need a list of the holidays applicable in your region.

****** Trading times ******

marketOpen = datetime.strptime('9:15:00', "%H:%M:%S").time()
marketClose = datetime.strptime('15:59:00', "%H:%M:%S").time()

df = df[(df.time >= marketOpen) & (df.time <= marketClose)]

******* Exclude specific day ******

holiday = datetime.strptime("03/30/2021", "%m/%d/%Y").date()
df = df[df.date != holiday]

Lastly, don’t forget to reset your dataframe’s index.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement