Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/src/standalonequerycommands.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,18 +433,18 @@ println(q)
│ 2 │ 3 │ 5 │
```

## The `@dissallowna` command
## The `@disallowna` command

The `@dissallowna` command has the form `source |> @dissallowna(columns...)`. `source` can be any source that can be queried and that has a table structure. If `@dissallowna()` is called without any arguments, it will check that there are no missing `NA` values in any column in any row of the input table and convert the element type of each column to one that cannot hold missing values. Alternatively one can pass a list of column names to `@dissallowna`, in which case it will only check for `NA` values in those columns, and only convert those columns to a type that cannot hold missing values.
The `@disallowna` command has the form `source |> @disallowna(columns...)`. `source` can be any source that can be queried and that has a table structure. If `@disallowna()` is called without any arguments, it will check that there are no missing `NA` values in any column in any row of the input table and convert the element type of each column to one that cannot hold missing values. Alternatively one can pass a list of column names to `@disallowna`, in which case it will only check for `NA` values in those columns, and only convert those columns to a type that cannot hold missing values.

Our first example uses the simple version of `@dissallowna()` that makes sure there are no missing values anywhere in the table. Note how the column type for column `a` is changed to `Int64` in this example, i.e. an element type that does not support missing values:
Our first example uses the simple version of `@disallowna()` that makes sure there are no missing values anywhere in the table. Note how the column type for column `a` is changed to `Int64` in this example, i.e. an element type that does not support missing values:

```jldoctest
using Query, DataFrames

df = DataFrame(a=[1,missing,3], b=[4,5,6])

q = df |> @filter(!isna(_.a)) |> @dissallowna() |> DataFrame
q = df |> @filter(!isna(_.a)) |> @disallowna() |> DataFrame

println(q)

Expand All @@ -465,7 +465,7 @@ using Query, DataFrames

df = DataFrame(a=[1,2,missing], b=[4,missing,5])

q = df |> @filter(!isna(_.b)) |> @dissallowna(:b) |> DataFrame
q = df |> @filter(!isna(_.b)) |> @disallowna(:b) |> DataFrame

println(q)

Expand Down
2 changes: 1 addition & 1 deletion src/Query.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export @from, @query, @count, Grouping, key
export @map, @filter, @groupby, @orderby, @orderby_descending, @unique,
@thenby, @thenby_descending, @groupjoin, @join, @mapmany, @take, @drop

export @select, @rename, @mutate, @dissallowna, @dropna, @replacena
export @select, @rename, @mutate, @disallowna, @dropna, @replacena

export isna, NA

Expand Down
8 changes: 4 additions & 4 deletions src/table_query_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,20 @@ our_get(x::DataValue) = get(x)
our_get(x, y) = x
our_get(x::DataValue, y) = get(x, y)

macro dissallowna()
macro disallowna()
return :( Query.@map(map(our_get, _)) )
end

macro dissallowna(columns...)
macro disallowna(columns...)
return :( Query.@mutate( $( ( :( $(columns[i].value) = our_get(_.$(columns[i].value)) ) for i=1:length(columns) )... ) ) )
end

macro dropna()
return :( i-> i |> Query.@filter(!any(isna, _)) |> Query.@dissallowna() )
return :( i-> i |> Query.@filter(!any(isna, _)) |> Query.@disallowna() )
end

macro dropna(columns...)
return :( i-> i |> Query.@filter(!any(($((:(isna(_.$(columns[i].value))) for i in 1:length(columns) )...),))) |> Query.@dissallowna($(columns...)) )
return :( i-> i |> Query.@filter(!any(($((:(isna(_.$(columns[i].value))) for i in 1:length(columns) )...),))) |> Query.@disallowna($(columns...)) )
end

macro replacena(arg, args...)
Expand Down
10 changes: 5 additions & 5 deletions test/test_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ end
@test df |> @replacena(:a=>2, :b=>8) |> collect == [(a=1,b=1.), (a=2, b=2.), (a=3, b=3.)]
end

@testset "@dissallowna" begin
@testset "@disallowna" begin

df = DataFrame(a=[1,missing,3], b=[1.,2.,3.])

@test_throws DataValueException df |> @dissallowna() |> collect
@test df |> @filter(!any(isna, _)) |> @dissallowna() |> collect == [(a=1,b=1.), (a=3, b=3.)]
@test_throws DataValueException df |> @dissallowna(:a) |> collect
@test df |> @dissallowna(:b) |> collect == [(a=DataValue(1),b=1.), (a=DataValue{Int}(),b=2.),(a=DataValue(3), b=3.)]
@test_throws DataValueException df |> @disallowna() |> collect
@test df |> @filter(!any(isna, _)) |> @disallowna() |> collect == [(a=1,b=1.), (a=3, b=3.)]
@test_throws DataValueException df |> @disallowna(:a) |> collect
@test df |> @disallowna(:b) |> collect == [(a=DataValue(1),b=1.), (a=DataValue{Int}(),b=2.),(a=DataValue(3), b=3.)]
end