Skip to content

Commit

Permalink
Rename (#30)
Browse files Browse the repository at this point in the history
* Rename
Closes #22
Closes #23

* Remove run test by filename
  • Loading branch information
femtotrader authored and dysonance committed Oct 31, 2018
1 parent 8ebef6b commit a194e33
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 2 deletions.
32 changes: 31 additions & 1 deletion docs/src/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,34 @@ isalpha("Hi mom") # hide
A = tsread(Pkg.dir("Temporal", "data", "XOM.csv"))
set_sanitize_names_option(true)
B = tsread(Pkg.dir("Temporal", "data", "XOM.csv"))
```
```

## Rename columns

### Not in place

`rename` function return a `TS`.

```@repl
using Temporal
N = 252*3
K = 4
data = cumsum(randn(N,K), dims=1)
dates = today()-Day(N-1):Day(1):today()
ts = TS(data, dates, [:a, :b, :c, :d])
rename(ts, :a => :A, :d => :D)
rename(ts, Dict(:a => :A, :d => :D)...)
rename(Symbol ∘ uppercase ∘ string, ts)
rename(uppercase, ts, String)
```

### In place

`rename!` function modify name of columns in place and return `true` when a `TS` is renamed.

```julia
rename!(ts, :a => :A, :d => :D)
rename!(ts, Dict(:a => :A, :d => :D)...)
rename!(Symbol uppercase string, ts)
rename!(uppercase, ts, String)
```
1 change: 1 addition & 0 deletions src/Temporal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Temporal
ones, zeros, trues, falses, isnan, countnz, sign, round,
sum, mean, maximum, minimum, prod, cumsum, cumprod, diff, lag, nans,
shift, pct_change,
rename!, rename,
# Aggregation
mondays, tuesdays, wednesdays, thursdays, fridays, saturdays, sundays,
bow, eow, bom, eom, boq, eoq, boy, eoy, collapse, apply,
Expand Down
47 changes: 47 additions & 0 deletions src/ts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,50 @@ ndims(::TS) = 2
float(x::TS) = ts(float(x.values), x.index, x.fields)
eltype(x::TS) = eltype(x.values)
copy(x::TS) = TS(x.values, x.index, x.fields)

################################################################################
# RENAME #######################################################################
################################################################################
# in place
function rename!(ts::TS, args::Pair{Symbol, Symbol}...)
d = Dict{Symbol, Symbol}(args...)
flag = false
for (i, field) in enumerate(ts.fields)
if field in keys(d)
ts.fields[i] = d[field]
flag = true
end
end
flag
end

function rename!(f::Base.Callable, ts::TS, colnametyp::Type{Symbol} = Symbol)
for (i, field) in enumerate(ts.fields)
ts.fields[i] = f(field)
end
true
end

function rename!(f::Base.Callable, ts::TS, colnametyp::Type{String})
f = Symbol f string
rename!(f, ts)
end

# not in place
function rename(ts::TS, args::Pair{Symbol, Symbol}...)
ts2 = copy(ts)
rename!(ts2, args...)
ts2
end

function rename(f::Base.Callable, ts::TS, colnametyp::Type{Symbol} = Symbol)
ts2 = copy(ts)
rename!(f, ts2, colnametyp)
ts2
end

function rename(f::Base.Callable, ts::TS, colnametyp::Type{String})
ts2 = copy(ts)
rename!(f, ts2, colnametyp)
ts2
end
82 changes: 82 additions & 0 deletions test/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,86 @@ using Test, Dates, Temporal
end
end

@testset "Rename" begin
@testset "inplace" begin
@testset "Pair" begin
ts = TS(data, times, [:a, :b, :c, :d])
rename!(ts, :a => :A)
@test ts.fields == [:A, :b, :c, :d]
end

@testset "Several Pairs" begin
ts = TS(data, times, [:a, :b, :c, :d])
rename!(ts, :a => :A, :d => :D)
@test ts.fields == [:A, :b, :c, :D]
end

@testset "Dict" begin
ts = TS(data, times, [:a, :b, :c, :d])
d = Dict(:a => :A, :d => :D)
rename!(ts, d...)
@test ts.fields == [:A, :b, :c, :D]
end

@testset "Lambda Function" begin
ts = TS(data, times, [:a, :b, :c, :d])
f = colname -> Symbol(uppercase(string(colname)))
rename!(f, ts)
@test ts.fields == [:A, :B, :C, :D]
end

@testset "Function with composition" begin
ts = TS(data, times, [:a, :b, :c, :d])
f = Symbol uppercase string
rename!(f, ts)
@test ts.fields == [:A, :B, :C, :D]
end

@testset "Function with do block" begin
ts = TS(data, times, [:a, :b, :c, :d])
rename!(ts) do x
x |> string |> uppercase |> Symbol
end
@test ts.fields == [:A, :B, :C, :D]
end

@testset "Function with automatic String/Symbol" begin
ts = TS(data, times, [:a, :b, :c, :d])
rename!(uppercase, ts, String)
@test ts.fields == [:A, :B, :C, :D]
end
end

@testset "not in place" begin
@testset "Pair" begin
ts1 = TS(data, times, [:a, :b, :c, :d])
ts2 = rename(ts1, :a => :A)
@test ts1.fields == [:a, :b, :c, :d]
@test ts2.fields == [:A, :b, :c, :d]
end

@testset "Several Pairs" begin
ts1 = TS(data, times, [:a, :b, :c, :d])
ts2 = rename(ts1, :a => :A, :d => :D)
@test ts1.fields == [:a, :b, :c, :d]
@test ts2.fields == [:A, :b, :c, :D]
end

@testset "Lambda Function" begin
ts1 = TS(data, times, [:a, :b, :c, :d])
f = colname -> Symbol(uppercase(string(colname)))
ts2 = rename(f, ts1)
@test ts1.fields == [:a, :b, :c, :d]
@test ts2.fields == [:A, :B, :C, :D]
end

@testset "Function with automatic String/Symbol" begin
ts1 = TS(data, times, [:a, :b, :c, :d])
ts2 = rename(uppercase, ts1, String)
@test ts1.fields == [:a, :b, :c, :d]
@test ts2.fields == [:A, :B, :C, :D]
end
end
end

# end
1 change: 0 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ test_scripts = [
for test_script in test_scripts
include(test_script)
end

0 comments on commit a194e33

Please sign in to comment.