Python Forum
Confusion about [date]time [formatting] - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Confusion about [date]time [formatting] (/thread-31542.html)



Confusion about [date]time [formatting] - Mark17 - Dec-17-2020

This did what I wanted:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime

ES = pd.read_csv(r'C:\Users\drkle\ES(daily).csv', parse_dates=["Date"], index_col="Date")

fig, ax = plt.subplots() #can I initialize fig, ax without plotting blank chart?

rawdate_start = '2017-01-01' #input('Please enter start date as YYYY-MM-DD: ')
rawdate_end = '2019-01-01' #input('Please enter end date as YYYY-MM-DD: ')
#date_start = datetime.datetime.strptime(rawdate_start, '%Y-%m-%d').date()
#date_end = datetime.datetime.strptime(rawdate_start, '%Y-%m-%d').date()

#print('Starting and ending dates are {} and {}, respectively'.format(date_start.date(),date_end.date()))

ES_cut = ES.drop(['Open','High','Low','Vol'],axis=1)
#print(ES_cut.index)
#print(date_start, date_end)
ES_skel = ES_cut[rawdate_start:rawdate_end]
#print(ES_cut[rawdate_start:rawdate_end])
#print(ES_skel.tail())

ES_skel['Daily_Return']=ES_skel['Close'].pct_change()
ES_skel['Cum_Return'] = (1 + ES_skel['Daily_Return']).cumprod() - 1
print(ES_skel.head(20))
Please look at Line 20. I initially had:
ES_skel = ES_cut[date_start:date_end]
and Lines 12-13 not commented out. That didn't work (I think it resulted in an empty dataframe). What I was trying to do with Lines 12-13 was take a user-inputted date (string) and convert it to datetime format so that I could use it to slice the dataframe, which has a datetime index. Why was this not necessary and would I ever have to do something like this?