1

As you can see a Date & Time Column are being saved in this CSV File. Now what problem is that the date & time are in format of something like - 30-1-2022 & 20:08:00

But i want it to look something like 30th Jan 22 and 8:08 PM

Any code for that ?

import requests
import pandas as pd
from datetime import datetime
from datetime import date

currentd = date.today()

s = requests.Session()
headers =   {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'}
url = 'https://www.nseindia.com/'
step = s.get(url,headers=headers)

today = datetime.now().strftime('%d-%m-%Y')
api_url = f'https://www.nseindia.com/api/corporate-announcements?index=equities&from_date={today}&to_date={today}'


resp = s.get(api_url,headers=headers).json()

result = pd.DataFrame(resp)
result.drop(['difference', 'dt','exchdisstime','csvName','old_new','orgid','seq_id','sm_isin','bflag','symbol','sort_date'], axis = 1, inplace = True)
result.rename(columns = {'an_dt':'DateandTime', 'attchmntFile':'Source','attchmntText':'Topic','desc':'Type','smIndustry':'Sector','sm_name':'Company Name'}, inplace = True)
result[['Date','Time']] = result.DateandTime.str.split(expand=True)
result.drop(['DateandTime'], axis = 1, inplace = True)
result.to_csv( ( str(currentd.day) +'-'+str(currentd.month) +'-'+'CA.csv'), index=True)

print('Saved the CSV File')

2 Answers2

0

Try this:

# Remove comment if needed
# import locale
# locale.setlocale(locale.LC_TIME, 'C')

# https://stackoverflow.com/a/16671271
def ord(n):
    return str(n)+("th" if 4<=n%100<=20 else {1:"st",2:"nd",3:"rd"}.get(n%10, "th"))

result['Date'] = pd.to_datetime(result['Date'], format='%d-%b-%Y')
result['Date'] = result['Date'].dt.day.map(ord) + result['Date'].dt.strftime(' %b %Y')

result['Time'] = pd.to_datetime(result['Time']).dt.strftime('%-H:%M %p')

# Now you can export

Output:

>>> result[['Date', 'Time']]
             Date      Time
0   30th Jan 2022  21:07 PM
1   30th Jan 2022  20:57 PM
2   30th Jan 2022  19:40 PM
3   30th Jan 2022  18:55 PM
4   30th Jan 2022  18:53 PM
5   30th Jan 2022  18:09 PM
6   30th Jan 2022  17:44 PM
7   30th Jan 2022  16:01 PM
8   30th Jan 2022  15:21 PM
9   30th Jan 2022  15:16 PM
10  30th Jan 2022  15:10 PM
11  30th Jan 2022  15:06 PM
12  30th Jan 2022  14:29 PM
13  30th Jan 2022  14:15 PM
14  30th Jan 2022  13:41 PM
15  30th Jan 2022  12:20 PM
16  30th Jan 2022  12:09 PM
17  30th Jan 2022  12:07 PM
18  30th Jan 2022  10:58 AM
19  30th Jan 2022  10:42 AM
20  30th Jan 2022  10:40 AM
21  30th Jan 2022  10:39 AM
22  30th Jan 2022  10:06 AM
23  30th Jan 2022   9:39 AM
24  30th Jan 2022   9:36 AM
25  30th Jan 2022   9:25 AM
26  30th Jan 2022   8:43 AM
27  30th Jan 2022   1:00 AM
28  30th Jan 2022   0:59 AM
29  30th Jan 2022   0:13 AM
Corralien
  • 109,409
  • 8
  • 28
  • 52
0

Try creating a temporary column:

result['Full_date']=pd.to_datetime(result['Date']+' '+result['Time'])

Then format 'Date' and 'Time'

result['Date']=result['Full_date'].dt.strftime('%b %d, %Y')
result['Time']=result['Full_date'].dt.strftime('%R' '%p')
Nev1111
  • 1,039
  • 9
  • 13