Python Forum
Confusion about [date]time [formatting]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Confusion about [date]time [formatting]
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(,

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

ES_skel['Cum_Return'] = (1 + ES_skel['Daily_Return']).cumprod() - 1
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?

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 653 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  Naming the file as time and date. BettyTurnips 3 750 Jan-15-2021, 07:52 AM
Last Post: BettyTurnips
  Update Date based on Time/String stevezemlicka 1 580 Jan-08-2021, 06:54 PM
Last Post: Gribouillis
  Formatting date in a dataframe WiPi 1 447 Jan-06-2021, 11:26 AM
Last Post: WiPi
  How to print n days back date at give time Mekala 1 655 Oct-10-2020, 03:35 AM
Last Post: bowlofred
  How to add date and years(integer) to get a date NG0824 4 1,005 Sep-03-2020, 02:25 PM
Last Post: NG0824
  Formatting Data/Time with Pyodbc and openpyxl bearcats6001 0 837 Aug-17-2020, 03:44 PM
Last Post: bearcats6001
  Date and time as filename Herbert58 3 1,127 Aug-08-2020, 10:11 AM
Last Post: Herbert58
  Parsing Date/Time from Metar Reports with 6 hourly weather information Lawrence 0 780 May-03-2020, 08:15 PM
Last Post: Lawrence
  Print date, Time and output to file tpolim008 3 927 Mar-26-2020, 06:49 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020