In power bi
- Combining two power bi queries - one with data & one with DateTime stamp text string
- fill down DateTime field - adding a custom column,
I am trying to achieve the same in python's data frame with no luck!
any help is greatly appreciated.
The Error that I am getting is as shown in the image. datetime stamp as appended column
My code :
import requests
import pandas as pd
import time
import datetime
import re
import json
class NseIndia2:
def __init__(self):
self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'}
self.session = requests.Session()
self.session.get("http://nseindia.com", headers=self.headers)
def get_stock_info(self, symbol, trade_info=False):
if trade_info:
url = 'https://www.nseindia.com/api/quote-equity?symbol=' symbol "§ion=trade_info"
else:
url = 'https://www.nseindia.com/api/quote-equity?symbol=' symbol
data = self.session.get(url, headers=self.headers).json()
return data
def get_option_chain(self, symbol, indices=False):
##df_list = []
if not indices:
url = 'https://www.nseindia.com/api/option-chain-equities?symbol=' symbol
else:
url = 'https://www.nseindia.com/api/option-chain-indices?symbol=' symbol
timestamp = (self.session.get(url,headers=self.headers).json()["records"]['timestamp'])
print(timestamp) ## how to append this with 'data' below ?
data = self.session.get(url,headers=self.headers).json() ["records"]["data"]
my_df = []
for i in data:
for k, v in i.items():
if k == "CE" or k == "PE":
info = v
info["instrumentType"] = k
my_df.append(info)
return pd.DataFrame(my_df)
nse = NseIndia2()
##print(nse.get_stock_info("RELIANCE"))
##print(nse.get_stock_info("RELIANCE", trade_info=True))
##print(nse.get_option_chain("ZEEL"))
df = (nse.get_option_chain("NIFTY",indices=True))
##df.loc[:,"Timestamp"] = time
## tried with no luck..I just need to append timestamp from nse as a column -
Print(df)
CodePudding user response:
You simply forgot ()
to execute function time()
- or rather time.time()
df.loc[:, "Timestamp"] = time.time()
EDIT:
If you want normal time and date then simply use `datetime
df.loc[:, "Timestamp"] = datetime.datetime.now()
BTW:
You can write it shorter
df["Timestamp"] = datetime.datetime.now()
CodePudding user response:
@furas
Code
import requests
import pandas as pd
import time
from time import gmtime, strftime
import datetime
from datetime import datetime
class NseIndia2:
def __init__(self):
self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'}
self.session = requests.Session()
self.session.get("http://nseindia.com", headers=self.headers)
def get_option_chain(self, symbol, indices=False):
if not indices:
url = 'https://www.nseindia.com/api/option-chain-equities?symbol=' symbol
else:
url = 'https://www.nseindia.com/api/option-chain-indices?symbol=' symbol
time = self.session.get(url,headers=self.headers).json()["records"]['timestamp']
# output 28-Oct-2021 13:16:44
# print(time)
# web_datetime = datetime.strptime(time,'%d-%b-%Y %H:%M:%S')
# print(dtm)
# output 2021-10-28 13:58:45
data = self.session.get(url,headers=self.headers).json()["records"]["data"]
my_df = []
for i in data:
for k, v in i.items():
if k == "CE" or k == "PE":
info = v
info["instrumentType"] = k
my_df.append(info)
return pd.DataFrame(my_df)
nse = NseIndia2()
##print(nse.get_stock_info("RELIANCE"))
df= (nse.get_option_chain("NIFTY",indices=True))
df.loc[:,"recordedat"] = time.time() ##?This is where i need web's date-time details instead of sciencitfic number
##print(df)
print(df["recordedat"])
`