400 status code when pulling US Census API - python

I want to use the census API to pull employment data that is identical to the CB1100A11 table (Screenshot attached). Each row of this table represents a different 2-digit NAICS sector. Although structuring this table is another task entirely, it appears that I am unable to get API data when I include additional variables.
I have had success with each of the example urls the Census Bureau provides, but I have not had any success with my own. I have included a code snippet below, minus my key, to show what this looks like. I am using Python 3 in Jupyter Notebooks and BS4 from BeautifulSoup.
I have already consulted the API users documentation and variable list without success.
example_vars = 'NAICS2007_TTL,GEO_TTL,EMP,LFO_TTL,ESTAB,PAYANN'
my_vars = 'NAICS2007,NAICS2007_TTL,GEO_TTL,EMP,LFO_TTL,ESTAB,PAYANN'
county_fips = '027'
state_fips = '42'
key ='str'
url= 'https://api.census.gov/data/2011/cbp?get='+my_vars+'&for=county:'+county_fips+'&in=state:'+state_fips+'&key='+key
res = requests.get(url)
res.status_code
When I add additional variables like NAICS2007 I receive a status code 400, but when I use the example variables I get a 200. The common denominator seems to be my code. Can anyone help?
image of the CB1100A11 table

This should be moved to comments (I can't comment bc of rep) but as someone who's worked closely with the US Census API I highly recommend using the Census library:
https://github.com/datamade/census
One of my queries looks like this (where acs1dp is the database I am querying):
from census import Census
conn = Census("MY API KEY")
name = 'NAME'
agriculture = 'DP03_0033PE'
laborForce = 'DP03_0003PE'
travelTime = 'DP03_0025E'
highSchool = 'DP02_0066PE'
unemployed = 'DP03_0009PE'
poverty = 'DP03_0128PE'
payload = conn.acs1dp.get((name, travelTime, agriculture, poverty,
unemployed, laborForce, highSchool), {'for': 'state:*'})
which returns each of those column values for all of the states.

Related

How to pull out the list of active German government bonds using xbbg?

I would like to pull out the list of ISINs for existing active german government bonds using the xbbg library but I couldn't find a way to do so.
The Bloomberg ticker used to access the universe in Bloomberg is DBR Govt. The bql query in excel would be the following:
=#BQL.Query("get(ID_ISIN) for(filter(bondsUniv('Active'),TICKER==DBR))";"cols=2;rows=49")
I tried a few things:
1)
from xbbg import blp
query = blp.bdib(ticker='DBR Govt', dt=date.today()).tail()
based on example for equities, but I get an error KeyError: 'Cannot find exchange info for DBR Govt'.
or, for instance,
2)
from xbbg import blp
query = blp.bdp(tickers='DBR Govt', flds=['ID_ISIN'])
but this produces an empty dataframe.
Does anyone have any idea what should I do?
Thanks a lot in advance.
Currently there is no officially published api for BQL via Python.
There are some workarounds in this SO question.
However, in this instance you can retrieve a list of current Bunds via the //blp/instruments service in the DAPI ... with a bit of work. The information on this service is buried on page 49 in the Bloomberg API reference.
The xbbg package does not support this explicitly, but you can use its excellent framework for any DAPI request. Unfortunately I have not found a way to filter the returned list upfront for active bonds: the DAPI function returns every Bund, whether matured or not. However you can do a second bdp query for the bond maturities and filter on that:
from xbbg.core import conn,process
from xbbg import blp
from datetime import date
def allGovts(ticker): #Return all govts with the given ticker, matured or not
req = process.create_request(service='//blp/instruments',request='govtListRequest')
req.set('ticker',ticker)
req.set('partialMatch',False)
req.set('maxResults',1000)
def _process_instruments(msg): #Process the response
for elt in msg.asElement().getElement('results').values():
yield elt.getElementAsString('parseky')
conn.send_request(request=req)
return process.rec_events(func=_process_instruments)
def liveGovts(ticker): #Just return 'live' bonds, ordered by maturity
tdy = date.today()
dfAll = blp.bdp([g for g in allGovts(ticker)],['id_isin','maturity'])
return dfAll[ dfAll['maturity'] > tdy ].sort_values('maturity')
print(liveGovts('DBR'))
with the result:
id_isin maturity
EJ506625 Corp DE0001102309 2023-02-15
EJ677578 Corp DE0001102317 2023-05-15
EJ815896 Corp DE0001102325 2023-08-15
...
ZR097974 Corp DE0001102481 2050-08-15
BR246981 Corp DE0001102572 2052-08-15
BY899086 Corp DE0001102614 2053-08-15
Of course, you may want other additional properties of the bonds, so you can amend the bdp call to include those extra fields.

Problem with getting tweet_fields from Twitter API 2.0 using Tweepy

I have a similar problem as in this question (Problem with getting user.fields from Twitter API 2.0)
but I am using Tweepy. When making the request with tweet_fields, the response is only giving me the default values. In another fuction where I use user_fields it works perfectly.
I followed this guide, specifically number 17 (https://dev.to/twitterdev/a-comprehensive-guide-for-using-the-twitter-api-v2-using-tweepy-in-python-15d9)
My function looks like this:
def get_user_tweets():
client = get_client()
tweets = client.get_users_tweets(id=get_user_id(), max_results=5)
ids = []
for tweet in tweets.data:
ids.append(str(tweet.id))
tweets_info = client.get_tweets(ids=ids, tweet_fields=["public_metrics"])
print(tweets_info)
This is my response (with the last tweets from elonmusk) also there is no error code or anything else
Response(data=[<Tweet id=1471419792770973699 text=#WholeMarsBlog I came to the US with no money & graduated with over $100k in debt, despite scholarships & working 2 jobs while at school>, <Tweet id=1471399837753135108 text=#TeslaOwnersEBay #PPathole #ScottAdamsSays #johniadarola #SenWarren It’s complicated, but hopefully out next quarter, along with Witcher. Lot of internal debate as to whether we should be putting effort towards generalized gaming emulation vs making individual games work well.>, <Tweet id=1471393851843792896 text=#PPathole #ScottAdamsSays #johniadarola #SenWarren Yeah!>, <Tweet id=1471338213549744130 text=link>, <Tweet id=1471325148435394566 text=#24_7TeslaNews #Tesla ❤️>], includes={}, errors=[], meta={})
I found this link: https://giters.com/tweepy/tweepy/issues/1670. According to it,
Response is a namedtuple. Here, within its data field, is a single Tweet object.
The string representation of a Tweet object will only ever include its ID and text. This was an intentional design choice, to reduce the excess of information that could be displayed when printing all the data as the string representation, as with models.Status. The ID and text are the only default / guaranteed fields, so the string representation remains consistent and unique, while still being concise. This design is used throughout the API v2 models.
To access the data of the Tweet object, you can use attributes or keys (like a dictionary) to access each field.
If you want all the data as a dictionary, you can use the data attribute/key.
In that case, to access public metrics, you could maybe try doing this instead:
tweets_info = client.get_tweets(ids=ids, tweet_fields=["public_metrics"])
for tweet in tweets_info.data:
print(tweet["id"])
print(tweet["public_metrics"])

How to get a list of streets from OpenStreetMaps for a populated place in a given country?

I am trying to extract lists of street names by names of populated places from OpenStreetMap / Overpass using e.g. the following Python code:
import requests
overpass_url = "http://overpass-api.de/api/interpreter"
overpass_query = """
[out:json];
area
[name="Fulda"];
way(area)[highway][name];
out;
"""
query = overpass_query
response = requests.get(overpass_url, params={'data': query})
data = response.json()
In this way I can get more or less everything I need, but the problem is how to avoid the ambiguity with the place names: there is a city in Germany called Fulda and a number or places in the US. The code above returns streets in all of them. Is there a possibility to modify the query so that it filters one certain country, e.g. Germany?
(I find BTW that the learning curve of OpenStreetMap is made unnecessarily steep, and it is difficult to find examples in the whole of the internet.)

Tracing Canadian borders with a function. (longitude and latitude boundaries of Canada)

I'm writing a python script that generates random addresses in Canada. To do this, I have to generate random tuples (longitude,latitude) that are within Canadian borders (not in the ocean).
I figured that I can approximate the borders with small rectangles (just like in calculus). Which does the job, but it is not optimal/accurate.
I couldn't find any academic paper/discussion on the web, maybe my searches did not contain the right keywords. Can you help me find the right resources or even answer this question? The programming part is fine, I just need the math!
Thank you
You are talking about reverse geocoding. The easiest way to do this is to make use of the google maps geocoding API.
You can do this without registering, but you are limited to like 4-5 calls per day. You can register for free for a relatively high number of calls (20-25k last I checked) and if you exceed that you have to pay.
import requests
import json
def getplace(lat, lon):
url = "http://maps.googleapis.com/maps/api/geocode/json?"
url += "latlng=%s,%s&sensor=false" % (lat, lon)
data = {'key': 'your-api-key-goes-here'} # If using your free 5 calls, include no data and just doa get request on the url
v = requests.post(url=url, data=data)
j = json.loads(v.text)
components = j['results'][0]['address_components']
country = town = None
for c in components:
if "country" in c['types']:
country = c['long_name']
if "locality" in c['types']:
town = c['long_name']
return town, country
print(getplace(45.425533, -75.69248))
print(getplace(45.525533, -77.69248))
The above outputs:
('Ottawa', 'Canada')
("Barry's Bay", 'Canada')
You can print out the raw response print(v.text to see the data object and find the fields you actually care about

How to generate summary from JSON data Using watson discovery news servies

How to generate summary like IBM from json using discovery news services with python
qopts = {'nested':'(enriched_text.entities)','filter':'(enriched_text.entities.type::Person)','term':'(enriched_text.entities.text,count:10)','filter':'(enriched_text.concepts.text:infosys)','filter':'(enriched_text.concepts.text:ceo)'}
my_query = discovery.query('system', 'news', qopts)
print(json.dumps(my_query, indent=2))
This query is proper or not for find ceo of Infosys ?
Output came in large json format the how I identify answer or create summary like top ten ceo or people.
How to generate summary from json using discovery news services with python. I fire query then output became large json format ..how to find proper summary from that json file my query is correct or not
I believe there are two questions here.
In order to answer a question like "Who is the CEO of Infosys?" I would instead make use of the natural_language_query parameter as follows:
qopts = {'natural_language_query':'Who is the CEO of Infosys?','count':'5'}
response = discovery.query(environment_id='system',collection_id='news',query_options=qopts)
print(json.dumps(response,indent=2))
In order to make use of aggregations, they must be specified in a single aggregation parameter combined with filter aggregations in the query options as follows:
qopts = {'aggregation': 'nested(enriched_text.entities).filter(enriched_text.entities.type::Person).term(enriched_text.entities.text,count:10)', 'filter':'enriched_text.entities:(text:Infosys,type:Company)','count':'0'}
response = discovery.query(environment_id='system',collection_id='news',query_options=qopts}
print(json.dumps(response,indent=2))
Notice that aggregations are chained/combined with the . symbol.

Categories