Skip to content

Commit

Permalink
Merge pull request #83 from alonsoC1s/main
Browse files Browse the repository at this point in the history
Implementing functions proposed in #48
  • Loading branch information
Karandeep Singh authored Apr 12, 2023
2 parents 83e469e + bef75ce commit 8207d8a
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 4 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Tidier.jl updates

## v0.7.4 - 2023-04-11
- Added `as_float()`, `as_integer()`, and `as_string()`

## v0.7.3 - 2023-04-10
- Added `@glimpse()`

Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Tidier"
uuid = "f0413319-3358-4bb0-8e7c-0c83523a93bd"
authors = ["Karandeep Singh"]
version = "0.7.3"
version = "0.7.4"

[deps]
Chain = "8be319e6-bccf-4806-a6f7-6fae938471bc"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Tidier.jl also supports the following helper functions:
- `ntile()`
- `lag()` and `lead()`
- `starts_with()`, `ends_with()`, `matches()`, and `contains()`
- `as_float()`, `as_integer()`, and `as_string()`

See the documentation [Home](https://tidierorg.github.io/Tidier.jl/dev/) page for a guide on how to get started, or the [Reference](https://tidierorg.github.io/Tidier.jl/dev/reference/) page for a detailed guide to each of the macros and functions.

Expand Down
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Tidier.jl also supports the following helper functions:
- `ntile()`
- `lag()` and `lead()`
- `starts_with()`, `ends_with()`, `matches()`, and `contains()`
- `as_float()`, `as_integer()`, and `as_string()`
```

See the [Reference](https://tidierorg.github.io/Tidier.jl/dev/reference/) page for a detailed guide to each of the macros and functions.
Expand Down
7 changes: 4 additions & 3 deletions src/Tidier.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ using Reexport
@reexport using ShiftedArrays: lag, lead

export Tidier_set, across, desc, n, row_number, starts_with, ends_with, matches, if_else, case_when, ntile,
@select, @transmute, @rename, @mutate, @summarize, @summarise, @filter, @group_by, @ungroup, @slice,
@arrange, @distinct, @pull, @left_join, @right_join, @inner_join, @full_join, @pivot_wider, @pivot_longer,
@bind_rows, @bind_cols, @clean_names, @count, @tally, @drop_na, @glimpse
as_float, as_integer, as_string, @select, @transmute, @rename, @mutate, @summarize, @summarise, @filter,
@group_by, @ungroup, @slice, @arrange, @distinct, @pull, @left_join, @right_join, @inner_join, @full_join,
@pivot_wider, @pivot_longer, @bind_rows, @bind_cols, @clean_names, @count, @tally, @drop_na, @glimpse

# Package global variables
const code = Ref{Bool}(false) # output DataFrames.jl code?
Expand All @@ -35,6 +35,7 @@ include("conditionals.jl")
include("pseudofunctions.jl")
include("helperfunctions.jl")
include("ntile.jl")
include("type_conversions.jl")

# Function to set global variables
"""
Expand Down
78 changes: 78 additions & 0 deletions src/docstrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1869,4 +1869,82 @@ Groups: a [100]
.b Int64 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
.c String a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,
```
"""

const docstring_as_float =
"""
as_float(value)
Convert a number or string to a Float64 data type.
This is a useful helper for type conversions. Missing values are propagated.
# Arguments
- `value`: An `AbstractString`, `Number`, or `missing` value.
# Examples
```jldoctest
julia> as_float(1)
1.0
julia> as_float("1.5")
1.5
julia> as_float(missing)
missing
```
"""

const docstring_as_integer =
"""
as_integer(value)
Convert a number or string to an Int64 data type.
This is a useful helper for type conversions. Missing values are propagated. Any values after the decimal point are removed.
# Arguments
- `value`: An `AbstractString`, `Number`, or `missing` value.
# Examples
```jldoctest
julia> as_integer(1)
1
julia> as_integer(1.5)
1
julia> as_integer("2")
2
julia> as_integer("2.5")
2
julia> as_integer(missing)
missing
```
"""

const docstring_as_string =
"""
as_string(value)
Convert a number or string to a String data type.
This is a useful helper for type conversions. Missing values are propagated.
# Arguments
- `value`: An `AbstractString`, `Number`, or `missing` value.
# Examples
```jldoctest
julia> as_string(1)
"1"
julia> as_string(1.5)
"1.5"
julia> as_string(missing)
missing
```
"""
47 changes: 47 additions & 0 deletions src/type_conversions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
$docstring_as_float
"""
function as_float(value)
try
passmissing(convert)(Float64, value)
catch
missing # if parsing failure
end
end

function as_float(value::AbstractString)
try
passmissing(parse)(Float64, value)
catch
missing # if parsing failure
end
end

"""
$docstring_as_integer
"""
function as_integer(value)
try
passmissing(floor)(value) |>
x -> passmissing(convert)(Int64, x)
catch
missing # if parsing failure
end
end

function as_integer(value::AbstractString)
try
passmissing(parse)(Float64, value) |>
x -> passmissing(floor)(x) |>
x -> passmissing(convert)(Int64, x)
catch
missing # if parsing failure
end
end

"""
$docstring_as_string
"""
function as_string(value)
passmissing(string)(value)
end

2 comments on commit 8207d8a

@kdpsingh
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/81458

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.4 -m "<description of version>" 8207d8a71d0a406ec11fb47ba9c503e9885b4ee4
git push origin v0.7.4

Please sign in to comment.