Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia readtable from a stream instead of from a file

Is there a way to read a table from a network url or from a runpipe external command? It seems that DataFrame.readtable only supports reading from file.

For example in R we could do:

df = read.table(url("http://example.com/data.txt"))

x = read.table(pipe("zcat data.txt | sed /^#/d  | cut  -f '11-13'"), colClasses=c("integer","integer","integer"), fill=TRUE, row.names=NULL)
like image 279
Phuoc Avatar asked May 19 '17 07:05

Phuoc


1 Answers

using DataFrames, Requests

julia> resp = get("https://data.cityofnewyork.us/api/views/kku6-nxdu/rows.csv?accessType=DOWNLOAD")
Response(200 OK, 17 headers, 27350 bytes in body)

julia> tbl = readtable(IOBuffer(resp.data));

julia> names(tbl)
46-element Array{Symbol,1}:
 :JURISDICTION_NAME                  
 :COUNT_PARTICIPANTS                 
 :COUNT_FEMALE                       
 :PERCENT_FEMALE                     
 :COUNT_MALE                         
 :PERCENT_MALE                       
 :COUNT_GENDER_UNKNOWN               
 :PERCENT_GENDER_UNKNOWN             
 :COUNT_GENDER_TOTAL                 
 :PERCENT_GENDER_TOTAL               
 :COUNT_PACIFIC_ISLANDER             
 :PERCENT_PACIFIC_ISLANDER           
 :COUNT_HISPANIC_LATINO              
 :PERCENT_HISPANIC_LATINO            
 :COUNT_AMERICAN_INDIAN              
 :PERCENT_AMERICAN_INDIAN            
 :COUNT_ASIAN_NON_HISPANIC           
 ⋮                                   
 :PERCENT_PERMANENT_RESIDENT_ALIEN   
 :COUNT_US_CITIZEN                   
 :PERCENT_US_CITIZEN                 
 :COUNT_OTHER_CITIZEN_STATUS         
 :PERCENT_OTHER_CITIZEN_STATUS       
 :COUNT_CITIZEN_STATUS_UNKNOWN       
 :PERCENT_CITIZEN_STATUS_UNKNOWN     
 :COUNT_CITIZEN_STATUS_TOTAL         
 :PERCENT_CITIZEN_STATUS_TOTAL       
 :COUNT_RECEIVES_PUBLIC_ASSISTANCE   
 :PERCENT_RECEIVES_PUBLIC_ASSISTANCE 
 :COUNT_NRECEIVES_PUBLIC_ASSISTANCE  
 :PERCENT_NRECEIVES_PUBLIC_ASSISTANCE
 :COUNT_PUBLIC_ASSISTANCE_UNKNOWN    
 :PERCENT_PUBLIC_ASSISTANCE_UNKNOWN  
 :COUNT_PUBLIC_ASSISTANCE_TOTAL      
 :PERCENT_PUBLIC_ASSISTANCE_TOTAL

julia> eltypes(tbl)
46-element Array{Type,1}:
 Int64  
 Int64  
 Int64  
 Float64
 Int64  
 Float64
 Int64  
 Int64  
 Int64  
 Int64  
 Int64  
 Float64
 Int64  
 Float64
 Int64  
 Float64
 Int64  
 ⋮      
 Float64
 Int64  
 Float64
 Int64  
 Float64
 Int64  
 Int64  
 Int64  
 Int64  
 Int64  
 Float64
 Int64  
 Float64
 Int64  
 Int64  
 Int64  
 Int64 
like image 110
Yasha Avatar answered Sep 30 '22 19:09

Yasha