Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constructor from TS to TS. Add Base.copy and Base.similar with the DataFrame semantics. #34

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
/Manifest.toml
/docs/Manifest.toml
/docs/build/
/test/Manifest.toml
/test/Manifest.toml

# Editors
.vscode
3 changes: 3 additions & 0 deletions src/TS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,6 @@ function TS(coredata::AbstractArray{T,2}, index::AbstractVector{V}) where {T, V}
df = DataFrame(coredata, :auto, copycols=true)
TS(df, index)
end

# To avoid error on using a TS to create a TS
TS(ts::TS) = ts
6 changes: 5 additions & 1 deletion src/TSx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module TSx

using DataFrames, Dates, ShiftedArrays, RecipesBase, RollingFunctions

import Base.copy
import Base.convert
import Base.diff
import Base.filter
Expand All @@ -16,8 +17,9 @@ import Base.names
import Base.print
import Base.==
import Base.show
import Base.summary
import Base.similar
import Base.size
import Base.summary
import Base.vcat

import Dates.Period
Expand All @@ -30,6 +32,7 @@ export TS,
Matrix,
apply,
convert,
copy,
cbind,
describe,
diff,
Expand All @@ -55,6 +58,7 @@ export TS,
log,
rbind,
show,
similar,
size,
subset,
summary,
Expand Down
91 changes: 86 additions & 5 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ julia> names(TS([1:10 11:20]))
"x2"
```
"""

function names(ts::TS)
names(ts.coredata[!, Not(:Index)])
end
Expand Down Expand Up @@ -242,8 +242,8 @@ Returns the first `n` rows of `ts`.
julia> head(TS(1:100))
(10 x 1) TS with Int64 Index

Index x1
Int64 Int64
Index x1
Int64 Int64
──────────────
1 1
2 2
Expand Down Expand Up @@ -274,8 +274,8 @@ Returns the last `n` rows of `ts`.
julia> tail(TS(1:100))
(10 x 1) TS with Int64 Index

Index x1
Int64 Int64
Index x1
Int64 Int64
──────────────
91 91
92 92
Expand All @@ -292,3 +292,84 @@ julia> tail(TS(1:100))
function tail(ts::TS, n::Int = 10)
TS(DataFrames.last(ts.coredata, n))
end


# Base.copy
"""
# Copy

```julia
copy(ts::TS; copycols::Bool=true)
```

Copy the time series `ts` following the semantics of `copy(::DataFrame)`. If `copycols=true` (the default),
return a new `TS` holding copies of column vectors in `ts`. If `copycols=false`, return a new
`TS` sharing column vectors with `ts`.

```jldoctest; setup = :(using TSx, DataFrames, Dates, Random)
julia> copy(TS(1:10))
(10 x 1) TS with Int64 Index

Index x1
Int64 Int64
──────────────
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
```
"""
Base.copy(ts::TS; copycols::Bool=true) = TS(copy(ts.coredata; copycols))


# Base.similar
"""
# Similar

```julia
similar(ts::TS, rows::Integer=nrow(ts))
```

Create a new `TS` with the same column names and column element types as `ts`. An optional
second argument can be provided to request a number of rows that is different than the number
of rows present in `ts`.

```jldoctest; setup = :(using TSx, DataFrames, Dates, Random)

julia> similar(TS(1:10))
(10 x 1) TS with Int64 Index

Index x1
Int64 Int64
──────────────────────────────────
3 3
3 3
20 20
32 32
32 32
100 100
250 250
1000 1000
3329 3329
140268692646400 140268692646400

julia> similar(TS(1:10), 4)
(4 x 1) TS with Int64 Index

Index x1
Int64 Int64
──────────────────────────────────
140268691629264 140267030569584
140268691701168 140267030569616
140268691701168 140267030569648
140268691731376 140268776059472

```
"""
Base.similar(ts::TS, rows::Integer=nrow(ts)) = TS(DataFrames::similar(ts.coredata, rows))