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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 120 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Date Time Series Help...Please spra8560 2 312 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Python date format changes to date & time 1418 4 516 Jan-20-2024, 04:45 AM
Last Post: 1418
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 961 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  Formatting a date time string read from a csv file DosAtPython 5 1,160 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  Wait til a date and time KatManDEW 2 1,390 Mar-11-2022, 08:05 PM
Last Post: KatManDEW
  Date format and past date check function Turtle 5 4,068 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,190 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  Naming the file as time and date. BettyTurnips 3 2,888 Jan-15-2021, 07:52 AM
Last Post: BettyTurnips
  Update Date based on Time/String stevezemlicka 1 1,985 Jan-08-2021, 06:54 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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