On Saturday, December 13, 2014 12:27:11 PM UTC-8, Jameson wrote:
>
> perhaps `filter(df) do row; ipaddr in row[:net]; end`? this might really
> be a dataframes question however, since I don't know iteration over a
> dataframe works
>
> On Sat Dec 13 2014 at 3:20:44 PM S <[email protected] <javascript:>>
> wrote:
>
>> Hi all,
>>
>> I have a dilemma:
>>
>> I currently have a type representing an IP address (it's the IPv4 object
>> from socket.jl with some additional constructors) and another type
>> representing an IP network. I've extended in and contains to these
>> respective types, so that, for instance, contains(IPv4network("1.2.3.0/24")
>> , IPv4("1.2.3.4")) yields true.
>>
>> Now the issue. I have a dataframe with a column (array) of IPv4networks,
>> and an IPv4 address I'd like to test against each element in the array,
>> returning the entire dataframe row if the IPv4network contains the IP
>> address. I could use a loop, but I think I'd prefer a vector operation
>> (for elegance, and so I can learn how to do this properly). That is, I'd
>> like to be able to do something like [row for row in df if df[row,:net]
>> contains ipaddr] vectorized (and, of course, conditionals aren't allowed
>> in array comprehensions in any case).
>>
>> I've looked into @vectorize_2arg but it doesn't seem to allow for
>> different types, and @vectorize_2arg Any IPnetwork.contains throws
>> warnings (though it appears to produce the correct output):
>>
>> julia> @vectorize_2arg Any IPnetwork.contains
>> Warning: New definition
>> contains(AbstractArray{T1,N},T2) at operators.jl:372
>> is ambiguous with:
>> contains(T1,AbstractArray{T2,N}) at operators.jl:370.
>> To fix, define
>> contains(_<:AbstractArray{T1,N},_<:AbstractArray{T2,N})
>> before the new definition.
>> contains (generic function with 4 methods)
>>
>> What's the best way to do this? Thanks for any advice.
>>
>
Thanks. Yeah, I'm not sure filter()'s going to work with DataArrays:
julia> filter(x->IPnetwork.contains(x,a), df[:net])
ERROR: type: typeassert: expected AbstractArray{Bool,N}, got
DataArray{Any,1}
in filter at array.jl:1209
I'll just use a loop for now, but that's really inelegant to my eyes :)