Home > Blockchain >  How to remove YYYY-MM-DD from datetime, leave HH:MM, and convert it to EST
How to remove YYYY-MM-DD from datetime, leave HH:MM, and convert it to EST

Time:04-30

I'm building a stock researching program that utilizes an API and I can't seem to figure out how to remove the years, months, and day from the datetime. The problem with this is that date is a user input so I tried using f strings but it did not work. The script itself works fine its just that I have plans on automating this so it transfers to an excel sheet by itself and I'd like for the time to be in EST as that is what the stock market operates on.

df = pd.DataFrame({'h': [6.50, 6.63, 6.50, 6.50, 6.55, 6.55, 6.55, 6.57],
                   'date': [2022-04-22 08:46:00, 2022-04-22 09:14:00, 
                            2022-04-22 09:41:00, 2022-04-22 09:45:00, 
                            2022-04-22 11:54:00, 2022-04-22 11:57:00, 
                            2022-04-22 12:06:00, 2022-04-22 13:00:00]

ticker = input('Ticker: ')
date = input('Date (YYYY-MM-DD): ')
data30 = requests.get(api_30).json()
data10 = requests.get(api_10).json()
data1 = requests.get(api_1).json()

data30['results']
data10['results'] 
data1['results']

df = pd.DataFrame(data1['results']) #Results grabbed from API
df['date'] = pd.to_datetime(df['t'], unit = 'ms')#To convert to datetime 
df['date'] = pd.to_datetime(df['date'])#Label new column 'date'

pmhightime = df[(df['date'] > f'{date} 07:30:00') & (df['date'] < f'{date} 13:30:00')]#To display premarket hours only
pmhightime = pmhightime[['h','date']]#Change dataframe to only include 'h' being high and 'date'
pmhightime = pmhightime[pmhightime['h'] == pmhightime['h'].max()] #To find the row that has the highest value
pmhightime = pmhightime[['date']]#Change dataframe to only have date

print('Premarket High: ', pmhightime.to_string(index=False,header=False)) #To get rid of index and header
###Output /// for ticker I will put DOGZ and date 2022-04-22
Ticker: DOGZ
Date (YYYY-MM-DD): 2022-04-22
Premarket High:  2022-04-22 09:14:00
###The output I'd like to have
Ticker: DOGZ
Date (YYYY-MM-DD): 2022-04-22
Premarket High: 5:14:00 AM

CodePudding user response:

You could surely use momentjs library it provides us with a wide variety of functions that we can use to fulfill our needs in that particular use case. Here is its website: https://momentjs.com/

CodePudding user response:

You can use:

from datetime import datetime, timedelta, tzinfo
import pytz

def parseDate(date):
  return datetime.strftime(pytz.timezone('US/Eastern').localize(date),'%H:%M')

datetime.strftime(date,'%H:%M') formats it in HH:MM

pytz.timezone('US/Eastern').localize(date) localizes the date

CodePudding user response:

Create a new column with just the time formatted as string:

df["time"] = df["date"].dt.strftime("%I:%M %p")

>>> df
      h                date      time
0  6.50 2022-04-22 08:46:00  08:46 AM
1  6.63 2022-04-22 09:14:00  09:14 AM
2  6.50 2022-04-22 09:41:00  09:41 AM
3  6.50 2022-04-22 09:45:00  09:45 AM
4  6.55 2022-04-22 11:54:00  11:54 AM
5  6.55 2022-04-22 11:57:00  11:57 AM
6  6.55 2022-04-22 12:06:00  12:06 PM
7  6.57 2022-04-22 13:00:00  01:00 PM
  • Related