Hi - I have a web request that returns JSON that I'm trying to get into a file geodatabase table. When I execute arcpy.da.NumPyArrayToTable, I get the following error:
RuntimeError: create table
This doesn't give me much to go on, so I'm not sure what to try next. Here is my code:
<SPAN class="keyword token">from</SPAN> pathlib <SPAN class="keyword token">import</SPAN> Path
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> numpy
<SPAN class="keyword token">import</SPAN> pandas
<SPAN class="keyword token">import</SPAN> requests
proxyDict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"http"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="string token">"<http_URL>"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"https"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="string token">"<https_URL>"</SPAN>
<SPAN class="punctuation token">}</SPAN>
url <SPAN class="operator token">=</SPAN> <SPAN class="string token">'https://api.census.gov/data/2018/acs/acs5?get=NAME,group(B18102)&for=county:*&in=state:53'</SPAN>
fgdb <SPAN class="operator token">=</SPAN> Path<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'C:/Temp/ACS2018/ACS2018.gdb'</SPAN><SPAN class="punctuation token">)</SPAN>
table <SPAN class="operator token">=</SPAN> fgdb <SPAN class="operator token">/</SPAN> <SPAN class="string token">'test_table'</SPAN>
r <SPAN class="operator token">=</SPAN> requests<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN>url<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># r = requests.get(url, proxies=proxyDict) # if you have a proxy server</SPAN>
df <SPAN class="operator token">=</SPAN> pandas<SPAN class="punctuation token">.</SPAN>read_json<SPAN class="punctuation token">(</SPAN>r<SPAN class="punctuation token">.</SPAN>text<SPAN class="punctuation token">)</SPAN>
headers <SPAN class="operator token">=</SPAN> df<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># first row is headers</SPAN>
header_list <SPAN class="operator token">=</SPAN> headers<SPAN class="punctuation token">.</SPAN>values<SPAN class="punctuation token">.</SPAN>tolist<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># convert to list</SPAN>
header_list<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="string token">'FULL_NAME'</SPAN> <SPAN class="comment token"># de-duplicate "NAME"</SPAN>
new_columns <SPAN class="operator token">=</SPAN> dict<SPAN class="punctuation token">(</SPAN>zip<SPAN class="punctuation token">(</SPAN>list<SPAN class="punctuation token">(</SPAN>range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>len<SPAN class="punctuation token">(</SPAN>header_list<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> header_list<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#make dict</SPAN>
df2 <SPAN class="operator token">=</SPAN> df<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># data in rows 2+</SPAN>
df3 <SPAN class="operator token">=</SPAN> df2<SPAN class="punctuation token">.</SPAN>rename<SPAN class="punctuation token">(</SPAN>columns<SPAN class="operator token">=</SPAN>new_columns<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># add column names back in</SPAN>
<SPAN class="comment token"># Convert to numpy array, then to table</SPAN>
numpy_array <SPAN class="operator token">=</SPAN> numpy<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN>numpy<SPAN class="punctuation token">.</SPAN>rec<SPAN class="punctuation token">.</SPAN>fromrecords<SPAN class="punctuation token">(</SPAN>df3<SPAN class="punctuation token">.</SPAN>values<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
names <SPAN class="operator token">=</SPAN> df3<SPAN class="punctuation token">.</SPAN>dtypes<SPAN class="punctuation token">.</SPAN>index<SPAN class="punctuation token">.</SPAN>tolist<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
numpy_array<SPAN class="punctuation token">.</SPAN>dtype<SPAN class="punctuation token">.</SPAN>names <SPAN class="operator token">=</SPAN> tuple<SPAN class="punctuation token">(</SPAN>names<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>NumPyArrayToTable<SPAN class="punctuation token">(</SPAN>numpy_array<SPAN class="punctuation token">,</SPAN> str<SPAN class="punctuation token">(</SPAN>table<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>I also tried df3.to_numpy(), but got the same result.