Skip to content

Instantly share code, notes, and snippets.

@pramitbiswas
Created April 25, 2021 14:59
Show Gist options
  • Save pramitbiswas/67d1843b065b7e25236c4d1687942de5 to your computer and use it in GitHub Desktop.
Save pramitbiswas/67d1843b065b7e25236c4d1687942de5 to your computer and use it in GitHub Desktop.
Load OHLCV data from csv file to a dataframe with given datetime range
import pandas as pd
import datetime as dt
startDateTime="2019-01-01"
endDateTime="2021-04-13"
# Get Data
df=pd.read_csv('myfile.csv')
# "Date" loaded as str, converted to datetime
df["Date"]=df["Date"].apply(lambda s: dt.datetime.strptime(s,"%Y-%m-%d"))
# "Date" loaded as column, changed to index
df.set_index("Date", inplace=True)
# Retain data only within range
df=df[ (df.index.date>=dt.datetime.strptime(startDateTime,'%Y-%m-%d').date()) \
& (df.index.date <= dt.datetime.strptime(endDateTime,'%Y-%m-%d').date()) ]
print(df.head(10))
print(df.tail(10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment