diff --git a/Benchmark.md b/Benchmark.md index 26e302e..02bef46 100644 --- a/Benchmark.md +++ b/Benchmark.md @@ -1,7 +1,7 @@ -# TimeFrames Benchmarks +# TSFrames Benchmarks ``` -import .TimeFrames +import .TSFrames using DataFrames, Dates, BenchmarkTools, MarketData BenchmarkTools.DEFAULT_PARAMETERS.samples = 10 df = DataFrame(ohlc) @@ -26,9 +26,9 @@ Rows: 500 Columns: timestamp, Open, High, Low, Close -## Creating a TimeFrame object from DataFrame +## Creating a TSFrame object from DataFrame ``` -@benchmark TimeFrames.TimeFrame(data) setup=(data = df) +@benchmark TSFrames.TSFrame(data) setup=(data = df) ``` ``` Range (min … max): 20.400 μs … 59.300 μs ┊ GC (min … max): 0.00% … 0.00% @@ -63,7 +63,7 @@ BenchmarkTools.Trial: 10 samples with 1 evaluation. ## Apply methods ``` -@benchmark TimeFrames.apply(data, Month, mean,[:Open, :Close]) setup=(data=ts) +@benchmark TSFrames.apply(data, Month, mean,[:Open, :Close]) setup=(data=ts) ``` ``` @@ -73,7 +73,7 @@ BenchmarkTools.Trial: 10 samples with 1 evaluation. ``` ``` -@benchmark TimeFrames.apply(data, Year, sum,[:Low, :Close]) setup=(data=ts) +@benchmark TSFrames.apply(data, Year, sum,[:Low, :Close]) setup=(data=ts) ``` ``` Range (min … max): 279.600 μs … 508.600 μs ┊ GC (min … max): 0.00% … 0.00% @@ -85,7 +85,7 @@ BenchmarkTools.Trial: 10 samples with 1 evaluation. ### Lag ``` - @benchmark TimeFrames.lag(data,2) setup=(data = ts) + @benchmark TSFrames.lag(data,2) setup=(data = ts) ``` ``` Range (min … max): 47.400 μs … 117.900 μs ┊ GC (min … max): 0.00% … 0.00% @@ -95,7 +95,7 @@ BenchmarkTools.Trial: 10 samples with 1 evaluation. ### Diff ``` - @benchmark TimeFrames.diff(data,1) setup=(data = ts) + @benchmark TSFrames.diff(data,1) setup=(data = ts) ``` ``` Range (min … max): 155.300 μs … 257.700 μs ┊ GC (min … max): 0.00% … 0.00% @@ -105,7 +105,7 @@ Range (min … max): 155.300 μs … 257.700 μs ┊ GC (min … max): 0.00% ### pctchange ``` -@benchmark TimeFrames.pctchange(data,1) setup=(data = ts) +@benchmark TSFrames.pctchange(data,1) setup=(data = ts) ``` ``` Range (min … max): 161.300 μs … 263.900 μs ┊ GC (min … max): 0.00% … 0.00% @@ -116,7 +116,7 @@ Range (min … max): 155.300 μs … 257.700 μs ┊ GC (min … max): 0.00% ## Log returns ``` -@benchmark TimeFrames.computelogreturns(data[1:500,2]) setup = (data = ts) +@benchmark TSFrames.computelogreturns(data[1:500,2]) setup = (data = ts) ``` ``` Range (min … max): 86.000 μs … 199.600 μs ┊ GC (min … max): 0.00% … 0.00% @@ -125,7 +125,7 @@ Time (mean ± σ): 104.520 μs ± 34.220 μs ┊ GC (mean ± σ): 0.00% ± ``` ``` -@benchmark TimeFrames.rollapply(mean, data, 3, 5) setup = (data = ts) +@benchmark TSFrames.rollapply(mean, data, 3, 5) setup = (data = ts) ``` ``` Range (min … max): 29.600 μs … 82.700 μs ┊ GC (min … max): 0.00% … 0.00% @@ -138,12 +138,12 @@ Time (mean ± σ): 104.520 μs ± 34.220 μs ┊ GC (mean ± σ): 0.00% ± v = [i for i in 1:2:500] df2 = df[v,[:timestamp,:Open,:High,:Low,:Close]] rename!(df2, :Open => :open1, :High => :high1, :Low => :low1, :Close => :close1) -ts2 = TimeFrames.TimeFrame(df2) +ts2 = TSFrames.TSFrame(df2) ``` ### Left Join ``` -@benchmark TimeFrames.leftjoin(ts,ts2) +@benchmark TSFrames.leftjoin(ts,ts2) ``` ``` Range (min … max): 107.100 μs … 220.100 μs ┊ GC (min … max): 0.00% … 0.00% @@ -154,7 +154,7 @@ ts2 = TimeFrames.TimeFrame(df2) ### Right Join ``` -@benchmark TimeFrames.rightjoin(ts,ts2) +@benchmark TSFrames.rightjoin(ts,ts2) ``` ``` Range (min … max): 96.700 μs … 206.400 μs ┊ GC (min … max): 0.00% … 0.00% @@ -165,7 +165,7 @@ ts2 = TimeFrames.TimeFrame(df2) ### Inner Join ``` -@benchmark TimeFrames.innerjoin(ts,ts2) +@benchmark TSFrames.innerjoin(ts,ts2) ``` ``` Range (min … max): 71.800 μs … 180.400 μs ┊ GC (min … max): 0.00% … 0.00% @@ -176,7 +176,7 @@ ts2 = TimeFrames.TimeFrame(df2) ### Outer Join ``` - @benchmark TimeFrames.outerjoin(ts,ts2) + @benchmark TSFrames.outerjoin(ts,ts2) ``` ``` Range (min … max): 114.300 μs … 228.800 μs ┊ GC (min … max): 0.00% … 0.00% @@ -189,8 +189,8 @@ ts2 = TimeFrames.TimeFrame(df2) ``` df3 = df[1:300,[:timestamp,:Open,:High,:Low,:Close]] df4 = df[300:end,[:timestamp,:Open,:High,:Low,:Close]] -ts3 = TimeFrames.TimeFrame(df3) -ts4 = TimeFrames.TimeFrame(df4) +ts3 = TSFrames.TSFrame(df3) +ts4 = TSFrames.TSFrame(df4) ``` ``` diff --git a/Project.toml b/Project.toml index 8258abc..294a522 100644 --- a/Project.toml +++ b/Project.toml @@ -1,4 +1,4 @@ -name = "TimeFrames" +name = "TSFrames" uuid = "9f90e835-9451-4aaa-bcb1-743a1b8d2f84" authors = ["Chirag Anand", "xKDR Forum", "Naman Kumar", "Sumeet Suley"] version = "0.1.0" diff --git a/README.md b/README.md index ec197e3..c67ad58 100644 --- a/README.md +++ b/README.md @@ -1,34 +1,34 @@ -# TimeFrames.jl +# TSFrames.jl -[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://xKDR.github.io/TimeFrames.jl/stable) -[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://xKDR.github.io/TimeFrames.jl/dev) -![Build Status](https://github.com/xKDR/TimeFrames.jl/actions/workflows/documentation.yml/badge.svg) -[![codecov](https://codecov.io/gh/xKDR/TimeFrames.jl/branch/main/graph/badge.svg?token=9qkJUtdgrz)](https://codecov.io/gh/xKDR/TimeFrames.jl) +[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://xKDR.github.io/TSFrames.jl/stable) +[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://xKDR.github.io/TSFrames.jl/dev) +![Build Status](https://github.com/xKDR/TSFrames.jl/actions/workflows/documentation.yml/badge.svg) +[![codecov](https://codecov.io/gh/xKDR/TSFrames.jl/branch/main/graph/badge.svg?token=9qkJUtdgrz)](https://codecov.io/gh/xKDR/TSFrames.jl) -TimeFrames is a Julia package to handle timeseries data. It provides a +TSFrames is a Julia package to handle timeseries data. It provides a convenient interface for the commonly used timeseries data -manipulations. TimeFrames is built on top of the powerful and mature +manipulations. TSFrames is built on top of the powerful and mature [DataFrames.jl](https://github.com/JuliaData/DataFrames.jl) making use of the many capabilities of the `DataFrame` type and being easily extensible at the same time. -## Installing TimeFrames +## Installing TSFrames ```julia julia> using Pkg -julia> Pkg.add(url="https://github.com/xKDR/TimeFrames.jl") +julia> Pkg.add(url="https://github.com/xKDR/TSFrames.jl") ``` ## Basic usage -### Creating TimeFrame objects -TimeFrames is a [Tables.jl](https://github.com/JuliaData/Tables.jl) compatible package. This helps in easy conversion between `TimeFrame` objects and other [Tables.jl](https://github.com/JuliaData/Tables.jl) compatible types. For example, to load a `CSV` into a `TimeFrame` object, we do the following. +### Creating TSFrame objects +TSFrames is a [Tables.jl](https://github.com/JuliaData/Tables.jl) compatible package. This helps in easy conversion between `TSFrame` objects and other [Tables.jl](https://github.com/JuliaData/Tables.jl) compatible types. For example, to load a `CSV` into a `TSFrame` object, we do the following. ```julia -julia> using CSV, Dates, DataFrames, TimeFrames +julia> using CSV, Dates, DataFrames, TSFrames -julia> ts = CSV.read("IBM.csv", TimeFrame) -(252 x 6) TimeFrame with Dates.Date Index +julia> ts = CSV.read("IBM.csv", TSFrame) +(252 x 6) TSFrame with Dates.Date Index Index Open High Low Close Adj Close Volume Date Float64 Float64 Float64 Float64 Float64 Int64 @@ -56,13 +56,13 @@ julia> ts = CSV.read("IBM.csv", TimeFrame) 233 rows omitted ``` -As another example of this, consider the following code, which converts a `TimeArray` object to a `TimeFrame` object. For this, we use the `MarketData.yahoo` function from the [MarketData.jl](https://juliaquant.github.io/MarketData.jl/stable/) package, which returns a `TimeArray` object. +As another example of this, consider the following code, which converts a `TimeArray` object to a `TSFrame` object. For this, we use the `MarketData.yahoo` function from the [MarketData.jl](https://juliaquant.github.io/MarketData.jl/stable/) package, which returns a `TimeArray` object. ```julia -julia> using TimeFrames, MarketData; +julia> using TSFrames, MarketData; -julia> TimeFrame(MarketData.yahoo(:AAPL)) -10550×6 TimeFrame with Date Index +julia> TSFrame(MarketData.yahoo(:AAPL)) +10550×6 TSFrame with Date Index Index Open High Low Close AdjClose Volume Date Float64 Float64 Float64 Float64 Float64 Float64 ─────────────────────────────────────────────────────────────────────────────────── @@ -101,7 +101,7 @@ julia> TimeFrame(MarketData.yahoo(:AAPL)) ### Indexing ```julia julia> ts[1:10, [:Close]] -(10 x 1) TimeFrame with Dates.Date Index +(10 x 1) TSFrame with Dates.Date Index Index Close Date Float64 @@ -123,8 +123,8 @@ julia> ts[1:10, [:Close]] ```julia julia> from = Date(2021, 04, 29); to = Date(2021, 06, 02); -julia> TimeFrames.subset(ts, from, to) -(24 x 6) TimeFrame with Date Index +julia> TSFrames.subset(ts, from, to) +(24 x 6) TSFrame with Date Index Index Open High Low Close Adj Close Volume Date Float64 Float64 Float64 Float64 Float64 Int64 @@ -160,7 +160,7 @@ julia> TimeFrames.subset(ts, from, to) ```julia julia> ep = endpoints(ts, Week(1)); julia> ts_weekly = ts[ep] -(52 x 6) TimeFrame with Date Index +(52 x 6) TSFrame with Date Index Index Open_last High_last Low_last Close_last Adj Close_last Volume_last Date Float64 Float64 Float64 Float64 Float64 Int64 @@ -184,9 +184,9 @@ julia> plot(ts_weekly[:, [:Close_last]], size = (600, 400)) ## Documentation -Head to the TimeFrames [user guide](https://xkdr.github.io/TimeFrames.jl/dev/user_guide/) for more +Head to the TSFrames [user guide](https://xkdr.github.io/TSFrames.jl/dev/user_guide/) for more examples and functionality. The API reference is available on the -[documentation](https://xkdr.github.io/TimeFrames.jl/dev/api/) page. +[documentation](https://xkdr.github.io/TSFrames.jl/dev/api/) page. ## Contributions diff --git a/docs/Project.toml b/docs/Project.toml index 732929f..0b6c035 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -6,7 +6,7 @@ MarketData = "945b72a4-3b13-509d-9b46-1525bb5c06de" Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" -TimeFrames = "9f90e835-9451-4aaa-bcb1-743a1b8d2f84" +TSFrames = "9f90e835-9451-4aaa-bcb1-743a1b8d2f84" [compat] Documenter = "0.27.15" diff --git a/docs/make.jl b/docs/make.jl index 875b6c4..1171e9f 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -1,21 +1,21 @@ -using TimeFrames +using TSFrames using Documenter -DocMeta.setdocmeta!(TimeFrames, :DocTestSetup, :(using TimeFrames, DataFrames, Dates, Statistics); recursive=true) +DocMeta.setdocmeta!(TSFrames, :DocTestSetup, :(using TSFrames, DataFrames, Dates, Statistics); recursive=true) makedocs(; - modules=[TimeFrames], + modules=[TSFrames], authors="xKDR Forum", - repo="https://github.com/xKDR/TimeFrames.jl/blob/{commit}{path}#{line}", - sitename="TimeFrames.jl", + repo="https://github.com/xKDR/TSFrames.jl/blob/{commit}{path}#{line}", + sitename="TSFrames.jl", format=Documenter.HTML(; prettyurls=get(ENV, "CI", "false") == "true", - canonical="https://xKDR.github.io/TimeFrames.jl", + canonical="https://xKDR.github.io/TSFrames.jl", assets=String[], ), pages=[ "Introduction" => "index.md", - "Basic demo of TimeFrames" => "demo_finance.md", + "Basic demo of TSFrames" => "demo_finance.md", "User guide" => "user_guide.md", "API reference" => "api.md", ], @@ -24,7 +24,7 @@ makedocs(; ) deploydocs(; - repo="github.com/xKDR/TimeFrames.jl", + repo="github.com/xKDR/TSFrames.jl", devbranch="main", target = "build", ) diff --git a/docs/src/api.md b/docs/src/api.md index a089f03..a91b07a 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -1,14 +1,14 @@ ```@meta -CurrentModule = TimeFrames +CurrentModule = TSFrames ``` -# TimeFrames API reference +# TSFrames API reference -API reference of [TimeFrames](https://github.com/xKDR/TimeFrames.jl). +API reference of [TSFrames](https://github.com/xKDR/TSFrames.jl). ```@index ``` ```@autodocs -Modules = [TimeFrames] +Modules = [TSFrames] ``` diff --git a/docs/src/demo_finance.md b/docs/src/demo_finance.md index e5ee328..da24ac8 100644 --- a/docs/src/demo_finance.md +++ b/docs/src/demo_finance.md @@ -1,44 +1,44 @@ -# Basic demo of TimeFrames using financial data +# Basic demo of TSFrames using financial data -## Create a TimeFrame object for IBM historical data +## Create a TSFrame object for IBM historical data -To load the IBM historical data, we will use the `MarketData.yahoo` function from [MarketData.jl](https://github.com/JuliaQuant/MarketData.jl), which returns the data in the form of a `TimeArray`. We just simply pass this on to the `TimeFrame` constructor. +To load the IBM historical data, we will use the `MarketData.yahoo` function from [MarketData.jl](https://github.com/JuliaQuant/MarketData.jl), which returns the data in the form of a `TimeArray`. We just simply pass this on to the `TSFrame` constructor. ```@repl e1 -using TimeFrames, MarketData, DataFrames, Dates, Plots, Statistics -ibm_ts = TimeFrame(MarketData.yahoo(:IBM)) +using TSFrames, MarketData, DataFrames, Dates, Plots, Statistics +ibm_ts = TSFrame(MarketData.yahoo(:IBM)) ``` -## Create TimeFrame object for AAPL +## Create TSFrame object for AAPL -Similarly, we can create a `TimeFrame` object for the AAPL data. +Similarly, we can create a `TSFrame` object for the AAPL data. ```@repl e1 -aapl_ts = TimeFrame(MarketData.yahoo(:AAPL)) +aapl_ts = TSFrame(MarketData.yahoo(:AAPL)) ``` ## Create a 6-month subset of stock data We would like to compare the stock returns for both the stocks for 6 months starting from June 1, 2021 till December 31, 2021. We use -`TimeFrames.subset` method to create new objects which contain the specified +`TSFrames.subset` method to create new objects which contain the specified duration of data. ```@repl e1 date_from = Date(2021, 06, 01); date_to = Date(2021, 12, 31); -ibm = TimeFrames.subset(ibm_ts, date_from, date_to) +ibm = TSFrames.subset(ibm_ts, date_from, date_to) ``` ```@repl e1 -aapl = TimeFrames.subset(aapl_ts, date_from, date_to) +aapl = TSFrames.subset(aapl_ts, date_from, date_to) ``` ## Combine adjusted closing prices of both stocks into one object We now join (cbind) both the stocks' data into a single object for -further analysis. We use `TimeFrames.join` to create two columns containing +further analysis. We use `TSFrames.join` to create two columns containing adjusted closing prices of both the stocks. The join happens by comparing the `Index` values (dates) of the two objects. The resulting object contains two columns with exactly the same dates for which both @@ -46,13 +46,13 @@ the objects have data, all the other rows are omitted from the result. ```@repl e1 -ibm_aapl = TimeFrames.join(ibm[:, ["AdjClose"]], aapl[:, ["AdjClose"]], JoinBoth) -TimeFrames.rename!(ibm_aapl, [:IBM, :AAPL]) +ibm_aapl = TSFrames.join(ibm[:, ["AdjClose"]], aapl[:, ["AdjClose"]], JoinBoth) +TSFrames.rename!(ibm_aapl, [:IBM, :AAPL]) ``` After the `join` operation the column names are modified because we merged two same-named columns (`AdjClose`) so we use -`TimeFrames.rename!()` method to rename the columns to easily +`TSFrames.rename!()` method to rename the columns to easily remembered stock names. ## Convert data into weekly frequency using last values @@ -70,7 +70,7 @@ ibm_aapl_weekly = ibm_aapl[ep] ```@repl e1 ibm_aapl_weekly_returns = diff(log.(ibm_aapl_weekly)) -TimeFrames.rename!(ibm_aapl_weekly_returns, [:IBM, :AAPL]) +TSFrames.rename!(ibm_aapl_weekly_returns, [:IBM, :AAPL]) ``` ## Compute standard deviation of weekly returns diff --git a/docs/src/index.md b/docs/src/index.md index 7cbc7d8..8b3badb 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -1,6 +1,6 @@ -# TimeFrames.jl +# TSFrames.jl -TimeFrames provides a convenient interface for performing standard +TSFrames provides a convenient interface for performing standard manipulations of timeseries data. The package uses `DataFrame` at it's core to allow powerful data manipulation functions while being lightweight. It is inspired by @@ -8,14 +8,14 @@ lightweight. It is inspired by [xts](https://cran.r-project.org/web/packages/xts/index.html) packages from the [R](https://www.r-project.org/) world. -TimeFrames wraps a familiar syntax for timeseries operations over `DataFrame` +TSFrames wraps a familiar syntax for timeseries operations over `DataFrame` type, thereby, providing the user with full set of `DataFrame` functionalities as well. Integrations with other packages in the Julia ecosystem which are supported by [DataFrames.jl](https://github.com/JuliaData/DataFrames.jl) come to -`TimeFrames` at little cost. +`TSFrames` at little cost. -To start using `TimeFrames.jl` take a look at the [basic demo](demo_finance.md) +To start using `TSFrames.jl` take a look at the [basic demo](demo_finance.md) and then head to the [User guide](user_guide.md). ## User guide diff --git a/docs/src/user_guide.md b/docs/src/user_guide.md index c079d13..6425d1b 100644 --- a/docs/src/user_guide.md +++ b/docs/src/user_guide.md @@ -1,49 +1,49 @@ # User guide -This page describes how to use the TimeFrames package for timeseries data +This page describes how to use the TSFrames package for timeseries data handling. ## Installation ```julia julia> using Pkg -julia> Pkg.add(url="https://github.com/xKDR/TimeFrames.jl") +julia> Pkg.add(url="https://github.com/xKDR/TSFrames.jl") ``` -## Constructing TimeFrame objects +## Constructing TSFrame objects -After installing TimeFrames you need to load the package in Julia -environment. Then, create a basic `TimeFrame` object. +After installing TSFrames you need to load the package in Julia +environment. Then, create a basic `TSFrame` object. ```@repl -using TimeFrames; -ts = TimeFrame(1:10) +using TSFrames; +ts = TSFrame(1:10) ts.coredata ``` -The basic TimeFrame constructor takes in a `Vector` of any type and +The basic TSFrame constructor takes in a `Vector` of any type and automatically generates an index out of it (the `Index` column). -There are many ways to construct a `TimeFrame` object. For real world +There are many ways to construct a `TSFrame` object. For real world applications you would want to read in a CSV file or download a dataset as a `DataFrame` and then operate on it. You can easily convert a -`DataFrame` to a `TimeFrame` object. +`DataFrame` to a `TSFrame` object. ```@repl e1 -using CSV, DataFrames, TimeFrames, Dates +using CSV, DataFrames, TSFrames, Dates dates = Date(2007, 1, 1):Day(1):Date(2008, 03, 06) -ts = TimeFrame(DataFrame(Index=dates, value=10*rand(431))) +ts = TSFrame(DataFrame(Index=dates, value=10*rand(431))) ``` In the above example you generate a random `DataFrame` and convert it -into a `TimeFrame` object `ts`. The top line of the `ts` object tells you the number of +into a `TSFrame` object `ts`. The top line of the `ts` object tells you the number of rows (`431` here) and the number of columns (`1`) along with the `Type` of `Index` (`Dates.Date` in the above example). You can also fetch the number of rows and columns by using `nr(ts)`, `nc(ts)`, and `size(ts)` methods. Respectively, they fetch the number of rows, columns, and a `Tuple` of row and column numbers. A -`length(::TimeFrame)` method is also provided for convenience which returns +`length(::TSFrame)` method is also provided for convenience which returns the number of rows of it's argument. ```@repl e1 @@ -57,24 +57,24 @@ Names of data columns can be fetched using the `names(ts)` method which returns a `Vector{String}` object. The `Index` column can be fetched as an object of `Vector` type by using the `index(ts)` method, it can also be fetched directly using the underlying `coredata` -property of TimeFrame: `ts.coredata[!, :Index]`. +property of TSFrame: `ts.coredata[!, :Index]`. ```@repl e1 names(ts) index(ts) ``` -Another simpler way to read a CSV is to pass `TimeFrame` as a sink to the `CSV.read` function. +Another simpler way to read a CSV is to pass `TSFrame` as a sink to the `CSV.read` function. ```julia-repl -julia> ts = CSV.File(filename, TimeFrame) +julia> ts = CSV.File(filename, TSFrame) ``` ## Indexing and subsetting One of the primary features of a timeseries package is to provide ways -to index or subset a dataset using convenient interfaces. TimeFrames makes it -easier to index a `TimeFrame` object by providing multiple intuitive +to index or subset a dataset using convenient interfaces. TSFrames makes it +easier to index a `TSFrame` object by providing multiple intuitive `getindex` methods which work by just using the regular square parentheses(`[ ]`). @@ -86,7 +86,7 @@ ts[1, [:value]] # using the column name ``` Apart from integer-based row indexing and integer, name based column -indexing, TimeFrames provides special subsetting methods for date and time +indexing, TSFrames provides special subsetting methods for date and time types defined inside the `Dates` module. ```@repl e1 @@ -104,19 +104,19 @@ ts.value # get the value column as a vector ## Summary statistics -The `describe()` method prints summary statistics of the TimeFrame +The `describe()` method prints summary statistics of the TSFrame object. The output is a `DataFrame` which includes the number of missing values, data types of columns along with computed statistical values. ```@repl e1 -TimeFrames.describe(ts) +TSFrames.describe(ts) ``` ## Plotting -A TimeFrame object can be plotted using the `plot()` function of the `Plots` +A TSFrame object can be plotted using the `plot()` function of the `Plots` package. The plotting functionality is provided by `RecipesBase` package so all the flexibility and functionality of the `Plots` package is available for users. @@ -128,7 +128,7 @@ plot(ts, size=(600,400); legend=false) ## Applying a function over a period -The `apply` method allows you to aggregate the TimeFrame object over a period +The `apply` method allows you to aggregate the TSFrame object over a period type (`Dates.Period`(@ref)) and return the output of applying the function on each period. For example, to convert frequency of daily timeseries to monthly you may use `first()`, `last()`, or @@ -144,7 +144,7 @@ apply(ts, Week(1), Statistics.std, last, renamecols=false) # do not rename colum ## Joins: Row and column binding with other objects -TimeFrames provides methods to join two TimeFrame objects by columns: `join` (alias: +TSFrames provides methods to join two TSFrame objects by columns: `join` (alias: `cbind`) or by rows: `vcat` (alias: `rbind`). Both the methods provide some basic intelligence while doing the merge. @@ -158,7 +158,7 @@ missing from any of the other object. ```@repl e1 dates = collect(Date(2007,1,1):Day(1):Date(2007,1,30)); -ts2 = TimeFrame(rand(length(dates)), dates) +ts2 = TSFrame(rand(length(dates)), dates) join(ts, ts2, JoinAll) # cbind/join on Index column ``` @@ -182,7 +182,7 @@ even throw unknown errors. ```@repl e1 dates = collect(Date(2008,4,1):Day(1):Date(2008,4,30)); -ts3 = TimeFrame(DataFrame(values=rand(length(dates)), Index=dates)) +ts3 = TSFrame(DataFrame(values=rand(length(dates)), Index=dates)) vcat(ts, ts3) # do the merge ``` @@ -201,7 +201,7 @@ rollapply(Statistics.mean, ts, :value, 10) ## Computing rolling difference and percent change Similar to `apply` and `rollapply` there are specific methods to -compute rolling differences and percent changes of a `TimeFrame` object. The +compute rolling differences and percent changes of a `TSFrame` object. The `diff` method computes mathematical difference of values in adjacent rows, inserting `missing` in the first row. `pctchange` computes the percentage change between adjacent rows. @@ -229,17 +229,17 @@ lead(ts, 2) ## Converting to Matrix and DataFrame -You can easily convert a TimeFrame object into a `Matrix` or fetch the -`DataFrame` for doing operations which are outside of the TimeFrames scope. +You can easily convert a TSFrame object into a `Matrix` or fetch the +`DataFrame` for doing operations which are outside of the TSFrames scope. ```@repl e1 ts[:, 1] # convert column 1 to a vector of floats -Matrix(ts) # convert entire TimeFrame into a Matrix +Matrix(ts) # convert entire TSFrame into a Matrix select(ts.coredata, :Index, :value, DataFrames.nrow) # use the underlying DataFrame for other operations ``` -## Writing TimeFrame into a CSV file +## Writing TSFrame into a CSV file -Writing a TimeFrame object into a CSV file can be done easily by using the +Writing a TSFrame object into a CSV file can be done easily by using the underlying `coredata` property. This `DataFrame` can be passed to the `CSV.write` method for writing into a file. @@ -249,13 +249,13 @@ CSV.write("/tmp/demo_ts.csv", ts) ## Broadcasting -Broadcasting can be used on a `TimeFrame` object to apply a function to a subset of it's columns. +Broadcasting can be used on a `TSFrame` object to apply a function to a subset of it's columns. ```jldoctest -julia> using TimeFrames, DataFrames; +julia> using TSFrames, DataFrames; -julia> ts = TimeFrame(DataFrame(Index = [1, 2, 3, 4, 5], A = [10.1, 12.4, 42.4, 24.1, 242.5], B = [2, 4, 6, 8, 10])) -(5 x 2) TimeFrame with Int64 Index +julia> ts = TSFrame(DataFrame(Index = [1, 2, 3, 4, 5], A = [10.1, 12.4, 42.4, 24.1, 242.5], B = [2, 4, 6, 8, 10])) +(5 x 2) TSFrame with Int64 Index Index A B Int64 Float64 Int64 @@ -267,7 +267,7 @@ julia> ts = TimeFrame(DataFrame(Index = [1, 2, 3, 4, 5], A = [10.1, 12.4, 42.4, 5 242.5 10 julia> sin_A = sin.(ts[:, [:A]]) # get sin of column A -(5 x 1) TimeFrame with Int64 Index +(5 x 1) TSFrame with Int64 Index Index A_sin Int64 Float64 @@ -279,7 +279,7 @@ julia> sin_A = sin.(ts[:, [:A]]) # get sin of column A 5 -0.562466 julia> log_ts = log.(ts) # take log of all columns -(5 x 2) TimeFrame with Int64 Index +(5 x 2) TSFrame with Int64 Index Index A_log B_log Int64 Float64 Float64 @@ -291,7 +291,7 @@ julia> log_ts = log.(ts) # take log of all columns 5 5.491 2.30259 julia> log_ts = log.(ts[:, [:A, :B]]) # can specify multiple columns -(5 x 2) TimeFrame with Int64 Index +(5 x 2) TSFrame with Int64 Index Index A_log B_log Int64 Float64 Float64 @@ -306,17 +306,17 @@ julia> log_ts = log.(ts[:, [:A, :B]]) # can specify multiple columns ## [Tables.jl](https://github.com/JuliaData/Tables.jl) Integration -`TimeFrame` objects are [Tables.jl](https://github.com/JuliaData/Tables.jl) compatible. This integration enables easy conversion between the `TimeFrame` format and other formats which are [Tables.jl](https://github.com/JuliaData/Tables.jl) compatible. +`TSFrame` objects are [Tables.jl](https://github.com/JuliaData/Tables.jl) compatible. This integration enables easy conversion between the `TSFrame` format and other formats which are [Tables.jl](https://github.com/JuliaData/Tables.jl) compatible. -As an example, first consider the following code which converts a `TimeFrame` object into a `DataFrame`, a `TimeArray` and a `CSV` file respectively. +As an example, first consider the following code which converts a `TSFrame` object into a `DataFrame`, a `TimeArray` and a `CSV` file respectively. ```julia -julia> using TimeFrames, TimeSeries, Dates, DataFrames, CSV; +julia> using TSFrames, TimeSeries, Dates, DataFrames, CSV; julia> dates = Date(2018, 1, 1):Day(1):Date(2018, 12, 31) Date("2018-01-01"):Day(1):Date("2018-12-31") -julia> ts = TimeFrame(DataFrame(Index = dates, x1 = 1:365)); +julia> ts = TSFrame(DataFrame(Index = dates, x1 = 1:365)); # conversion to DataFrames julia> df = DataFrame(ts); @@ -329,26 +329,26 @@ julia> CSV.write("ts.csv", ts); ``` -Next, here is some code which converts a `DataFrame`, a `TimeArray` and a `CSV` file to a `TimeFrame` object. +Next, here is some code which converts a `DataFrame`, a `TimeArray` and a `CSV` file to a `TSFrame` object. ```julia-repl -julia> using TimeFrames, DataFrames, CSV, TimeSeries, Dates; +julia> using TSFrames, DataFrames, CSV, TimeSeries, Dates; -# converting DataFrame to TimeFrame -julia> ts = TimeFrame(DataFrame(Index=1:10, x1=1:10)); +# converting DataFrame to TSFrame +julia> ts = TSFrame(DataFrame(Index=1:10, x1=1:10)); -# converting from TimeArray to TimeFrame +# converting from TimeArray to TSFrame julia> dates = Date(2018, 1, 1):Day(1):Date(2018, 12, 31) Date("2018-01-01"):Day(1):Date("2018-12-31") julia> ta = TimeArray(dates, rand(length(dates))); -julia> ts = TimeFrame(ta); +julia> ts = TSFrame(ta); -# converting from CSV to TimeFrame -julia> CSV.read("ts.csv", TimeFrame); +# converting from CSV to TSFrame +julia> CSV.read("ts.csv", TSFrame); ``` !!! note - This discussion warrants a note about how we've implemented the [`Tables.jl`](https://github.com/JuliaData/Tables.jl) interfaces. Since `TimeFrame` objects are nothing but a wrapper around a `DataFrame`, our implementations of these interfaces just call [`DataFrames.jl`](https://github.com/JuliaData/DataFrames.jl)'s implementations. Moreover, while constructing `TimeFrame` objects out of other [Tables.jl](https://github.com/JuliaData/Tables.jl) compatible types, our constructor first converts the input table to a `DataFrame`, and then converts the `DataFrame` to a `TimeFrame` object. + This discussion warrants a note about how we've implemented the [`Tables.jl`](https://github.com/JuliaData/Tables.jl) interfaces. Since `TSFrame` objects are nothing but a wrapper around a `DataFrame`, our implementations of these interfaces just call [`DataFrames.jl`](https://github.com/JuliaData/DataFrames.jl)'s implementations. Moreover, while constructing `TSFrame` objects out of other [Tables.jl](https://github.com/JuliaData/Tables.jl) compatible types, our constructor first converts the input table to a `DataFrame`, and then converts the `DataFrame` to a `TSFrame` object. diff --git a/src/TimeFrame.jl b/src/TSFrame.jl similarity index 72% rename from src/TimeFrame.jl rename to src/TSFrame.jl index 63b98dc..51ecbf1 100644 --- a/src/TimeFrame.jl +++ b/src/TSFrame.jl @@ -1,11 +1,11 @@ """ - struct TimeFrame + struct TSFrame coredata :: DataFrame end -`::TimeFrame` - A type to hold ordered data with an index. +`::TSFrame` - A type to hold ordered data with an index. -A TimeFrame object is essentially a `DataFrame` with a specific column marked +A TSFrame object is essentially a `DataFrame` with a specific column marked as an index. The input `DataFrame` is sorted during construction and is stored under the property `coredata`. The index is stored in the `Index` column of `coredata`. @@ -15,30 +15,30 @@ and 2-dimensional `Array`. If an index is already not present in the constructor then a sequential integer index is created automatically. -`TimeFrame(coredata::DataFrame)`: Here, the constructor looks for a column +`TSFrame(coredata::DataFrame)`: Here, the constructor looks for a column named `Index` in `coredata` as the index column, if this is not found then the first column of `coredata` is made the index by default. If `coredata` only has a single column then a new sequential index is generated. -Since `TimeFrame.coredata` is a DataFrame it can be operated upon +Since `TSFrame.coredata` is a DataFrame it can be operated upon independently using methods provided by the DataFrames package (ex. `transform`, `combine`, etc.). # Constructors ```julia -TimeFrame(coredata::DataFrame, index::Union{String, Symbol, Int}) -TimeFrame(coredata::DataFrame, index::AbstractVector{T}) where {T<:Union{Int, TimeType}} -TimeFrame(coredata::DataFrame) -TimeFrame(coredata::DataFrame, index::UnitRange{Int}) -TimeFrame(coredata::AbstractVector{T}, index::AbstractVector{V}; colnames=:auto) where {T, V} -TimeFrame(coredata::AbstractVector{T}; colnames=:auto) where {T} -TimeFrame(coredata::AbstractArray{T,2}; colnames=:auto) where {T} -TimeFrame(coredata::AbstractArray{T,2}, index::AbstractVector{V}; colnames=:auto) where {T, V} +TSFrame(coredata::DataFrame, index::Union{String, Symbol, Int}) +TSFrame(coredata::DataFrame, index::AbstractVector{T}) where {T<:Union{Int, TimeType}} +TSFrame(coredata::DataFrame) +TSFrame(coredata::DataFrame, index::UnitRange{Int}) +TSFrame(coredata::AbstractVector{T}, index::AbstractVector{V}; colnames=:auto) where {T, V} +TSFrame(coredata::AbstractVector{T}; colnames=:auto) where {T} +TSFrame(coredata::AbstractArray{T,2}; colnames=:auto) where {T} +TSFrame(coredata::AbstractArray{T,2}, index::AbstractVector{V}; colnames=:auto) where {T, V} ``` # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) julia> using Random; julia> random(x) = rand(MersenneTwister(123), x); @@ -58,8 +58,8 @@ julia> df = DataFrame(x1 = random(10)) 9 │ 0.26864 10 │ 0.108871 -julia> ts = TimeFrame(df) # generates index -(10 x 1) TimeFrame with Int64 Index +julia> ts = TSFrame(df) # generates index +(10 x 1) TSFrame with Int64 Index Index x1 Int64 Float64 @@ -92,8 +92,8 @@ julia> df = DataFrame(ind = [1, 2, 3], x1 = random(3)) 2 │ 2 0.940515 3 │ 3 0.673959 -julia> ts = TimeFrame(df, 1) # the first column is index -(3 x 1) TimeFrame with Int64 Index +julia> ts = TSFrame(df, 1) # the first column is index +(3 x 1) TSFrame with Int64 Index Index x1 Int64 Float64 @@ -111,8 +111,8 @@ julia> df = DataFrame(x1 = random(3), x2 = random(3), Index = [1, 2, 3]); 2 │ 0.940515 0.940515 2 3 │ 0.673959 0.673959 3 -julia> ts = TimeFrame(df) # uses existing `Index` column -(3 x 2) TimeFrame with Int64 Index +julia> ts = TSFrame(df) # uses existing `Index` column +(3 x 2) TSFrame with Int64 Index Index x1 x2 Int64 Float64 Float64 @@ -139,8 +139,8 @@ julia> df = DataFrame(dates = dates, x1 = random(10)) 9 │ 2017-01-09 0.26864 10 │ 2017-01-10 0.108871 -julia> ts = TimeFrame(df, :dates) -(10 x 1) TimeFrame with Date Index +julia> ts = TSFrame(df, :dates) +(10 x 1) TSFrame with Date Index Index x1 Date Float64 @@ -156,10 +156,10 @@ julia> ts = TimeFrame(df, :dates) 2017-01-09 0.26864 2017-01-10 0.108871 -julia> ts = TimeFrame(DataFrame(x1=random(10)), dates); +julia> ts = TSFrame(DataFrame(x1=random(10)), dates); -julia> ts = TimeFrame(random(10)) -(10 x 1) TimeFrame with Int64 Index +julia> ts = TSFrame(random(10)) +(10 x 1) TSFrame with Int64 Index Index x1 Int64 Float64 @@ -175,8 +175,8 @@ julia> ts = TimeFrame(random(10)) 9 0.26864 10 0.108871 -julia> ts = TimeFrame(random(10), colnames=[:A]) # column is named A -(10 x 1) TimeFrame with Int64 Index +julia> ts = TSFrame(random(10), colnames=[:A]) # column is named A +(10 x 1) TSFrame with Int64 Index Index A Int64 Float64 @@ -192,8 +192,8 @@ julia> ts = TimeFrame(random(10), colnames=[:A]) # column is named A 9 0.26864 10 0.108871 -julia> ts = TimeFrame(random(10), dates) -(10 x 1) TimeFrame with Date Index +julia> ts = TSFrame(random(10), dates) +(10 x 1) TSFrame with Date Index Index x1 Date Float64 @@ -209,8 +209,8 @@ julia> ts = TimeFrame(random(10), dates) 2017-01-09 0.26864 2017-01-10 0.108871 -julia> ts = TimeFrame(random(10), dates, colnames=[:A]) # column is named A -(10 x 1) TimeFrame with Date Index +julia> ts = TSFrame(random(10), dates, colnames=[:A]) # column is named A +(10 x 1) TSFrame with Date Index Index A Date Float64 @@ -226,8 +226,8 @@ julia> ts = TimeFrame(random(10), dates, colnames=[:A]) # column is named A 2017-01-09 0.26864 2017-01-10 0.108871 -julia> ts = TimeFrame([random(10) random(10)]) # matrix object -(10 x 2) TimeFrame with Int64 Index +julia> ts = TSFrame([random(10) random(10)]) # matrix object +(10 x 2) TSFrame with Int64 Index Index x1 x2 Int64 Float64 Float64 @@ -243,8 +243,8 @@ julia> ts = TimeFrame([random(10) random(10)]) # matrix object 9 0.26864 0.26864 10 0.108871 0.108871 -julia> ts = TimeFrame([random(10) random(10)], colnames=[:A, :B]) # columns are named A and B -(10 x 2) TimeFrame with Int64 Index +julia> ts = TSFrame([random(10) random(10)], colnames=[:A, :B]) # columns are named A and B +(10 x 2) TSFrame with Int64 Index Index A B Int64 Float64 Float64 @@ -260,8 +260,8 @@ julia> ts = TimeFrame([random(10) random(10)], colnames=[:A, :B]) # columns are 9 0.26864 0.26864 10 0.108871 0.108871 -julia> ts = TimeFrame([random(10) random(10)], dates) -(10 x 2) TimeFrame with Date Index +julia> ts = TSFrame([random(10) random(10)], dates) +(10 x 2) TSFrame with Date Index Index x1 x2 Date Float64 Float64 @@ -277,8 +277,8 @@ julia> ts = TimeFrame([random(10) random(10)], dates) 2017-01-09 0.26864 0.26864 2017-01-10 0.108871 0.108871 -julia> ts = TimeFrame([random(10) random(10)], dates, colnames=[:A, :B]) # columns are named A and B -(10 x 2) TimeFrame with Date Index +julia> ts = TSFrame([random(10) random(10)], dates, colnames=[:A, :B]) # columns are named A and B +(10 x 2) TSFrame with Date Index Index A B Date Float64 Float64 @@ -296,18 +296,18 @@ julia> ts = TimeFrame([random(10) random(10)], dates, colnames=[:A, :B]) # colum ``` """ -struct TimeFrame +struct TSFrame coredata :: DataFrame # From DataFrame, index number/name/symbol - function TimeFrame(coredata::DataFrame, index::Union{String, Symbol, Int}) + function TSFrame(coredata::DataFrame, index::Union{String, Symbol, Int}) if ! (eltype(coredata[!, index]) <: Union{Int, TimeType}) throw(ArgumentError("only Int and TimeType index is supported")) end if (DataFrames.ncol(coredata) == 1) - TimeFrame(coredata, collect(Base.OneTo(DataFrames.nrow(coredata)))) + TSFrame(coredata, collect(Base.OneTo(DataFrames.nrow(coredata)))) end sorted_cd = sort(coredata, index) @@ -320,7 +320,7 @@ struct TimeFrame end # From DataFrame, external index - function TimeFrame(coredata::DataFrame, index::AbstractVector{T}) where {T<:Union{Int, TimeType}} + function TSFrame(coredata::DataFrame, index::AbstractVector{T}) where {T<:Union{Int, TimeType}} sorted_index = sort(index) cd = copy(coredata) @@ -338,46 +338,46 @@ end #################################### # For general Tables.jl compatible types -function TimeFrame(table) +function TSFrame(table) coredata = DataFrame(table, copycols=true) if "Index" in names(coredata) - return TimeFrame(coredata, :Index) + return TSFrame(coredata, :Index) elseif DataFrames.ncol(coredata) == 1 - return TimeFrame(coredata, collect(1:DataFrames.nrow(coredata))) + return TSFrame(coredata, collect(1:DataFrames.nrow(coredata))) else - return TimeFrame(coredata, 1) + return TSFrame(coredata, 1) end end # From DataFrame, index range -function TimeFrame(coredata::DataFrame, index::UnitRange{Int}) +function TSFrame(coredata::DataFrame, index::UnitRange{Int}) index_vals = collect(index) cd = copy(coredata) insertcols!(cd, 1, :Index => index_vals, after=false, copycols=true) - TimeFrame(cd, :Index) + TSFrame(cd, :Index) end # From AbstractVector -function TimeFrame(coredata::AbstractVector{T}, index::AbstractVector{V}; colnames=:auto) where {T, V} +function TSFrame(coredata::AbstractVector{T}, index::AbstractVector{V}; colnames=:auto) where {T, V} df = DataFrame([coredata], colnames) - TimeFrame(df, index) + TSFrame(df, index) end -function TimeFrame(coredata::AbstractVector{T}; colnames=:auto) where {T} +function TSFrame(coredata::AbstractVector{T}; colnames=:auto) where {T} index_vals = collect(Base.OneTo(length(coredata))) - TimeFrame(coredata, index_vals, colnames=colnames) + TSFrame(coredata, index_vals, colnames=colnames) end # From Matrix and meta # FIXME: use Metadata.jl -function TimeFrame(coredata::AbstractArray{T,2}; colnames=:auto) where {T} +function TSFrame(coredata::AbstractArray{T,2}; colnames=:auto) where {T} index_vals = collect(Base.OneTo(size(coredata)[1])) df = DataFrame(coredata, colnames, copycols=true) - TimeFrame(df, index_vals) + TSFrame(df, index_vals) end -function TimeFrame(coredata::AbstractArray{T,2}, index::AbstractVector{V}; colnames=:auto) where {T, V} +function TSFrame(coredata::AbstractArray{T,2}, index::AbstractVector{V}; colnames=:auto) where {T, V} df = DataFrame(coredata, colnames, copycols=true) - TimeFrame(df, index) + TSFrame(df, index) end diff --git a/src/TimeFrames.jl b/src/TSFrames.jl similarity index 91% rename from src/TimeFrames.jl rename to src/TSFrames.jl index 6c1a877..6d3cd88 100644 --- a/src/TimeFrames.jl +++ b/src/TSFrames.jl @@ -1,4 +1,4 @@ -module TimeFrames +module TSFrames using DataFrames, Dates, ShiftedArrays, RecipesBase, RollingFunctions, Tables @@ -21,7 +21,7 @@ import Base.vcat import Dates.Period -export TimeFrame, +export TSFrame, JoinBoth, JoinAll, JoinInner, @@ -65,7 +65,7 @@ export TimeFrame, DataFrame, Date -include("TimeFrame.jl") +include("TSFrame.jl") include("utils.jl") include("apply.jl") @@ -85,4 +85,4 @@ include("vcat.jl") include("broadcasting.jl") include("tables.jl") -end # END module TimeFrames +end # END module TSFrames diff --git a/src/apply.jl b/src/apply.jl index 1df8cfa..6e5510f 100644 --- a/src/apply.jl +++ b/src/apply.jl @@ -1,7 +1,7 @@ """ # Apply/Period conversion ```julia -apply(ts::TimeFrame, +apply(ts::TSFrame, period::T, fun::V, index_at::Function=first; @@ -30,14 +30,14 @@ an input. names are used. # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) julia> using Random, Statistics; julia> random(x) = rand(MersenneTwister(123), x); julia> dates = collect(Date(2017,1,1):Day(1):Date(2018,3,10)); -julia> ts = TimeFrame(random(length(dates)), dates) +julia> ts = TSFrame(random(length(dates)), dates) julia> show(ts[1:10]) -(10 x 1) TimeFrame with Date Index +(10 x 1) TSFrame with Date Index Index x1 Date Float64 @@ -54,7 +54,7 @@ julia> show(ts[1:10]) 2017-01-10 0.108871 julia> apply(ts, Month(1), first) -(15 x 1) TimeFrame with Date Index +(15 x 1) TSFrame with Date Index Index x1_first Date Float64 @@ -77,7 +77,7 @@ julia> apply(ts, Month(1), first) # alternate months julia> apply(ts, Month(2), first) -(8 x 1) TimeFrame with Date Index +(8 x 1) TSFrame with Date Index Index x1_first Date Float64 @@ -94,7 +94,7 @@ julia> apply(ts, Month(2), first) julia> ts_weekly = apply(ts, Week(1), Statistics.std) # weekly standard deviation julia> show(ts_weekly[1:10]) -(10 x 1) TimeFrame with Date Index +(10 x 1) TSFrame with Date Index Index x1_std Date Float64 @@ -113,7 +113,7 @@ julia> show(ts_weekly[1:10]) julia> ts_weekly = apply(ts, Week(1), Statistics.std, last) # indexed by last date of the week julia> show(ts_weekly[1:10]) -(10 x 1) TimeFrame with Date Index +(10 x 1) TSFrame with Date Index Index x1_std Date Float64 @@ -131,7 +131,7 @@ julia> show(ts_weekly[1:10]) ``` """ -function apply(ts::TimeFrame, period::T, fun::V, index_at::Function=first; renamecols::Bool=true) where {T<:Dates.Period, V<:Function} +function apply(ts::TSFrame, period::T, fun::V, index_at::Function=first; renamecols::Bool=true) where {T<:Dates.Period, V<:Function} ep = endpoints(ts, period) j = 1 @@ -150,7 +150,7 @@ function apply(ts::TimeFrame, period::T, fun::V, index_at::Function=first; renam Not(["Index", tmp_col]) .=> fun, keepkeys=false, renamecols=renamecols) - TimeFrame(df, :Index) + TSFrame(df, :Index) end function get_tmp_colname(cols::AbstractVector{String}) diff --git a/src/broadcasting.jl b/src/broadcasting.jl index b23d5d6..b4927b3 100644 --- a/src/broadcasting.jl +++ b/src/broadcasting.jl @@ -1,5 +1,5 @@ -function Base.Broadcast.broadcasted(f, ts::TimeFrame) - return TimeFrame( +function Base.Broadcast.broadcasted(f, ts::TSFrame) + return TSFrame( select( ts.coredata, :Index, diff --git a/src/diff.jl b/src/diff.jl index 49ff3e3..32102d1 100644 --- a/src/diff.jl +++ b/src/diff.jl @@ -1,7 +1,7 @@ """ # Differencing ```julia -diff(ts::TimeFrame, periods::Int = 1) +diff(ts::TSFrame, periods::Int = 1) ``` Return the discrete difference of successive row elements. @@ -11,16 +11,16 @@ of rows to be shifted over. The skipped rows are rendered as `missing`. `diff` returns an error if column type does not have the method `-`. # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) julia> using Random, Statistics; julia> random(x) = rand(MersenneTwister(123), x); julia> dates = collect(Date(2017,1,1):Day(1):Date(2018,3,10)); -julia> ts = TimeFrame(random(length(dates)), dates); +julia> ts = TSFrame(random(length(dates)), dates); julia> ts[1:10] -(10 x 1) TimeFrame with Date Index +(10 x 1) TSFrame with Date Index Index x1 Date Float64 @@ -37,7 +37,7 @@ julia> ts[1:10] 2017-01-10 0.108871 julia> diff(ts)[1:10] # difference over successive rows -(10 x 1) TimeFrame with Date Index +(10 x 1) TSFrame with Date Index Index x1 Date Float64? @@ -54,7 +54,7 @@ julia> diff(ts)[1:10] # difference over successive rows 2017-01-10 -0.159769 julia> diff(ts, 3)[1:10] # difference over the third row -(10 x 1) TimeFrame with Date Index +(10 x 1) TSFrame with Date Index Index x1 Date Float64? @@ -74,11 +74,11 @@ julia> diff(ts, 3)[1:10] # difference over the third row """ # Diff -function diff(ts::TimeFrame, periods::Int = 1) +function diff(ts::TSFrame, periods::Int = 1) if periods <= 0 error("periods must be a postive int") end - ddf = ts.coredata[:, Not(:Index)] .- TimeFrames.lag(ts, periods).coredata[:, Not(:Index)] + ddf = ts.coredata[:, Not(:Index)] .- TSFrames.lag(ts, periods).coredata[:, Not(:Index)] insertcols!(ddf, 1, "Index" => ts.coredata[:, :Index]) - TimeFrame(ddf, :Index) + TSFrame(ddf, :Index) end diff --git a/src/endpoints.jl b/src/endpoints.jl index d57a474..440083a 100644 --- a/src/endpoints.jl +++ b/src/endpoints.jl @@ -15,10 +15,10 @@ endpoints(timestamps::AbstractVector{T}, on::V) where {T<:Union{Date, DateTime, Microsecond, Nanosecond }} -endpoints(ts::TimeFrame, on::T) where {T<:Dates.Period} -endpoints(ts::TimeFrame, on::Symbol, k::Int=1) -endpoints(ts::TimeFrame, on::String, k::Int=1) -endpoints(ts::TimeFrame, on::Function, k::Int=1) +endpoints(ts::TSFrame, on::T) where {T<:Dates.Period} +endpoints(ts::TSFrame, on::Symbol, k::Int=1) +endpoints(ts::TSFrame, on::String, k::Int=1) +endpoints(ts::TSFrame, on::Function, k::Int=1) endpoints(values::AbstractVector, on::Function, k::Int=1) ``` @@ -26,14 +26,14 @@ Return an integer vector of values for last observation in `timestamps` for each period given by `on`. The values are picked up every `on.value` instance of the period. -Can be used to subset a `TimeFrame` object directly using this function's +Can be used to subset a `TSFrame` object directly using this function's return value. The methods work for regular time series of any periodicity and irregular time series belonging to any of the time-based types provided by the `Dates` module. The primary method works for series of all time types including `Date`, `DateTime`, and `Time`, and for `on` belonging to any of the -sub-types of `Dates.Period`. The `::TimeFrame` methods are provided for +sub-types of `Dates.Period`. The `::TSFrame` methods are provided for convenience and call the primary method directly using the `Index` column. @@ -47,24 +47,24 @@ ones created by `on`. See the examples below to see how the function works in the real world. The `on` function should return a `Vector` to be used as grouping keys. -`endpoints(ts::TimeFrame, on::Symbol)` and `endpoints(ts::TimeFrame, on::String)` +`endpoints(ts::TSFrame, on::Symbol)` and `endpoints(ts::TSFrame, on::String)` are convenience methods where valid values for `on` are: `:years`, `:quarters`, `:months`, `:weeks`, `:days`, `:hours`, `:minutes`, `:seconds`, `:milliseconds`, `:microseconds`, and `:nanoseconds`. Note, that except for `on::Function` all other methods expect `Index` -type of `TimeFrame` to be a subtype of `TimeType`. +type of `TSFrame` to be a subtype of `TimeType`. The method returns `Vector{Int}` corresponding to the matched values in the first argument. # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) julia> using Random julia> random(x) = rand(MersenneTwister(123), x); julia> dates = Date(2017):Day(1):Date(2019); -julia> ts = TimeFrame(random(length(dates)), dates) -(731 x 1) TimeFrame with Date Index +julia> ts = TSFrame(random(length(dates)), dates) +(731 x 1) TSFrame with Date Index Index x1 Date Float64 @@ -120,7 +120,7 @@ julia> ep = endpoints(ts, Month(1)) 731 julia> ts[ep] -(25 x 1) TimeFrame with Date Index +(25 x 1) TSFrame with Date Index Index x1 Date Float64 @@ -180,7 +180,7 @@ julia> diff(index(ts[ep])) # every 2ⁿᵈ month julia> ts[endpoints(ts, Month(2))] -(12 x 1) TimeFrame with Date Index +(12 x 1) TSFrame with Date Index Index x1 Date Float64 @@ -313,7 +313,7 @@ function endpoints(values::AbstractVector, on::Function, k::Int=1) [findlast([p] .== keys) for p in points] end -function endpoints(ts::TimeFrame, on::Function, k::Int=1) +function endpoints(ts::TSFrame, on::Function, k::Int=1) endpoints(index(ts), on, k) end @@ -377,11 +377,11 @@ function endpoints(timestamps::AbstractVector{T}, on::V)::Vector{Int} where {T<: return ep end -function endpoints(ts::TimeFrame, on::T) where {T<:Dates.Period} +function endpoints(ts::TSFrame, on::T) where {T<:Dates.Period} endpoints(index(ts), on) end -function endpoints(ts::TimeFrame, on::Symbol, k::Int=1) +function endpoints(ts::TSFrame, on::Symbol, k::Int=1) if (on == :days) endpoints(ts, Day(k)) elseif (on == :weeks) @@ -409,6 +409,6 @@ function endpoints(ts::TimeFrame, on::Symbol, k::Int=1) end end -function endpoints(ts::TimeFrame, on::String, k::Int=1) +function endpoints(ts::TSFrame, on::String, k::Int=1) endpoints(ts, Symbol(on), k) end diff --git a/src/getindex.jl b/src/getindex.jl index c3155b4..196a986 100644 --- a/src/getindex.jl +++ b/src/getindex.jl @@ -1,7 +1,7 @@ """ # Indexing -`TimeFrame` can be indexed using row and column indices. The row selector +`TSFrame` can be indexed using row and column indices. The row selector could be an integer, a range, an array or it could also be a `Date` object or an ISO-formatted date string ("2007-04-10"). There are methods to subset on year, year-month, and year-quarter. The latter @@ -17,17 +17,17 @@ For fetching the index column vector use the `index()` method. # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) julia> using Random; julia> random(x) = rand(MersenneTwister(123), x); -julia> ts = TimeFrame([random(10) random(10) random(10)]) +julia> ts = TSFrame([random(10) random(10) random(10)]) julia> show(ts) # first row julia> ts[1] -(1 x 3) TimeFrame with Int64 Index +(1 x 3) TSFrame with Int64 Index Index x1 x2 x3 Int64 Float64 Float64 Float64 @@ -36,7 +36,7 @@ julia> ts[1] # first five rows julia> ts[1:5] -(5 x 3) TimeFrame with Int64 Index +(5 x 3) TSFrame with Int64 Index Index x1 x2 x3 Int64 Float64 Float64 Float64 @@ -57,7 +57,7 @@ julia> ts[1:5, :x2] 0.3132439558075186 julia> ts[1:5, 2:3] -(5 x 2) TimeFrame with Int64 Index +(5 x 2) TSFrame with Int64 Index Index x2 x3 Int64 Float64 Float64 @@ -70,7 +70,7 @@ julia> ts[1:5, 2:3] # individual rows julia> ts[[1, 9]] -(2 x 3) TimeFrame with Int64 Index +(2 x 3) TSFrame with Int64 Index Index x1 x2 x3 Int64 Float64 Float64 Float64 @@ -93,7 +93,7 @@ julia> ts[:, :x1] # returns a Vector julia> ts[:, [:x1, :x2]] -(10 x 2) TimeFrame with Int64 Index +(10 x 2) TSFrame with Int64 Index Index x1 x2 Int64 Float64 Float64 @@ -111,9 +111,9 @@ julia> ts[:, [:x1, :x2]] julia> dates = collect(Date(2007):Day(1):Date(2008, 2, 22)); -julia> ts = TimeFrame(random(length(dates)), dates) +julia> ts = TSFrame(random(length(dates)), dates) julia> show(ts[1:10]) -(10 x 1) TimeFrame with Date Index +(10 x 1) TSFrame with Date Index Index x1 Date Float64 @@ -130,7 +130,7 @@ julia> show(ts[1:10]) 2007-01-10 0.108871 julia> ts[Date(2007, 01, 01)] -(1 x 1) TimeFrame with Dates.Date Index +(1 x 1) TSFrame with Dates.Date Index Index x1 Date Float64 @@ -139,7 +139,7 @@ julia> ts[Date(2007, 01, 01)] julia> ts[[Date(2007, 1, 1), Date(2007, 1, 2)]] -(2 x 1) TimeFrame with Date Index +(2 x 1) TSFrame with Date Index Index x1 Date Float64 @@ -155,7 +155,7 @@ julia> ts[[Date(2007, 1, 1), Date(2007, 1, 2)], :x1] julia> ts[Date(2007)] -(1 x 1) TimeFrame with Dates.Date Index +(1 x 1) TSFrame with Dates.Date Index Index x1 Date Float64 @@ -164,7 +164,7 @@ julia> ts[Date(2007)] julia> ts[Year(2007)] -(365 x 1) TimeFrame with Dates.Date Index +(365 x 1) TSFrame with Dates.Date Index Index x1 Date Float64 @@ -190,7 +190,7 @@ julia> ts[Year(2007)] julia> ts[Year(2007), Month(11)] -(30 x 1) TimeFrame with Date Index +(30 x 1) TSFrame with Date Index Index x1 Date Float64 @@ -231,7 +231,7 @@ julia> ts[Year(2007), Quarter(2)]; julia> ts["2007-01-01"] -(1 x 1) TimeFrame with Date Index +(1 x 1) TSFrame with Date Index Index x1 Date Float64 @@ -252,61 +252,61 @@ julia> ts[1, "x1"]; # same as above ### ### Inputs: row scalar, column scalar; Output: scalar -function Base.getindex(ts::TimeFrame, i::Int, j::Int) +function Base.getindex(ts::TSFrame, i::Int, j::Int) return ts.coredata[i,j+1] end -function Base.getindex(ts::TimeFrame, i::Int, j::Union{Symbol, String}) +function Base.getindex(ts::TSFrame, i::Int, j::Union{Symbol, String}) return ts.coredata[i, j] end -function Base.getindex(ts::TimeFrame, dt::T, j::Int) where {T<:TimeType} +function Base.getindex(ts::TSFrame, dt::T, j::Int) where {T<:TimeType} idx = findfirst(x -> x == dt, index(ts)) ts.coredata[idx, j+1] end -function Base.getindex(ts::TimeFrame, dt::T, j::Union{String, Symbol}) where {T<:TimeType} +function Base.getindex(ts::TSFrame, dt::T, j::Union{String, Symbol}) where {T<:TimeType} idx = findfirst(x -> x == dt, index(ts)) ts.coredata[idx, j] end ### -### Inputs: row scalar, column vector; Output: TimeFrame -function Base.getindex(ts::TimeFrame, i::Int, j::AbstractVector{Int}) - TimeFrame(ts.coredata[[i], Cols(:Index, j.+1)]) # increment: account for Index +### Inputs: row scalar, column vector; Output: TSFrame +function Base.getindex(ts::TSFrame, i::Int, j::AbstractVector{Int}) + TSFrame(ts.coredata[[i], Cols(:Index, j.+1)]) # increment: account for Index end -function Base.getindex(ts::TimeFrame, i::Int, j::AbstractVector{T}) where {T<:Union{String, Symbol}} - TimeFrame(ts.coredata[[i], Cols(:Index, j)]) +function Base.getindex(ts::TSFrame, i::Int, j::AbstractVector{T}) where {T<:Union{String, Symbol}} + TSFrame(ts.coredata[[i], Cols(:Index, j)]) end -function Base.getindex(ts::TimeFrame, dt::T, j::AbstractVector{Int}) where {T<:TimeType} +function Base.getindex(ts::TSFrame, dt::T, j::AbstractVector{Int}) where {T<:TimeType} idx = findfirst(x -> x == dt, index(ts)) ts[idx, j] end -function Base.getindex(ts::TimeFrame, dt::D, j::AbstractVector{T}) where {D<:TimeType, T<:Union{String, Symbol}} +function Base.getindex(ts::TSFrame, dt::D, j::AbstractVector{T}) where {D<:TimeType, T<:Union{String, Symbol}} idx = findfirst(x -> x == dt, index(ts)) ts[idx, j] end ### -### Inputs: row scalar, column range; Output: TimeFrame -function Base.getindex(ts::TimeFrame, i::Int, j::UnitRange) - return TimeFrame(ts.coredata[[i], Cols(:Index, 1 .+(j))]) +### Inputs: row scalar, column range; Output: TSFrame +function Base.getindex(ts::TSFrame, i::Int, j::UnitRange) + return TSFrame(ts.coredata[[i], Cols(:Index, 1 .+(j))]) end ### ### Inputs: row vector, column scalar; Output: vector -function Base.getindex(ts::TimeFrame, i::AbstractVector{Int}, j::Int) +function Base.getindex(ts::TSFrame, i::AbstractVector{Int}, j::Int) ts.coredata[i, j+1] # increment: account for Index end -function Base.getindex(ts::TimeFrame, i::AbstractVector{Int}, j::Union{String, Symbol}) +function Base.getindex(ts::TSFrame, i::AbstractVector{Int}, j::Union{String, Symbol}) ts.coredata[i, j] end -function Base.getindex(ts::TimeFrame, dt::AbstractVector{T}, j::Int) where {T<:TimeType} +function Base.getindex(ts::TSFrame, dt::AbstractVector{T}, j::Int) where {T<:TimeType} idx = map(d -> findfirst(x -> x == d, index(ts)), dt) if length(idx) == 0 return nothing @@ -314,7 +314,7 @@ function Base.getindex(ts::TimeFrame, dt::AbstractVector{T}, j::Int) where {T<:T ts[idx, j] end -function Base.getindex(ts::TimeFrame, dt::AbstractVector{T}, j::Union{String, Symbol}) where {T<:TimeType} +function Base.getindex(ts::TSFrame, dt::AbstractVector{T}, j::Union{String, Symbol}) where {T<:TimeType} idx = map(d -> findfirst(x -> x == d, index(ts)), dt) if length(idx) == 0 return nothing @@ -323,16 +323,16 @@ function Base.getindex(ts::TimeFrame, dt::AbstractVector{T}, j::Union{String, Sy end ### -### Inputs: row vector, column vector; Output: TimeFrame -function Base.getindex(ts::TimeFrame, i::AbstractVector{Int}, j::AbstractVector{Int}) - TimeFrame(ts.coredata[i, Cols(:Index, j.+1)]) # increment: account for Index +### Inputs: row vector, column vector; Output: TSFrame +function Base.getindex(ts::TSFrame, i::AbstractVector{Int}, j::AbstractVector{Int}) + TSFrame(ts.coredata[i, Cols(:Index, j.+1)]) # increment: account for Index end -function Base.getindex(ts::TimeFrame, i::AbstractVector{Int}, j::AbstractVector{T}) where {T<:Union{String, Symbol}} - TimeFrame(ts.coredata[i, Cols(:Index, j)]) +function Base.getindex(ts::TSFrame, i::AbstractVector{Int}, j::AbstractVector{T}) where {T<:Union{String, Symbol}} + TSFrame(ts.coredata[i, Cols(:Index, j)]) end -function Base.getindex(ts::TimeFrame, dt::AbstractVector{T}, j::AbstractVector{Int}) where {T<:TimeType} +function Base.getindex(ts::TSFrame, dt::AbstractVector{T}, j::AbstractVector{Int}) where {T<:TimeType} idx = map(d -> findfirst(x -> x == d, index(ts)), dt) if length(idx) == 0 return nothing @@ -340,45 +340,45 @@ function Base.getindex(ts::TimeFrame, dt::AbstractVector{T}, j::AbstractVector{I ts[idx, j] end -function Base.getindex(ts::TimeFrame, dt::AbstractVector{D}, j::AbstractVector{T}) where {D<:TimeType, T<:Union{String, Symbol}} +function Base.getindex(ts::TSFrame, dt::AbstractVector{D}, j::AbstractVector{T}) where {D<:TimeType, T<:Union{String, Symbol}} idx = map(d -> findfirst(x -> x == d, index(ts)), dt) ts[idx, j] end ### -### Inputs: row vector, column range; Output: TimeFrame -function Base.getindex(ts::TimeFrame, i::AbstractVector{Int}, j::UnitRange) +### Inputs: row vector, column range; Output: TSFrame +function Base.getindex(ts::TSFrame, i::AbstractVector{Int}, j::UnitRange) ts[i, collect(j)] end -function Base.getindex(ts::TimeFrame, dt::AbstractVector{T}, j::UnitRange) where {T<:TimeType} +function Base.getindex(ts::TSFrame, dt::AbstractVector{T}, j::UnitRange) where {T<:TimeType} ts[dt, collect(j)] end ### ### Inputs: row range, column scalar: return a vector -function Base.getindex(ts::TimeFrame, i::UnitRange, j::Int) +function Base.getindex(ts::TSFrame, i::UnitRange, j::Int) ts[collect(i), j] end -function Base.getindex(ts::TimeFrame, i::UnitRange, j::Union{String, Symbol}) +function Base.getindex(ts::TSFrame, i::UnitRange, j::Union{String, Symbol}) ts[collect(i), j] end ### -### Inputs: row range, column vector: return TimeFrame -function Base.getindex(ts::TimeFrame, i::UnitRange, j::AbstractVector{Int}) +### Inputs: row range, column vector: return TSFrame +function Base.getindex(ts::TSFrame, i::UnitRange, j::AbstractVector{Int}) ts[collect(i), j] end -function Base.getindex(ts::TimeFrame, i::UnitRange, j::AbstractVector{T}) where {T<:Union{String, Symbol}} +function Base.getindex(ts::TSFrame, i::UnitRange, j::AbstractVector{T}) where {T<:Union{String, Symbol}} ts[collect(i), j] end ### -### Inputs: row range, column range: return TimeFrame -function Base.getindex(ts::TimeFrame, i::UnitRange, j::UnitRange) +### Inputs: row range, column range: return TSFrame +function Base.getindex(ts::TSFrame, i::UnitRange, j::UnitRange) ts[collect(i), collect(j)] end @@ -387,84 +387,84 @@ end ## Row indexing interfaces ### -function Base.getindex(ts::TimeFrame, i::Int) - ts[i, 1:TimeFrames.ncol(ts)] +function Base.getindex(ts::TSFrame, i::Int) + ts[i, 1:TSFrames.ncol(ts)] end -function Base.getindex(ts::TimeFrame, i::UnitRange) - ts[i, 1:TimeFrames.ncol(ts)] +function Base.getindex(ts::TSFrame, i::UnitRange) + ts[i, 1:TSFrames.ncol(ts)] end -function Base.getindex(ts::TimeFrame, i::AbstractVector{Int64}) - ts[i, 1:TimeFrames.ncol(ts)] +function Base.getindex(ts::TSFrame, i::AbstractVector{Int64}) + ts[i, 1:TSFrames.ncol(ts)] end -function Base.getindex(ts::TimeFrame, dt::AbstractVector{T}) where {T<:TimeType} - ts[dt, 1:TimeFrames.ncol(ts)] +function Base.getindex(ts::TSFrame, dt::AbstractVector{T}) where {T<:TimeType} + ts[dt, 1:TSFrames.ncol(ts)] end -function Base.getindex(ts::TimeFrame, d::T) where {T<:TimeType} - ts[[d], 1:TimeFrames.ncol(ts)] +function Base.getindex(ts::TSFrame, d::T) where {T<:TimeType} + ts[[d], 1:TSFrames.ncol(ts)] end # By period -function Base.getindex(ts::TimeFrame, y::Year) +function Base.getindex(ts::TSFrame, y::Year) sdf = filter(:Index => x -> Dates.Year(x) == y, ts.coredata) - TimeFrame(sdf) + TSFrame(sdf) end -function Base.getindex(ts::TimeFrame, y::Year, q::Quarter) +function Base.getindex(ts::TSFrame, y::Year, q::Quarter) sdf = filter(:Index => x -> (Year(x), Quarter(x)) == (y, q), ts.coredata) - TimeFrame(sdf) + TSFrame(sdf) end # XXX: ideally, Dates.YearMonth class should exist -function Base.getindex(ts::TimeFrame, y::Year, m::Month) +function Base.getindex(ts::TSFrame, y::Year, m::Month) sdf = filter(:Index => x -> (Year(x), Month(x)) == (y, m), ts.coredata) - TimeFrame(sdf) + TSFrame(sdf) end -function Base.getindex(ts::TimeFrame, y::Year, m::Month, w::Week) +function Base.getindex(ts::TSFrame, y::Year, m::Month, w::Week) sdf = filter(:Index => x -> (Year(x), Month(x), Week(x)) == (y, m, w), ts.coredata) - TimeFrame(sdf) + TSFrame(sdf) end -function Base.getindex(ts::TimeFrame, y::Year, m::Month, d::Day) +function Base.getindex(ts::TSFrame, y::Year, m::Month, d::Day) sdf = filter(:Index => x -> (Year(x), Month(x), Day(x)) == (y, m, d), ts.coredata) - TimeFrame(sdf) + TSFrame(sdf) end -function Base.getindex(ts::TimeFrame, y::Year, m::Month, d::Day, h::Hour) +function Base.getindex(ts::TSFrame, y::Year, m::Month, d::Day, h::Hour) sdf = filter(:Index => x -> (Year(x), Month(x), Day(x), Hour(x)) == (y, m, d, h), ts.coredata) - TimeFrame(sdf) + TSFrame(sdf) end -function Base.getindex(ts::TimeFrame, y::Year, m::Month, d::Day, h::Hour, min::Minute) +function Base.getindex(ts::TSFrame, y::Year, m::Month, d::Day, h::Hour, min::Minute) sdf = filter(:Index => x -> (Year(x), Month(x), Day(x), Hour(x), Minute(x)) == (y, m, d, h, min), ts.coredata) - TimeFrame(sdf) + TSFrame(sdf) end -function Base.getindex(ts::TimeFrame, y::Year, m::Month, d::Day, h::Hour, min::Minute, sec::Second) +function Base.getindex(ts::TSFrame, y::Year, m::Month, d::Day, h::Hour, min::Minute, sec::Second) sdf = filter(:Index => x -> (Year(x), Month(x), Day(x), Hour(x), Minute(x), Second(x)) == (y, m, d, h, min, sec), ts.coredata) - TimeFrame(sdf) + TSFrame(sdf) end -function Base.getindex(ts::TimeFrame, y::Year, m::Month, d::Day, h::Hour, min::Minute, sec::Second, ms::Millisecond) +function Base.getindex(ts::TSFrame, y::Year, m::Month, d::Day, h::Hour, min::Minute, sec::Second, ms::Millisecond) sdf = filter(:Index => x -> (Year(x), Month(x), Day(x), Hour(x), Minute(x), Second(x), Millisecond(x)) == (y, m, d, h, min, sec, ms), ts.coredata) - TimeFrame(sdf) + TSFrame(sdf) end # By string timestamp -function Base.getindex(ts::TimeFrame, i::String) +function Base.getindex(ts::TSFrame, i::String) d::Date = Date(Dates.parse_components(i, Dates.dateformat"yyyy-mm-dd")...) ind = findall(x -> x == d, index(ts)) # returns duplicate indices ts[ind] end # By {TimeType, Period} range -# function Base.getindex(ts::TimeFrame, r::StepRange{T, V}) where {T<:TimeType, V<:Period} +# function Base.getindex(ts::TSFrame, r::StepRange{T, V}) where {T<:TimeType, V<:Period} # end ### @@ -472,27 +472,27 @@ end ### ### Inputs: row colon, column scalar: return vector -function Base.getindex(ts::TimeFrame, ::Colon, j::Int) - ts[1:TimeFrames.nrow(ts), j] +function Base.getindex(ts::TSFrame, ::Colon, j::Int) + ts[1:TSFrames.nrow(ts), j] end -function Base.getindex(ts::TimeFrame, ::Colon, j::Union{String, Symbol}) - ts[1:TimeFrames.nrow(ts), j] +function Base.getindex(ts::TSFrame, ::Colon, j::Union{String, Symbol}) + ts[1:TSFrames.nrow(ts), j] end ### -### Inputs: row colon, column vector: return TimeFrame -function Base.getindex(ts::TimeFrame, ::Colon, j::AbstractVector{Int}) - ts[1:TimeFrames.nrow(ts), j] +### Inputs: row colon, column vector: return TSFrame +function Base.getindex(ts::TSFrame, ::Colon, j::AbstractVector{Int}) + ts[1:TSFrames.nrow(ts), j] end -function Base.getindex(ts::TimeFrame, ::Colon, j::AbstractVector{T}) where {T<:Union{String, Symbol}} - ts[1:TimeFrames.nrow(ts), j] +function Base.getindex(ts::TSFrame, ::Colon, j::AbstractVector{T}) where {T<:Union{String, Symbol}} + ts[1:TSFrames.nrow(ts), j] end ### -### Inputs: row colon, column range: return TimeFrame -function Base.getindex(ts::TimeFrame, i::Colon, j::UnitRange) - ts[1:TimeFrames.nrow(ts), collect(j)] +### Inputs: row colon, column range: return TSFrame +function Base.getindex(ts::TSFrame, i::Colon, j::UnitRange) + ts[1:TSFrames.nrow(ts), collect(j)] end ### diff --git a/src/getproperty.jl b/src/getproperty.jl index bc3c427..4b7b0c5 100644 --- a/src/getproperty.jl +++ b/src/getproperty.jl @@ -1,3 +1,3 @@ -function Base.getproperty(ts::TimeFrame, f::Symbol) +function Base.getproperty(ts::TSFrame, f::Symbol) return (f == :coredata) ? getfield(ts, :coredata) : getproperty(ts.coredata, f) end diff --git a/src/join.jl b/src/join.jl index c8e5a6b..739a436 100644 --- a/src/join.jl +++ b/src/join.jl @@ -14,27 +14,27 @@ end """ # Joins/Column-binding -`TimeFrame` objects can be combined together column-wise using `Index` as the +`TSFrame` objects can be combined together column-wise using `Index` as the column key. There are four kinds of column-binding operations possible as of now. Each join operation works by performing a Set operation on the `Index` column and then merging the datasets based on the output from the Set operation. Each operation changes column names in the final object automatically if the operation encounters duplicate -column names amongst the TimeFrame objects. +column names amongst the TSFrame objects. The following join types are supported: -`join(ts1::TimeFrame, ts2::TimeFrame, ::Type{JoinInner})` and -`join(ts1::TimeFrame, ts2::TimeFrame, ::Type{JoinBoth})` +`join(ts1::TSFrame, ts2::TSFrame, ::Type{JoinInner})` and +`join(ts1::TSFrame, ts2::TSFrame, ::Type{JoinBoth})` a.k.a. inner join, takes the intersection of the indexes of `ts1` and `ts2`, and then merges the columns of both the objects. The resulting object will only contain rows which are present in both the objects' indexes. The function will rename columns in the final object if -they had same names in the TimeFrame objects. +they had same names in the TSFrame objects. -`join(ts1::TimeFrame, ts2::TimeFrame, ::Type{JoinOuter})` and -`join(ts1::TimeFrame, ts2::TimeFrame, ::Type{JoinAll})`: +`join(ts1::TSFrame, ts2::TSFrame, ::Type{JoinOuter})` and +`join(ts1::TSFrame, ts2::TSFrame, ::Type{JoinAll})`: a.k.a. outer join, takes the union of the indexes of `ts1` and `ts2` before merging the other columns of input objects. The output will @@ -43,7 +43,7 @@ inserting `missing` values where a row was not present in any of the objects. This is the default behaviour if no `JoinType` object is provided. -`join(ts1::TimeFrame, ts2::TimeFrame, ::Type{JoinLeft})`: +`join(ts1::TSFrame, ts2::TSFrame, ::Type{JoinLeft})`: Left join takes the index values which are present in the left object `ts1` and finds matching index values in the right object @@ -53,7 +53,7 @@ associated with matching index rows on the right. The operation inserts `missing` values where in the unmatched rows of the right object. -`join(ts1::TimeFrame, ts2::TimeFrame, ::Type{JoinRight})` +`join(ts1::TSFrame, ts2::TSFrame, ::Type{JoinRight})` Right join, similar to left join but works in the opposite direction. The final object contains all the rows from the right @@ -66,16 +66,16 @@ is provided to the `join` method. `cbind` is an alias for `join` method. # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) julia> using Random; julia> random(x) = rand(MersenneTwister(123), x); julia> dates = collect(Date(2017,1,1):Day(1):Date(2017,1,10)); -julia> ts1 = TimeFrame(random(length(dates)), dates) +julia> ts1 = TSFrame(random(length(dates)), dates) julia> show(ts1) -(10 x 1) TimeFrame with Dates.Date Index +(10 x 1) TSFrame with Dates.Date Index Index x1 Date Float64 @@ -93,9 +93,9 @@ julia> show(ts1) julia> dates = collect(Date(2017,1,1):Day(1):Date(2017,1,30)); -julia> ts2 = TimeFrame(random(length(dates)), dates); +julia> ts2 = TSFrame(random(length(dates)), dates); julia> show(ts2) -(30 x 1) TimeFrame with Dates.Date Index +(30 x 1) TSFrame with Dates.Date Index Index x1 Date Float64 @@ -123,7 +123,7 @@ julia> show(ts2) # join on all index values # equivalent to `join(ts1, ts2, JoinAll)` call julia> join(ts1, ts2) -(30 x 2) TimeFrame with Date Index +(30 x 2) TSFrame with Date Index Index x1 x1_1 Date Float64? Float64? @@ -155,7 +155,7 @@ julia> cbind(ts1, ts2); # join only the common index values julia> join(ts1, ts2, JoinBoth) -(10 x 2) TimeFrame with Date Index +(10 x 2) TSFrame with Date Index Index x1 x1_1 Date Float64 Float64 @@ -174,7 +174,7 @@ julia> join(ts1, ts2, JoinBoth) # keep index values of `ts1` julia> join(ts1, ts2, JoinLeft) -(10 x 2) TimeFrame with Date Index +(10 x 2) TSFrame with Date Index Index x1 x1_1 Date Float64 Float64? @@ -193,7 +193,7 @@ julia> join(ts1, ts2, JoinLeft) # keep index values of `ts2` julia> join(ts1, ts2, JoinRight) -(30 x 2) TimeFrame with Date Index +(30 x 2) TSFrame with Date Index Index x1 x1_1 Date Float64? Float64 @@ -222,30 +222,30 @@ julia> join(ts1, ts2, JoinRight) ``` """ -function Base.join(ts1::TimeFrame, ts2::TimeFrame) +function Base.join(ts1::TSFrame, ts2::TSFrame) join(ts1, ts2, JoinAll) end -function Base.join(ts1::TimeFrame, ts2::TimeFrame, ::Type{JoinBoth}) +function Base.join(ts1::TSFrame, ts2::TSFrame, ::Type{JoinBoth}) result = DataFrames.innerjoin(ts1.coredata, ts2.coredata, on=:Index, makeunique=true) - return TimeFrame(result) + return TSFrame(result) end -Base.join(ts1::TimeFrame, ts2::TimeFrame, ::Type{JoinInner}) = Base.join(ts1, ts2, JoinBoth) +Base.join(ts1::TSFrame, ts2::TSFrame, ::Type{JoinInner}) = Base.join(ts1, ts2, JoinBoth) -function Base.join(ts1::TimeFrame, ts2::TimeFrame, ::Type{JoinAll}) +function Base.join(ts1::TSFrame, ts2::TSFrame, ::Type{JoinAll}) result = DataFrames.outerjoin(ts1.coredata, ts2.coredata, on=:Index, makeunique=true) - return TimeFrame(result) + return TSFrame(result) end -Base.join(ts1::TimeFrame, ts2::TimeFrame, ::Type{JoinOuter}) = Base.join(ts1, ts2, JoinAll) +Base.join(ts1::TSFrame, ts2::TSFrame, ::Type{JoinOuter}) = Base.join(ts1, ts2, JoinAll) -function Base.join(ts1::TimeFrame, ts2::TimeFrame, ::Type{JoinLeft}) +function Base.join(ts1::TSFrame, ts2::TSFrame, ::Type{JoinLeft}) result = DataFrames.leftjoin(ts1.coredata, ts2.coredata, on=:Index, makeunique=true) - return TimeFrame(result) + return TSFrame(result) end -function Base.join(ts1::TimeFrame, ts2::TimeFrame, ::Type{JoinRight}) +function Base.join(ts1::TSFrame, ts2::TSFrame, ::Type{JoinRight}) result = DataFrames.rightjoin(ts1.coredata, ts2.coredata, on=:Index, makeunique=true) - return TimeFrame(result) + return TSFrame(result) end # alias cbind = join diff --git a/src/lag.jl b/src/lag.jl index 275fa62..95d2cdf 100644 --- a/src/lag.jl +++ b/src/lag.jl @@ -1,24 +1,24 @@ """ # Lagging ```julia -lag(ts::TimeFrame, lag_value::Int = 1) +lag(ts::TSFrame, lag_value::Int = 1) ``` Lag the `ts` object by the specified `lag_value`. The rows corresponding to lagged values will be rendered as `missing`. Negative values of lag are -also accepted (see `TimeFrames.lead`). +also accepted (see `TSFrames.lead`). # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) julia> using Random, Statistics; julia> random(x) = rand(MersenneTwister(123), x); julia> dates = collect(Date(2017,1,1):Day(1):Date(2017,1,10)); -julia> ts = TimeFrame(random(length(dates)), dates); +julia> ts = TSFrame(random(length(dates)), dates); julia> show(ts) -(10 x 1) TimeFrame with Dates.Date Index +(10 x 1) TSFrame with Dates.Date Index Index x1 Date Float64 @@ -36,7 +36,7 @@ julia> show(ts) julia> lag(ts) -(10 x 1) TimeFrame with Date Index +(10 x 1) TSFrame with Date Index Index x1 Date Float64? @@ -53,7 +53,7 @@ julia> lag(ts) 2017-01-10 0.26864 julia> lag(ts, 2) # lags by 2 values -(10 x 1) TimeFrame with Date Index +(10 x 1) TSFrame with Date Index Index x1 Date Float64? @@ -71,8 +71,8 @@ julia> lag(ts, 2) # lags by 2 values ``` """ -function lag(ts::TimeFrame, lag_value::Int = 1) - sdf = DataFrame(ShiftedArrays.lag.(eachcol(ts.coredata[!, Not(:Index)]), lag_value), TimeFrames.names(ts)) +function lag(ts::TSFrame, lag_value::Int = 1) + sdf = DataFrame(ShiftedArrays.lag.(eachcol(ts.coredata[!, Not(:Index)]), lag_value), TSFrames.names(ts)) insertcols!(sdf, 1, :Index => ts.coredata[!, :Index]) - TimeFrame(sdf, :Index) + TSFrame(sdf, :Index) end diff --git a/src/lead.jl b/src/lead.jl index 8636a55..0532f70 100644 --- a/src/lead.jl +++ b/src/lead.jl @@ -1,24 +1,24 @@ """ # Leading ```julia -lead(ts::TimeFrame, lead_value::Int = 1) +lead(ts::TSFrame, lead_value::Int = 1) ``` Similar to lag, this method leads the `ts` object by `lead_value`. The lead rows are inserted with `missing`. Negative values of lead are -also accepted (see `TimeFrames.lag`). +also accepted (see `TSFrames.lag`). # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) julia> using Random, Statistics; julia> random(x) = rand(MersenneTwister(123), x); julia> dates = collect(Date(2017,1,1):Day(1):Date(2018,3,10)); -julia> ts = TimeFrame(DataFrame(Index = dates, x1 = random(length(dates)))) +julia> ts = TSFrame(DataFrame(Index = dates, x1 = random(length(dates)))) julia> show(ts) -(434 x 1) TimeFrame with Dates.Date Index +(434 x 1) TSFrame with Dates.Date Index Index x1 Date Float64 @@ -44,7 +44,7 @@ julia> show(ts) julia> lead(ts)[1:10] # leads once -(10 x 1) TimeFrame with Date Index +(10 x 1) TSFrame with Date Index Index x1 Date Float64? @@ -61,7 +61,7 @@ julia> lead(ts)[1:10] # leads once 2017-01-10 0.163666 julia> lead(ts, 2)[1:10] # leads by 2 values -(10 x 1) TimeFrame with Date Index +(10 x 1) TSFrame with Date Index Index x1 Date Float64? @@ -79,8 +79,8 @@ julia> lead(ts, 2)[1:10] # leads by 2 values ``` """ -function lead(ts::TimeFrame, lead_value::Int = 1) - sdf = DataFrame(ShiftedArrays.lead.(eachcol(ts.coredata[!, Not(:Index)]), lead_value), TimeFrames.names(ts)) +function lead(ts::TSFrame, lead_value::Int = 1) + sdf = DataFrame(ShiftedArrays.lead.(eachcol(ts.coredata[!, Not(:Index)]), lead_value), TSFrames.names(ts)) insertcols!(sdf, 1, :Index => ts.coredata[!, :Index]) - TimeFrame(sdf, :Index) + TSFrame(sdf, :Index) end diff --git a/src/matrix.jl b/src/matrix.jl index 7b6efaf..ffded7a 100644 --- a/src/matrix.jl +++ b/src/matrix.jl @@ -1,18 +1,18 @@ """ # Conversion of non-Index data to Matrix -Data in non-index columns of a TimeFrame object can be converted into a +Data in non-index columns of a TSFrame object can be converted into a `Matrix` type for further numerical analysis using the `Matrix()` constructor. # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) julia> using Random; julia> random(x) = rand(MersenneTwister(123), x); -julia> ts = TimeFrame([random(10) random(10)]) +julia> ts = TSFrame([random(10) random(10)]) julia> show(ts) -(10 x 2) TimeFrame with Int64 Index +(10 x 2) TSFrame with Int64 Index Index x1 x2 Int64 Float64 Float64 @@ -42,6 +42,6 @@ julia> Matrix(ts) 0.108871 0.108871 ``` """ -function Base.Matrix(ts::TimeFrame) +function Base.Matrix(ts::TSFrame) DataFrames.Matrix(ts.coredata[!, Not(:Index)]) end diff --git a/src/pctchange.jl b/src/pctchange.jl index 4ef82e7..661dc65 100644 --- a/src/pctchange.jl +++ b/src/pctchange.jl @@ -2,7 +2,7 @@ # Percent Change ```julia -pctchange(ts::TimeFrame, periods::Int = 1) +pctchange(ts::TSFrame, periods::Int = 1) ``` Return the percentage change between successive row elements. @@ -12,16 +12,16 @@ of rows to be shifted over. The skipped rows are rendered as `missing`. `pctchange` returns an error if column type does not have the method `/`. # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) julia> using Random, Statistics; julia> random(x) = rand(MersenneTwister(123), x); julia> dates = collect(Date(2017,1,1):Day(1):Date(2017,1,10)); -julia> ts = TimeFrame(random(length(dates)), dates) +julia> ts = TSFrame(random(length(dates)), dates) julia> show(ts) -(10 x 1) TimeFrame with Date Index +(10 x 1) TSFrame with Date Index Index x1 Date Float64 @@ -39,7 +39,7 @@ julia> show(ts) # Pctchange over successive rows julia> pctchange(ts) -(10 x 1) TimeFrame with Date Index +(10 x 1) TSFrame with Date Index Index x1 Date Float64? @@ -58,7 +58,7 @@ julia> pctchange(ts) # Pctchange over the third row julia> pctchange(ts, 3) -(10 x 1) TimeFrame with Date Index +(10 x 1) TSFrame with Date Index Index x1 Date Float64? @@ -78,11 +78,11 @@ julia> pctchange(ts, 3) """ # Pctchange -function pctchange(ts::TimeFrame, periods::Int = 1) +function pctchange(ts::TSFrame, periods::Int = 1) if periods <= 0 throw(ArgumentError("periods must be a positive int")) end - ddf = (ts.coredata[:, Not(:Index)] .- TimeFrames.lag(ts, periods).coredata[:, Not(:Index)]) ./ abs.(TimeFrames.lag(ts, periods).coredata[:, Not(:Index)]) + ddf = (ts.coredata[:, Not(:Index)] .- TSFrames.lag(ts, periods).coredata[:, Not(:Index)]) ./ abs.(TSFrames.lag(ts, periods).coredata[:, Not(:Index)]) insertcols!(ddf, 1, "Index" => ts.coredata[:, :Index]) - TimeFrame(ddf, :Index) + TSFrame(ddf, :Index) end diff --git a/src/plot.jl b/src/plot.jl index e6012a1..4828161 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -2,17 +2,17 @@ # Plotting ```julia -plot(ts::TimeFrame, cols::Vector{Int} = collect(1:TimeFrames.ncol(ts))) -plot(ts::TimeFrame, cols::Vector{T}) where {T<:Union{String, Symbol}} -plot(ts::TimeFrame, cols::T) where {T<:Union{Int, String, Symbol}} +plot(ts::TSFrame, cols::Vector{Int} = collect(1:TSFrames.ncol(ts))) +plot(ts::TSFrame, cols::Vector{T}) where {T<:Union{String, Symbol}} +plot(ts::TSFrame, cols::T) where {T<:Union{Int, String, Symbol}} ``` -Plots a TimeFrame object with the index on the x-axis and selected `cols` on +Plots a TSFrame object with the index on the x-axis and selected `cols` on the y-axis. By default, plot all the columns. Columns can be selected using Int indexes, String(s), or Symbol(s). # Example -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Plots, Random, Statistics) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Plots, Random, Statistics) julia> using Random; julia> random(x) = rand(MersenneTwister(123), x); julia> dates = Date("2022-01-01"):Month(1):Date("2022-01-01")+Month(11); @@ -22,9 +22,9 @@ julia> df = DataFrame(Index = dates, val2 = random(12), val3 = random(12)); -julia> ts = TimeFrame(df) +julia> ts = TSFrame(df) julia> show(ts) -(12 x 3) TimeFrame with Dates.Date Index +(12 x 3) TSFrame with Dates.Date Index Index val1 val2 val3 Date Float64 Float64 Float64 @@ -54,22 +54,22 @@ julia> # plot(ts[1:6], [:val1, :val3]); julia> # plot(ts, [1, 2], size=(600, 400)); ``` """ -@recipe function f(ts::TimeFrame, cols::Vector{Int} = collect(1:TimeFrames.ncol(ts))) +@recipe function f(ts::TSFrame, cols::Vector{Int} = collect(1:TSFrames.ncol(ts))) seriestype := :line size --> (1200, 1200) xlabel --> :Index - ylabel --> join(TimeFrames.names(ts)[cols], ", ") + ylabel --> join(TSFrames.names(ts)[cols], ", ") legend := true - label := permutedims(TimeFrames.names(ts)[cols]) - (TimeFrames.index(ts), Matrix(ts.coredata[!, cols.+1])) # increment to account for Index + label := permutedims(TSFrames.names(ts)[cols]) + (TSFrames.index(ts), Matrix(ts.coredata[!, cols.+1])) # increment to account for Index end -@recipe function f(ts::TimeFrame, cols::Vector{T}) where {T<:Union{String, Symbol}} +@recipe function f(ts::TSFrame, cols::Vector{T}) where {T<:Union{String, Symbol}} colindices = [DataFrames.columnindex(ts.coredata, i) for i in cols] colindices .-= 1 # decrement to account for Index (ts, colindices) end -@recipe function f(ts::TimeFrame, cols::T) where {T<:Union{Int, String, Symbol}} +@recipe function f(ts::TSFrame, cols::T) where {T<:Union{Int, String, Symbol}} (ts, [cols]) end diff --git a/src/rollapply.jl b/src/rollapply.jl index 3d11f94..2ed98d9 100644 --- a/src/rollapply.jl +++ b/src/rollapply.jl @@ -2,14 +2,14 @@ # Rolling Functions ```julia -rollapply(fun::Function, ts::TimeFrame, column::Any, windowsize::Int) +rollapply(fun::Function, ts::TSFrame, column::Any, windowsize::Int) ``` Apply a function to a column of `ts` for each continuous set of rows of size `windowsize`. `column` could be any of the `DataFrame` column selectors. -The output is a TimeFrame object with `(nrow(ts) - windowsize + 1)` rows +The output is a TSFrame object with `(nrow(ts) - windowsize + 1)` rows indexed with the last index value of each window. This method uses `RollingFunctions` package to implement this @@ -17,11 +17,11 @@ functionality. # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) -julia> ts = TimeFrame(1:12, Date("2022-02-01"):Month(1):Date("2022-02-01")+Month(11)) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) +julia> ts = TSFrame(1:12, Date("2022-02-01"):Month(1):Date("2022-02-01")+Month(11)) julia> show(ts) -(12 x 1) TimeFrame with Dates.Date Index +(12 x 1) TSFrame with Dates.Date Index Index x1 Date Int64 @@ -40,7 +40,7 @@ julia> show(ts) 2023-01-01 12 julia> rollapply(sum, ts, :x1, 10) -(3 x 1) TimeFrame with Dates.Date Index +(3 x 1) TSFrame with Dates.Date Index Index x1_rolling_sum Date Float64 @@ -50,7 +50,7 @@ julia> rollapply(sum, ts, :x1, 10) 2023-01-01 75.0 julia> rollapply(Statistics.mean, ts, 1, 5) -(8 x 1) TimeFrame with Dates.Date Index +(8 x 1) TSFrame with Dates.Date Index Index x1_rolling_mean Date Float64 @@ -66,7 +66,7 @@ julia> rollapply(Statistics.mean, ts, 1, 5) ``` """ -function rollapply(fun::Function, ts::TimeFrame, column::Any, windowsize::Int) # TODO: multiple columns +function rollapply(fun::Function, ts::TSFrame, column::Any, windowsize::Int) # TODO: multiple columns if windowsize < 1 throw(ArgumentError("windowsize must be greater than or equal to 1")) end @@ -78,8 +78,8 @@ function rollapply(fun::Function, ts::TimeFrame, column::Any, windowsize::Int) # col = column end res = RollingFunctions.rolling(fun, ts.coredata[!, col], windowsize) - idx = TimeFrames.index(ts)[windowsize:end] + idx = TSFrames.index(ts)[windowsize:end] colname = names(ts.coredata[!, [col]])[1] res_df = DataFrame([idx, res], ["Index", "$(colname)_rolling_$(fun)"]) - return TimeFrame(res_df) + return TSFrame(res_df) end diff --git a/src/subset.jl b/src/subset.jl index e8f5ed8..6335582 100644 --- a/src/subset.jl +++ b/src/subset.jl @@ -2,20 +2,20 @@ # Subsetting based on Index ```julia -subset(ts::TimeFrame, from::T, to::T) where {T<:Union{Int, TimeType}} +subset(ts::TSFrame, from::T, to::T) where {T<:Union{Int, TimeType}} ``` Create a subset of `ts` based on the `Index` starting `from` (inclusive) till `to` (inclusive). # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) julia> using Random; julia> random(x) = rand(MersenneTwister(123), x); julia> dates = Date("2022-02-01"):Week(1):Date("2022-02-01")+Month(9); -julia> ts = TimeFrame(random(length(dates)), dates) +julia> ts = TSFrame(random(length(dates)), dates) julia> show(ts) -(40 x 1) TimeFrame with Date Index +(40 x 1) TSFrame with Date Index Index x1 Date Float64 @@ -62,7 +62,7 @@ julia> show(ts) 2022-11-01 0.348173 julia> subset(ts, Date(2022, 03), Date(2022, 07)) -(18 x 1) TimeFrame with Date Index +(18 x 1) TSFrame with Date Index Index x1 Date Float64 @@ -86,8 +86,8 @@ julia> subset(ts, Date(2022, 03), Date(2022, 07)) 2022-06-21 0.70586 2022-06-28 0.291978 -julia> subset(TimeFrame(1:20, -9:10), -4, 5) -(10 x 1) TimeFrame with Int64 Index +julia> subset(TSFrame(1:20, -9:10), -4, 5) +(10 x 1) TSFrame with Int64 Index Index x1 Int64 Int64 @@ -104,7 +104,7 @@ julia> subset(TimeFrame(1:20, -9:10), -4, 5) 5 15 julia> subset(ts,:,Date("2022-04-12")) -(11 x 1) TimeFrame with Date Index +(11 x 1) TSFrame with Date Index Index x1 Date Float64 @@ -122,7 +122,7 @@ julia> subset(ts,:,Date("2022-04-12")) 2022-04-12 0.163666 julia> subset(ts,Date("2022-9-27"),:) -(6 x 1) TimeFrame with Date Index +(6 x 1) TSFrame with Date Index Index x1 Date Float64 @@ -138,14 +138,14 @@ julia> subset(ts,Date("2022-9-27"),:) ``` """ -function subset(ts::TimeFrame, from::T, to::T) where {T<:Union{Int, TimeType}} - TimeFrame(DataFrames.subset(ts.coredata, :Index => x -> x .>= from .&& x .<= to)) +function subset(ts::TSFrame, from::T, to::T) where {T<:Union{Int, TimeType}} + TSFrame(DataFrames.subset(ts.coredata, :Index => x -> x .>= from .&& x .<= to)) end -function subset(ts::TimeFrame, ::Colon, to::T) where {T<:Union{Int, TimeType}} - TimeFrame(DataFrames.subset(ts.coredata, :Index => x -> x .<= to)) +function subset(ts::TSFrame, ::Colon, to::T) where {T<:Union{Int, TimeType}} + TSFrame(DataFrames.subset(ts.coredata, :Index => x -> x .<= to)) end -function subset(ts::TimeFrame, from::T, ::Colon) where {T<:Union{Int, TimeType}} - TimeFrame(DataFrames.subset(ts.coredata, :Index => x -> x .>= from)) +function subset(ts::TSFrame, from::T, ::Colon) where {T<:Union{Int, TimeType}} + TSFrame(DataFrames.subset(ts.coredata, :Index => x -> x .>= from)) end diff --git a/src/tables.jl b/src/tables.jl index 4622a74..7b3eea6 100644 --- a/src/tables.jl +++ b/src/tables.jl @@ -1,16 +1,16 @@ -Tables.istable(::Type{TimeFrame}) = true +Tables.istable(::Type{TSFrame}) = true -Tables.rowaccess(::Type{TimeFrame}) = true -Tables.rows(ts::TimeFrame) = DataFrames.eachrow(ts.coredata) +Tables.rowaccess(::Type{TSFrame}) = true +Tables.rows(ts::TSFrame) = DataFrames.eachrow(ts.coredata) -Tables.columnaccess(::Type{TimeFrame}) = true -Tables.columns(ts::TimeFrame) = DataFrames.eachcol(ts.coredata) +Tables.columnaccess(::Type{TSFrame}) = true +Tables.columns(ts::TSFrame) = DataFrames.eachcol(ts.coredata) -Tables.rowtable(ts::TimeFrame) = Tables.rowtable(ts.coredata) -Tables.columntable(ts::TimeFrame) = Tables.columntable(ts.coredata) +Tables.rowtable(ts::TSFrame) = Tables.rowtable(ts.coredata) +Tables.columntable(ts::TSFrame) = Tables.columntable(ts.coredata) -Tables.namedtupleiterator(ts::TimeFrame) = Tables.namedtupleiterator(ts.coredata) +Tables.namedtupleiterator(ts::TSFrame) = Tables.namedtupleiterator(ts.coredata) -Tables.schema(ts::TimeFrame) = Tables.schema(ts.coredata) +Tables.schema(ts::TSFrame) = Tables.schema(ts.coredata) -Tables.materializer(::Type{<:TimeFrame}) = TimeFrame +Tables.materializer(::Type{<:TSFrame}) = TSFrame diff --git a/src/upsample.jl b/src/upsample.jl index 08a2b52..ed0449c 100644 --- a/src/upsample.jl +++ b/src/upsample.jl @@ -1,7 +1,7 @@ """ # Upsampling ```julia -upsample(ts::TimeFrame, +upsample(ts::TSFrame, period::T where {T<:Union{DatePeriod, TimePeriod}} ``` @@ -12,14 +12,14 @@ from monthly series to daily.) `period` is any of `Period` types in the By default, the added rows contain `missing` data. # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) julia> using Random, Statistics; julia> random(x) = rand(MersenneTwister(123), x); julia> dates = collect(DateTime(2017,1,1):Day(1):DateTime(2018,3,10)); -julia> ts = TimeFrame(random(length(dates)), dates) +julia> ts = TSFrame(random(length(dates)), dates) julia> show(ts[1:10]) -(10 x 1) TimeFrame with DateTime Index +(10 x 1) TSFrame with DateTime Index Index x1 DateTime Float64 @@ -36,7 +36,7 @@ julia> show(ts[1:10]) 2017-01-10T00:00:00 0.108871 julia> upsample(ts, Hour(1)) -(10393 x 1) TimeFrame with DateTime Index +(10393 x 1) TSFrame with DateTime Index Index x1 DateTime Float64? @@ -53,7 +53,7 @@ julia> show(ts[1:10]) 10385 rows omitted upsample(ts, Hour(12)) -(867 x 1) TimeFrame with DateTime Index +(867 x 1) TSFrame with DateTime Index Index x1 DateTime Float64? @@ -70,7 +70,7 @@ DateTime Float64? 859 rows omitted """ -function upsample(ts::TimeFrame, period::T) where {T<:Union{DatePeriod, TimePeriod}} +function upsample(ts::TSFrame, period::T) where {T<:Union{DatePeriod, TimePeriod}} dex = collect(first(index(ts)):period:last(index(ts))) - join(ts, TimeFrame(DataFrame(index = dex), :index)) + join(ts, TSFrame(DataFrame(index = dex), :index)) end \ No newline at end of file diff --git a/src/utils.jl b/src/utils.jl index a64623d..e70840d 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -2,8 +2,8 @@ # Summary statistics ```julia -describe(ts::TimeFrame; cols=:) -describe(ts::TimeFrame, stats::Union{Symbol, Pair}...; cols=:) +describe(ts::TSFrame; cols=:) +describe(ts::TSFrame, stats::Union{Symbol, Pair}...; cols=:) ``` Compute summary statistics of `ts`. The output is a `DataFrame` @@ -14,10 +14,10 @@ summary statistics are to be printed. For more information about these keywords, check out the corresponding [documentation from DataFrames.jl](https://dataframes.juliadata.org/stable/lib/functions/#DataAPI.describe). # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) julia> using Random; julia> random(x) = rand(MersenneTwister(123), x...); -julia> ts = TimeFrame(random(([1, 2, 3, 4, missing], 10))) +julia> ts = TSFrame(random(([1, 2, 3, 4, missing], 10))) julia> describe(ts) 2×7 DataFrame Row │ variable mean min median max nmissing eltype @@ -53,66 +53,66 @@ julia> describe(ts, :min, sum => :sum, cols=:x1) ``` """ -function describe(io::IO, ts::TimeFrame; cols=:) +function describe(io::IO, ts::TSFrame; cols=:) DataFrames.describe(ts.coredata; cols=cols) end -TimeFrames.describe(ts::TimeFrame; cols=:) = TimeFrames.describe(stdout, ts; cols=cols) +TSFrames.describe(ts::TSFrame; cols=:) = TSFrames.describe(stdout, ts; cols=cols) function describe( io::IO, - ts::TimeFrame, + ts::TSFrame, stats::Union{Symbol, Pair{<:Base.Callable, <:Union{Symbol, AbstractString}}}...; cols=: ) DataFrames.describe(ts.coredata, stats...; cols=cols) end -TimeFrames.describe( - ts::TimeFrame, +TSFrames.describe( + ts::TSFrame, stats::Union{Symbol, Pair{<:Base.Callable, <:Union{Symbol, AbstractString}}}...; cols=: -) = TimeFrames.describe(stdout, ts, stats...; cols=cols) +) = TSFrames.describe(stdout, ts, stats...; cols=cols) -function Base.show(io::IO, ts::TimeFrame) - title = "$(TimeFrames.nrow(ts))×$(TimeFrames.ncol(ts)) TimeFrame with $(eltype(index(ts))) Index" +function Base.show(io::IO, ts::TSFrame) + title = "$(TSFrames.nrow(ts))×$(TSFrames.ncol(ts)) TSFrame with $(eltype(index(ts))) Index" Base.show(io, ts.coredata; show_row_number=false, title=title) return nothing end -Base.show(ts::TimeFrame) = show(stdout, ts) +Base.show(ts::TSFrame) = show(stdout, ts) -function Base.summary(io::IO, ts::TimeFrame) - println("(", nr(ts), " x ", nc(ts), ") TimeFrame") +function Base.summary(io::IO, ts::TSFrame) + println("(", nr(ts), " x ", nc(ts), ") TSFrame") end """ # Size methods ```julia -nrow(ts::TimeFrame) -nr(ts::TimeFrame) +nrow(ts::TSFrame) +nr(ts::TSFrame) ``` Return the number of rows of `ts`. `nr` is an alias for `nrow`. # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) -julia> ts = TimeFrame(collect(1:10)) -julia> TimeFrames.nrow(ts) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) +julia> ts = TSFrame(collect(1:10)) +julia> TSFrames.nrow(ts) 10 ``` """ -function nrow(ts::TimeFrame) +function nrow(ts::TSFrame) DataFrames.size(ts.coredata)[1] end # alias -nr = TimeFrames.nrow +nr = TSFrames.nrow -function Base.lastindex(ts::TimeFrame) +function Base.lastindex(ts::TSFrame) lastindex(index(ts)) end -function Base.length(ts::TimeFrame) - TimeFrames.nrow(ts) +function Base.length(ts::TSFrame) + TSFrames.nrow(ts) end # Number of columns @@ -120,48 +120,48 @@ end # Size methods ```julia -ncol(ts::TimeFrame) +ncol(ts::TSFrame) ``` Return the number of columns of `ts`. `nc` is an alias for `ncol`. # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) julia> using Random; julia> random(x) = rand(MersenneTwister(123), x); -julia> TimeFrames.ncol(TimeFrame([random(100) random(100) random(100)])) +julia> TSFrames.ncol(TSFrame([random(100) random(100) random(100)])) 3 -julia> nc(TimeFrame([random(100) random(100) random(100)])) +julia> nc(TSFrame([random(100) random(100) random(100)])) 3 ``` """ -function ncol(ts::TimeFrame) +function ncol(ts::TSFrame) DataFrames.size(ts.coredata)[2] - 1 end # alias -nc = TimeFrames.ncol +nc = TSFrames.ncol # Size of """ # Size methods ```julia -size(ts::TimeFrame) +size(ts::TSFrame) ``` Return the number of rows and columns of `ts` as a tuple. # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) -julia> TimeFrames.size(TimeFrame([collect(1:100) collect(1:100) collect(1:100)])) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) +julia> TSFrames.size(TSFrame([collect(1:100) collect(1:100) collect(1:100)])) (100, 3) ``` """ -function size(ts::TimeFrame) - nr = TimeFrames.nrow(ts) - nc = TimeFrames.ncol(ts) +function size(ts::TSFrame) + nr = TSFrames.nrow(ts) + nc = TSFrames.ncol(ts) (nr, nc) end @@ -170,23 +170,23 @@ end # Index column ```julia -index(ts::TimeFrame) +index(ts::TSFrame) ``` Return the index vector from the `coredata` DataFrame. # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) julia> using Random; julia> random(x) = rand(MersenneTwister(123), x); -julia> ts = TimeFrame(random(10), Date("2022-02-01"):Month(1):Date("2022-02-01")+Month(9)); +julia> ts = TSFrame(random(10), Date("2022-02-01"):Month(1):Date("2022-02-01")+Month(9)); julia> show(ts) -(10 x 1) TimeFrame with Dates.Date Index +(10 x 1) TSFrame with Dates.Date Index Index x1 Date Float64 @@ -219,29 +219,29 @@ julia> eltype(index(ts)) Date ``` """ -function index(ts::TimeFrame) +function index(ts::TSFrame) ts.coredata[!, :Index] end """ # Column names ```julia -names(ts::TimeFrame) +names(ts::TSFrame) ``` -Return a `Vector{String}` containing column names of the TimeFrame object, +Return a `Vector{String}` containing column names of the TSFrame object, excludes index. # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) -julia> names(TimeFrame([1:10 11:20])) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) +julia> names(TSFrame([1:10 11:20])) 2-element Vector{String}: "x1" "x2" ``` """ -function names(ts::TimeFrame) +function names(ts::TSFrame) names(ts.coredata[!, Not(:Index)]) end @@ -249,15 +249,15 @@ end """ # First Row ```julia -first(ts::TimeFrame) +first(ts::TSFrame) ``` -Returns the first row of `ts` as a TimeFrame object. +Returns the first row of `ts` as a TSFrame object. # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random) -julia> first(TimeFrame(1:10)) -(10 x 1) TimeFrame with Dates.Date Index +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random) +julia> first(TSFrame(1:10)) +(10 x 1) TSFrame with Dates.Date Index Index x1 Date Float64 @@ -266,22 +266,22 @@ julia> first(TimeFrame(1:10)) ``` """ -function Base.first(ts::TimeFrame) - TimeFrame(Base.first(ts.coredata,1)) +function Base.first(ts::TSFrame) + TSFrame(Base.first(ts.coredata,1)) end """ # Head ```julia -head(ts::TimeFrame, n::Int = 10) +head(ts::TSFrame, n::Int = 10) ``` Returns the first `n` rows of `ts`. # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random) -julia> head(TimeFrame(1:100)) -(10 x 1) TimeFrame with Int64 Index +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random) +julia> head(TSFrame(1:100)) +(10 x 1) TSFrame with Int64 Index Index x1 Int64 Int64 @@ -298,22 +298,22 @@ julia> head(TimeFrame(1:100)) 10 10 ``` """ -function head(ts::TimeFrame, n::Int = 10) - TimeFrame(Base.first(ts.coredata, n)) +function head(ts::TSFrame, n::Int = 10) + TSFrame(Base.first(ts.coredata, n)) end """ # Tail ```julia -tail(ts::TimeFrame, n::Int = 10) +tail(ts::TSFrame, n::Int = 10) ``` Returns the last `n` rows of `ts`. -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random) -julia> tail(TimeFrame(1:100)) -(10 x 1) TimeFrame with Int64 Index +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random) +julia> tail(TSFrame(1:100)) +(10 x 1) TSFrame with Int64 Index Index x1 Int64 Int64 @@ -330,25 +330,25 @@ julia> tail(TimeFrame(1:100)) 100 100 ``` """ -function tail(ts::TimeFrame, n::Int = 10) - TimeFrame(DataFrames.last(ts.coredata, n)) +function tail(ts::TSFrame, n::Int = 10) + TSFrame(DataFrames.last(ts.coredata, n)) end """ # Column Rename ```julia -rename!(ts::TimeFrame, colnames::AbstractVector{String}) -rename!(ts::TimeFrame, colnames::AbstractVector{Symbol}) +rename!(ts::TSFrame, colnames::AbstractVector{String}) +rename!(ts::TSFrame, colnames::AbstractVector{Symbol}) ``` Renames columns of `ts` to the values in `colnames`, in order. Input is a vector of either Strings or Symbols. The `Index` column name is reserved, and `rename!()` will throw an error if `colnames` contains the name `Index`. -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random) julia> ts -(100 x 2) TimeFrame with Int64 Index +(100 x 2) TSFrame with Int64 Index Index x1 x2 Int64 Int64 Int64 @@ -365,7 +365,7 @@ julia> ts 92 rows omitted julia> rename!(ts, ["Col1", "Col2"]) -(100 x 2) TimeFrame with Int64 Index +(100 x 2) TSFrame with Int64 Index Index Col1 Col2 Int64 Int64 Int64 @@ -383,14 +383,14 @@ Int64 Int64 Int64 ``` """ -function rename!(ts::TimeFrame, colnames::AbstractVector{String}) +function rename!(ts::TSFrame, colnames::AbstractVector{String}) rename!(ts, Symbol.(colnames)) end -function rename!(ts::TimeFrame, colnames::AbstractVector{Symbol}) +function rename!(ts::TSFrame, colnames::AbstractVector{Symbol}) idx = findall(i -> i == :Index, colnames) if length(idx) > 0 - error("Column name `Index` not allowed in TimeFrame object") + error("Column name `Index` not allowed in TSFrame object") end cols = copy(colnames) insert!(cols, 1, :Index) @@ -399,9 +399,9 @@ function rename!(ts::TimeFrame, colnames::AbstractVector{Symbol}) end """ -Internal function to check consistency of the Index of a TimeFrame +Internal function to check consistency of the Index of a TSFrame object. """ -function _check_consistency(ts::TimeFrame)::Bool +function _check_consistency(ts::TSFrame)::Bool issorted(index(ts)) end diff --git a/src/vcat.jl b/src/vcat.jl index cb52013..1d0f134 100644 --- a/src/vcat.jl +++ b/src/vcat.jl @@ -2,10 +2,10 @@ # Row-merging (vcat/rbind) ```julia -vcat(ts1::TimeFrame, ts2::TimeFrame; colmerge::Symbol=:union) +vcat(ts1::TSFrame, ts2::TSFrame; colmerge::Symbol=:union) ``` -Concatenate rows of two TimeFrame objects, append `ts2` to `ts1`. +Concatenate rows of two TSFrame objects, append `ts2` to `ts1`. The `colmerge` keyword argument specifies the column merge strategy. The value of `colmerge` is directly passed to `cols` @@ -19,7 +19,7 @@ Currently, `DataFrames.vcat` supports four types of column-merge strategies: 4. `:union`: merge even if columns differ, the resulting object has all the columns filled with `missing`, if necessary. # Examples -```jldoctest; setup = :(using TimeFrames, DataFrames, Dates, Random, Statistics) +```jldoctest; setup = :(using TSFrames, DataFrames, Dates, Random, Statistics) julia> using Random; julia> random(x) = rand(MersenneTwister(123), x); @@ -28,9 +28,9 @@ julia> dates1 = collect(Date(2017,1,1):Day(1):Date(2017,1,10)); julia> dates2 = collect(Date(2017,1,11):Day(1):Date(2017,1,30)); -julia> ts1 = TimeFrame([randn(length(dates1)) randn(length(dates1))], dates1) +julia> ts1 = TSFrame([randn(length(dates1)) randn(length(dates1))], dates1) julia> show(ts1) -(10 x 1) TimeFrame with Dates.Date Index +(10 x 1) TSFrame with Dates.Date Index Index x1 Date Float64 @@ -47,9 +47,9 @@ julia> show(ts1) 2017-01-10 -1.08461 julia> df = DataFrame(x1 = randn(length(dates2)), y1 = randn(length(dates2))) -julia> ts2 = TimeFrame(df, dates2) +julia> ts2 = TSFrame(df, dates2) julia> show(ts2) -(20 x 1) TimeFrame with Dates.Date Index +(20 x 1) TSFrame with Dates.Date Index Index x1 Date Float64 @@ -75,7 +75,7 @@ julia> show(ts2) julia> vcat(ts1, ts2) -(30 x 3) TimeFrame with Date Index +(30 x 3) TSFrame with Date Index Index x1 x2 y1 Date Float64 Float64? Float64? @@ -112,7 +112,7 @@ julia> vcat(ts1, ts2) 2017-01-30 0.516704 missing 0.216855 julia> vcat(ts1, ts2; colmerge=:intersect) -(30 x 1) TimeFrame with Date Index +(30 x 1) TSFrame with Date Index Index x1 Date Float64 @@ -150,9 +150,9 @@ julia> vcat(ts1, ts2; colmerge=:intersect) ``` """ -function Base.vcat(ts1::TimeFrame, ts2::TimeFrame; colmerge=:union) +function Base.vcat(ts1::TSFrame, ts2::TSFrame; colmerge=:union) result_df = DataFrames.vcat(ts1.coredata, ts2.coredata; cols=colmerge) - return TimeFrame(result_df) + return TSFrame(result_df) end # alias rbind = vcat diff --git a/test/TimeFrame.jl b/test/TSFrame.jl similarity index 62% rename from test/TimeFrame.jl rename to test/TSFrame.jl index 7b7b0b3..9ef0ded 100644 --- a/test/TimeFrame.jl +++ b/test/TSFrame.jl @@ -1,62 +1,62 @@ function test_df_index_integer() df = DataFrame(A=["a", "b"], B=[1, 2]) - @test_throws ArgumentError TimeFrame(df) + @test_throws ArgumentError TSFrame(df) df = DataFrame(A=[:a, :b], B=[1, 2]) - @test_throws ArgumentError TimeFrame(df) + @test_throws ArgumentError TSFrame(df) - ts = TimeFrame(df_integer_index, 1) - @test typeof(ts) == TimeFrames.TimeFrame + ts = TSFrame(df_integer_index, 1) + @test typeof(ts) == TSFrames.TSFrame @test ts.coredata == df_integer_index end function test_df_index_timetype() - ts = TimeFrame(df_timetype_index, 1) - @test typeof(ts) == TimeFrames.TimeFrame + ts = TSFrame(df_timetype_index, 1) + @test typeof(ts) == TSFrames.TSFrame @test ts.coredata == df_timetype_index end function test_df_index_symbol() - ts = TimeFrame(df_integer_index, :Index) - @test typeof(ts) == TimeFrames.TimeFrame + ts = TSFrame(df_integer_index, :Index) + @test typeof(ts) == TSFrames.TSFrame @test ts.coredata == df_integer_index end function test_df_index_string() - ts = TimeFrame(df_integer_index, "Index") - @test typeof(ts) == TimeFrames.TimeFrame + ts = TSFrame(df_integer_index, "Index") + @test typeof(ts) == TSFrames.TSFrame @test ts.coredata == df_integer_index end function test_df_index_range() - ts = TimeFrame(df_vector, index_range) - @test typeof(ts) == TimeFrames.TimeFrame + ts = TSFrame(df_vector, index_range) + @test typeof(ts) == TSFrames.TSFrame @test ts.coredata[!, :data] == df_vector[!, :data] end function test_vector_index_vector_integer() - ts = TimeFrame(data_vector, index_integer) - @test typeof(ts) == TimeFrames.TimeFrame + ts = TSFrame(data_vector, index_integer) + @test typeof(ts) == TSFrames.TSFrame @test ts.coredata[!, :Index] == index_integer @test ts.coredata[!, 2] == data_vector end function test_vector_index_vector_timetype() - ts = TimeFrame(data_vector, index_timetype) - @test typeof(ts) == TimeFrames.TimeFrame + ts = TSFrame(data_vector, index_timetype) + @test typeof(ts) == TSFrames.TSFrame @test ts.coredata[!, :Index] == index_timetype @test ts.coredata[!, 2] == data_vector end function test_vector() - ts = TimeFrame(data_vector) - @test typeof(ts) == TimeFrames.TimeFrame + ts = TSFrame(data_vector) + @test typeof(ts) == TSFrames.TSFrame @test ts.coredata[!, :Index] == collect(1:length(data_vector)) @test ts.coredata[!, 2] == data_vector end function test_array() - ts = TimeFrame(data_array) - @test typeof(ts) == TimeFrames.TimeFrame + ts = TSFrame(data_array) + @test typeof(ts) == TSFrames.TSFrame @test typeof(ts.coredata) == DataFrames.DataFrame @test ts.coredata[!, :Index] == collect(1:size(data_vector)[1]) @test Matrix(ts.coredata[!, Not(:Index)]) == data_array @@ -66,16 +66,16 @@ function test_colnames() random(x) = rand(MersenneTwister(123), x) dates = collect(Date(2017,1,1):Day(1):Date(2017,1,10)) - ts = TimeFrame(random(10), colnames=[:A]) + ts = TSFrame(random(10), colnames=[:A]) @test names(ts.coredata) == ["Index", "A"] - ts = TimeFrame(random(10), dates, colnames=[:A]) + ts = TSFrame(random(10), dates, colnames=[:A]) @test names(ts.coredata) == ["Index", "A"] - ts = TimeFrame([random(10) random(10)], colnames=[:A, :B]) + ts = TSFrame([random(10) random(10)], colnames=[:A, :B]) @test names(ts.coredata) == ["Index", "A", "B"] - ts = TimeFrame([random(10) random(10)], dates, colnames=[:A, :B]) + ts = TSFrame([random(10) random(10)], dates, colnames=[:A, :B]) @test names(ts.coredata) == ["Index", "A", "B"] end diff --git a/test/apply.jl b/test/apply.jl index 1696473..7b6fcff 100644 --- a/test/apply.jl +++ b/test/apply.jl @@ -3,56 +3,56 @@ data_vector_1 = randn(DATA_SIZE_1) index_timetype_1 = Date(2007, 1,1) + Day.(0:(DATA_SIZE_1 - 1)) df_timetype_index_1 = DataFrame(Index = index_timetype_1, data = data_vector_1) -ts_daily_1 = TimeFrame(df_timetype_index_1, 1) -ts_daily_matrix_1 = TimeFrame(DataFrames.innerjoin(df_timetype_index_1, df_timetype_index_1, on=:Index, makeunique=true)) +ts_daily_1 = TSFrame(df_timetype_index_1, 1) +ts_daily_matrix_1 = TSFrame(DataFrames.innerjoin(df_timetype_index_1, df_timetype_index_1, on=:Index, makeunique=true)) DATA_SIZE_2 = 86400 data_vector_2 = randn(DATA_SIZE_2) index_timetype_2 = DateTime(2000, 1, 1, 0, 0, 0) + Second.(0:(DATA_SIZE_2 - 1)) df_timetype_index_2 = DataFrame(Index = index_timetype_2, data = data_vector_2) -ts_intraday_2 = TimeFrame(df_timetype_index_2) -ts_daily_matrix_2 = TimeFrame(DataFrames.innerjoin(df_timetype_index_2, df_timetype_index_2, on=:Index, makeunique=true)) +ts_intraday_2 = TSFrame(df_timetype_index_2) +ts_daily_matrix_2 = TSFrame(DataFrames.innerjoin(df_timetype_index_2, df_timetype_index_2, on=:Index, makeunique=true)) -# function apply(ts::TimeFrame, period::Union{T,Type{T}}, fun::V, index_at::Function=first) where {T<:Union{DatePeriod,TimePeriod}, V<:Function} +# function apply(ts::TSFrame, period::Union{T,Type{T}}, fun::V, index_at::Function=first) where {T<:Union{DatePeriod,TimePeriod}, V<:Function} # Resampling # Daily -> Monthly ts_monthly = apply(ts_daily_1, Dates.Month(1), first) -@test typeof(ts_monthly) == TimeFrames.TimeFrame +@test typeof(ts_monthly) == TSFrames.TSFrame @test typeof(ts_monthly.coredata) == DataFrame @test DataFrames.nrow(ts_monthly.coredata) == 12 # 360 days ts_monthly = apply(ts_daily_1, Dates.Month(1), last) -@test typeof(ts_monthly) == TimeFrames.TimeFrame +@test typeof(ts_monthly) == TSFrames.TSFrame @test typeof(ts_monthly.coredata) == DataFrame @test DataFrames.nrow(ts_monthly.coredata) == 12 ts_monthly = apply(ts_daily_1, Dates.Month(1), first) -@test typeof(ts_monthly) == TimeFrames.TimeFrame +@test typeof(ts_monthly) == TSFrames.TSFrame @test typeof(ts_monthly.coredata) == DataFrame @test DataFrames.nrow(ts_monthly.coredata) == 12 ts_monthly = apply(ts_daily_1, Dates.Month(2), first) -@test typeof(ts_monthly) == TimeFrames.TimeFrame +@test typeof(ts_monthly) == TSFrames.TSFrame @test typeof(ts_monthly.coredata) == DataFrame @test DataFrames.nrow(ts_monthly.coredata) == 6 ts_monthly = apply(ts_daily_1, Dates.Month(12), first) -@test typeof(ts_monthly) == TimeFrames.TimeFrame +@test typeof(ts_monthly) == TSFrames.TSFrame @test typeof(ts_monthly.coredata) == DataFrame @test DataFrames.nrow(ts_monthly.coredata) == 1 ts_monthly = apply(ts_daily_1, Dates.Month(100), first) -@test typeof(ts_monthly) == TimeFrames.TimeFrame +@test typeof(ts_monthly) == TSFrames.TSFrame @test typeof(ts_monthly.coredata) == DataFrame @test DataFrames.nrow(ts_monthly.coredata) == 1 ts_monthly = apply(ts_daily_1, Dates.Month(1), Statistics.mean) t = ts_daily_1[:, "data"] -@test typeof(ts_monthly) == TimeFrames.TimeFrame +@test typeof(ts_monthly) == TSFrames.TSFrame @test typeof(ts_monthly.coredata) == DataFrame @test DataFrames.nrow(ts_monthly.coredata) == 12 @test typeof(ts_monthly[:, "data_mean"]) == Vector{Float64} @@ -60,7 +60,7 @@ t = ts_daily_1[:, "data"] ts_monthly = apply(ts_daily_1, Dates.Month(1), sum) t = ts_daily_1[:, "data"] -@test typeof(ts_monthly) == TimeFrames.TimeFrame +@test typeof(ts_monthly) == TSFrames.TSFrame @test typeof(ts_monthly.coredata) == DataFrame @test DataFrames.nrow(ts_monthly.coredata) == 12 @test typeof(ts_monthly[:, "data_sum"]) == Vector{Float64} @@ -68,7 +68,7 @@ t = ts_daily_1[:, "data"] ts_monthly = apply(ts_daily_1, Dates.Month(1), sum, last) t = ts_daily_1[:, "data"] -@test typeof(ts_monthly) == TimeFrames.TimeFrame +@test typeof(ts_monthly) == TSFrames.TSFrame @test typeof(ts_monthly.coredata) == DataFrame @test DataFrames.nrow(ts_monthly.coredata) == 12 @test typeof(ts_monthly[:, "data_sum"]) == Vector{Float64} @@ -76,33 +76,33 @@ t = ts_daily_1[:, "data"] # Daily -> Yearly ts_yearly = apply(ts_daily_1, Dates.Year(1), first) -@test typeof(ts_yearly) == TimeFrames.TimeFrame +@test typeof(ts_yearly) == TSFrames.TSFrame @test typeof(ts_yearly.coredata) == DataFrame @test DataFrames.nrow(ts_yearly.coredata) == 1 ts_yearly = apply(ts_daily_1, Dates.Year(1), last) -@test typeof(ts_yearly) == TimeFrames.TimeFrame +@test typeof(ts_yearly) == TSFrames.TSFrame @test typeof(ts_yearly.coredata) == DataFrame @test DataFrames.nrow(ts_yearly.coredata) == 1 ts_yearly = apply(ts_daily_1, Dates.Year(1), first) -@test typeof(ts_yearly) == TimeFrames.TimeFrame +@test typeof(ts_yearly) == TSFrames.TSFrame @test typeof(ts_yearly.coredata) == DataFrame @test DataFrames.nrow(ts_yearly.coredata) == 1 ts_yearly = apply(ts_daily_1, Dates.Year(2), first) -@test typeof(ts_yearly) == TimeFrames.TimeFrame +@test typeof(ts_yearly) == TSFrames.TSFrame @test typeof(ts_yearly.coredata) == DataFrame @test DataFrames.nrow(ts_yearly.coredata) == 1 ts_yearly = apply(ts_daily_1, Dates.Year(100), first) -@test typeof(ts_yearly) == TimeFrames.TimeFrame +@test typeof(ts_yearly) == TSFrames.TSFrame @test typeof(ts_yearly.coredata) == DataFrame @test DataFrames.nrow(ts_yearly.coredata) == 1 ts_yearly = apply(ts_daily_1, Dates.Year(1), Statistics.mean) t = ts_daily_1[:, "data"] -@test typeof(ts_yearly) == TimeFrames.TimeFrame +@test typeof(ts_yearly) == TSFrames.TSFrame @test typeof(ts_yearly.coredata) == DataFrame @test DataFrames.nrow(ts_yearly.coredata) == 1 @test typeof(ts_yearly[:, "data_mean"]) == Vector{Float64} @@ -110,7 +110,7 @@ t = ts_daily_1[:, "data"] ts_yearly = apply(ts_daily_1, Dates.Year(1), sum) t = ts_daily_1[:, "data"] -@test typeof(ts_yearly) == TimeFrames.TimeFrame +@test typeof(ts_yearly) == TSFrames.TSFrame @test typeof(ts_yearly.coredata) == DataFrame @test DataFrames.nrow(ts_yearly.coredata) == 1 @test typeof(ts_yearly[:, "data_sum"]) == Vector{Float64} @@ -118,7 +118,7 @@ t = ts_daily_1[:, "data"] ts_yearly = apply(ts_daily_1, Dates.Year(1), sum, last) t = ts_daily_1[:, "data"] -@test typeof(ts_yearly) == TimeFrames.TimeFrame +@test typeof(ts_yearly) == TSFrames.TSFrame @test typeof(ts_yearly.coredata) == DataFrame @test DataFrames.nrow(ts_yearly.coredata) == 1 @test typeof(ts_yearly[:, "data_sum"]) == Vector{Float64} @@ -127,69 +127,69 @@ t = ts_daily_1[:, "data"] # Daily -> Weekly ts_weekly = apply(ts_daily_1, Dates.Week(1), first) -@test typeof(ts_weekly) == TimeFrames.TimeFrame +@test typeof(ts_weekly) == TSFrames.TSFrame @test typeof(ts_weekly.coredata) == DataFrame @test DataFrames.nrow(ts_weekly.coredata) == 52 ts_weekly = apply(ts_daily_1, Dates.Week(1), last) -@test typeof(ts_weekly) == TimeFrames.TimeFrame +@test typeof(ts_weekly) == TSFrames.TSFrame @test typeof(ts_weekly.coredata) == DataFrame @test DataFrames.nrow(ts_weekly.coredata) == 52 ts_weekly = apply(ts_daily_1, Dates.Week(1), first) -@test typeof(ts_weekly) == TimeFrames.TimeFrame +@test typeof(ts_weekly) == TSFrames.TSFrame @test typeof(ts_weekly.coredata) == DataFrame @test DataFrames.nrow(ts_weekly.coredata) == 52 ts_weekly = apply(ts_daily_1, Dates.Week(2), first) -@test typeof(ts_weekly) == TimeFrames.TimeFrame +@test typeof(ts_weekly) == TSFrames.TSFrame @test typeof(ts_weekly.coredata) == DataFrame @test DataFrames.nrow(ts_weekly.coredata) == 26 ts_weekly = apply(ts_daily_1, Dates.Week(52), first) -@test typeof(ts_weekly) == TimeFrames.TimeFrame +@test typeof(ts_weekly) == TSFrames.TSFrame @test typeof(ts_weekly.coredata) == DataFrame @test DataFrames.nrow(ts_weekly.coredata) == 1 ts_weekly = apply(ts_daily_1, Dates.Week(100), first) -@test typeof(ts_weekly) == TimeFrames.TimeFrame +@test typeof(ts_weekly) == TSFrames.TSFrame @test typeof(ts_weekly.coredata) == DataFrame @test DataFrames.nrow(ts_weekly.coredata) == 1 # Daily -> Quarterly ts_quarterly = apply(ts_daily_1, Dates.Quarter(1), first) -@test typeof(ts_quarterly) == TimeFrames.TimeFrame +@test typeof(ts_quarterly) == TSFrames.TSFrame @test typeof(ts_quarterly.coredata) == DataFrame @test DataFrames.nrow(ts_quarterly.coredata) == 4 ts_quarterly = apply(ts_daily_1, Dates.Quarter(1), last) -@test typeof(ts_quarterly) == TimeFrames.TimeFrame +@test typeof(ts_quarterly) == TSFrames.TSFrame @test typeof(ts_quarterly.coredata) == DataFrame @test DataFrames.nrow(ts_quarterly.coredata) == 4 ts_quarterly = apply(ts_daily_1, Dates.Quarter(1), first) -@test typeof(ts_quarterly) == TimeFrames.TimeFrame +@test typeof(ts_quarterly) == TSFrames.TSFrame @test typeof(ts_quarterly.coredata) == DataFrame @test DataFrames.nrow(ts_quarterly.coredata) == 4 ts_quarterly = apply(ts_daily_1, Dates.Quarter(2), first) -@test typeof(ts_quarterly) == TimeFrames.TimeFrame +@test typeof(ts_quarterly) == TSFrames.TSFrame @test typeof(ts_quarterly.coredata) == DataFrame @test DataFrames.nrow(ts_quarterly.coredata) == 2 ts_quarterly = apply(ts_daily_1, Dates.Quarter(4), first) -@test typeof(ts_quarterly) == TimeFrames.TimeFrame +@test typeof(ts_quarterly) == TSFrames.TSFrame @test typeof(ts_quarterly.coredata) == DataFrame @test DataFrames.nrow(ts_quarterly.coredata) == 1 ts_quarterly = apply(ts_daily_1, Dates.Quarter(100), first) -@test typeof(ts_quarterly) == TimeFrames.TimeFrame +@test typeof(ts_quarterly) == TSFrames.TSFrame @test typeof(ts_quarterly.coredata) == DataFrame @test DataFrames.nrow(ts_quarterly.coredata) == 1 ts_quarterly = apply(ts_daily_1, Dates.Quarter(1), Statistics.mean) t = ts_daily_1[:, "data"] -@test typeof(ts_quarterly) == TimeFrames.TimeFrame +@test typeof(ts_quarterly) == TSFrames.TSFrame @test typeof(ts_quarterly.coredata) == DataFrame @test DataFrames.nrow(ts_quarterly.coredata) == 4 @test typeof(ts_quarterly[:, "data_mean"]) == Vector{Float64} @@ -197,7 +197,7 @@ t = ts_daily_1[:, "data"] ts_quarterly = apply(ts_daily_1, Dates.Quarter(1), sum) t = ts_daily_1[:, "data"] -@test typeof(ts_quarterly) == TimeFrames.TimeFrame +@test typeof(ts_quarterly) == TSFrames.TSFrame @test typeof(ts_quarterly.coredata) == DataFrame @test DataFrames.nrow(ts_quarterly.coredata) == 4 @test typeof(ts_quarterly[:, "data_sum"]) == Vector{Float64} @@ -205,7 +205,7 @@ t = ts_daily_1[:, "data"] ts_quarterly = apply(ts_daily_1, Dates.Quarter(1), sum, last) t = ts_daily_1[:, "data"] -@test typeof(ts_quarterly) == TimeFrames.TimeFrame +@test typeof(ts_quarterly) == TSFrames.TSFrame @test typeof(ts_quarterly.coredata) == DataFrame @test DataFrames.nrow(ts_quarterly.coredata) == 4 @test typeof(ts_quarterly[:, "data_sum"]) == Vector{Float64} @@ -215,200 +215,200 @@ t = ts_daily_1[:, "data"] # Daily -> Daily ts_test_daily = apply(ts_daily_1, Dates.Day(1), first) -@test typeof(ts_test_daily) == TimeFrames.TimeFrame +@test typeof(ts_test_daily) == TSFrames.TSFrame @test typeof(ts_test_daily.coredata) == DataFrame @test DataFrames.nrow(ts_test_daily.coredata) == 360 ts_test_daily = apply(ts_daily_1, Dates.Day(1), last) -@test typeof(ts_test_daily) == TimeFrames.TimeFrame +@test typeof(ts_test_daily) == TSFrames.TSFrame @test typeof(ts_test_daily.coredata) == DataFrame @test DataFrames.nrow(ts_test_daily.coredata) == 360 ts_test_daily = apply(ts_daily_1, Dates.Day(1), first) -@test typeof(ts_test_daily) == TimeFrames.TimeFrame +@test typeof(ts_test_daily) == TSFrames.TSFrame @test typeof(ts_test_daily.coredata) == DataFrame @test DataFrames.nrow(ts_test_daily.coredata) == 360 ts_test_daily = apply(ts_daily_1, Dates.Day(2), first) -@test typeof(ts_test_daily) == TimeFrames.TimeFrame +@test typeof(ts_test_daily) == TSFrames.TSFrame @test typeof(ts_test_daily.coredata) == DataFrame @test DataFrames.nrow(ts_test_daily.coredata) == 180 ts_test_daily = apply(ts_daily_1, Dates.Day(3), first) -@test typeof(ts_test_daily) == TimeFrames.TimeFrame +@test typeof(ts_test_daily) == TSFrames.TSFrame @test typeof(ts_test_daily.coredata) == DataFrame @test DataFrames.nrow(ts_test_daily.coredata) == 120 ts_test_daily = apply(ts_daily_1, Dates.Day(180), first) -@test typeof(ts_test_daily) == TimeFrames.TimeFrame +@test typeof(ts_test_daily) == TSFrames.TSFrame @test typeof(ts_test_daily.coredata) == DataFrame @test DataFrames.nrow(ts_test_daily.coredata) == 2 ts_test_daily = apply(ts_daily_1, Dates.Day(360), first) -@test typeof(ts_test_daily) == TimeFrames.TimeFrame +@test typeof(ts_test_daily) == TSFrames.TSFrame @test typeof(ts_test_daily.coredata) == DataFrame @test DataFrames.nrow(ts_test_daily.coredata) == 1 ts_test_daily = apply(ts_daily_1, Dates.Day(1000), first) -@test typeof(ts_test_daily) == TimeFrames.TimeFrame +@test typeof(ts_test_daily) == TSFrames.TSFrame @test typeof(ts_test_daily.coredata) == DataFrame @test DataFrames.nrow(ts_test_daily.coredata) == 1 # Secondly -> Yearly ts_yearly = apply(ts_intraday_2, Dates.Year(1), first) -@test typeof(ts_yearly) == TimeFrames.TimeFrame +@test typeof(ts_yearly) == TSFrames.TSFrame @test typeof(ts_yearly.coredata) == DataFrame @test DataFrames.nrow(ts_yearly.coredata) == 1 ts_yearly = apply(ts_intraday_2, Dates.Year(1), last) -@test typeof(ts_yearly) == TimeFrames.TimeFrame +@test typeof(ts_yearly) == TSFrames.TSFrame @test typeof(ts_yearly.coredata) == DataFrame @test DataFrames.nrow(ts_yearly.coredata) == 1 ts_yearly = apply(ts_intraday_2, Dates.Year(1), first) -@test typeof(ts_yearly) == TimeFrames.TimeFrame +@test typeof(ts_yearly) == TSFrames.TSFrame @test typeof(ts_yearly.coredata) == DataFrame @test DataFrames.nrow(ts_yearly.coredata) == 1 ts_yearly = apply(ts_intraday_2, Dates.Year(100), first) -@test typeof(ts_yearly) == TimeFrames.TimeFrame +@test typeof(ts_yearly) == TSFrames.TSFrame @test typeof(ts_yearly.coredata) == DataFrame @test DataFrames.nrow(ts_yearly.coredata) == 1 # Secondly -> Monthly ts_monthly = apply(ts_intraday_2, Dates.Month(1), first) -@test typeof(ts_monthly) == TimeFrames.TimeFrame +@test typeof(ts_monthly) == TSFrames.TSFrame @test typeof(ts_monthly.coredata) == DataFrame @test DataFrames.nrow(ts_monthly.coredata) == 1 ts_monthly = apply(ts_intraday_2, Dates.Month(1), last) -@test typeof(ts_monthly) == TimeFrames.TimeFrame +@test typeof(ts_monthly) == TSFrames.TSFrame @test typeof(ts_monthly.coredata) == DataFrame @test DataFrames.nrow(ts_monthly.coredata) == 1 ts_monthly = apply(ts_intraday_2, Dates.Month(1), first) -@test typeof(ts_monthly) == TimeFrames.TimeFrame +@test typeof(ts_monthly) == TSFrames.TSFrame @test typeof(ts_monthly.coredata) == DataFrame @test DataFrames.nrow(ts_monthly.coredata) == 1 ts_monthly = apply(ts_intraday_2, Dates.Month(100), first) -@test typeof(ts_monthly) == TimeFrames.TimeFrame +@test typeof(ts_monthly) == TSFrames.TSFrame @test typeof(ts_monthly.coredata) == DataFrame @test DataFrames.nrow(ts_monthly.coredata) == 1 # Secondly -> Weekly ts_weekly = apply(ts_intraday_2, Dates.Week(1), first) -@test typeof(ts_weekly) == TimeFrames.TimeFrame +@test typeof(ts_weekly) == TSFrames.TSFrame @test typeof(ts_weekly.coredata) == DataFrame @test DataFrames.nrow(ts_weekly.coredata) == 1 ts_weekly = apply(ts_intraday_2, Dates.Week(1), last) -@test typeof(ts_weekly) == TimeFrames.TimeFrame +@test typeof(ts_weekly) == TSFrames.TSFrame @test typeof(ts_weekly.coredata) == DataFrame @test DataFrames.nrow(ts_weekly.coredata) == 1 ts_weekly = apply(ts_intraday_2, Dates.Week(1), first) -@test typeof(ts_weekly) == TimeFrames.TimeFrame +@test typeof(ts_weekly) == TSFrames.TSFrame @test typeof(ts_weekly.coredata) == DataFrame @test DataFrames.nrow(ts_weekly.coredata) == 1 ts_weekly = apply(ts_intraday_2, Dates.Week(100), first) -@test typeof(ts_weekly) == TimeFrames.TimeFrame +@test typeof(ts_weekly) == TSFrames.TSFrame @test typeof(ts_weekly.coredata) == DataFrame @test DataFrames.nrow(ts_weekly.coredata) == 1 # Secondly -> Quarterly ts_quarterly = apply(ts_intraday_2, Dates.Quarter(1), first) -@test typeof(ts_quarterly) == TimeFrames.TimeFrame +@test typeof(ts_quarterly) == TSFrames.TSFrame @test typeof(ts_quarterly.coredata) == DataFrame @test DataFrames.nrow(ts_quarterly.coredata) == 1 ts_quarterly = apply(ts_intraday_2, Dates.Quarter(1), last) -@test typeof(ts_quarterly) == TimeFrames.TimeFrame +@test typeof(ts_quarterly) == TSFrames.TSFrame @test typeof(ts_quarterly.coredata) == DataFrame @test DataFrames.nrow(ts_quarterly.coredata) == 1 ts_quarterly = apply(ts_intraday_2, Dates.Quarter(1), first) -@test typeof(ts_quarterly) == TimeFrames.TimeFrame +@test typeof(ts_quarterly) == TSFrames.TSFrame @test typeof(ts_quarterly.coredata) == DataFrame @test DataFrames.nrow(ts_quarterly.coredata) == 1 ts_quarterly = apply(ts_intraday_2, Dates.Quarter(100), first) -@test typeof(ts_quarterly) == TimeFrames.TimeFrame +@test typeof(ts_quarterly) == TSFrames.TSFrame @test typeof(ts_quarterly.coredata) == DataFrame @test DataFrames.nrow(ts_quarterly.coredata) == 1 # Secondly -> Daily ts_test_daily = apply(ts_intraday_2, Dates.Day(1), first) -@test typeof(ts_test_daily) == TimeFrames.TimeFrame +@test typeof(ts_test_daily) == TSFrames.TSFrame @test typeof(ts_test_daily.coredata) == DataFrame @test DataFrames.nrow(ts_test_daily.coredata) == 1 ts_test_daily = apply(ts_intraday_2, Dates.Day(1), last) -@test typeof(ts_test_daily) == TimeFrames.TimeFrame +@test typeof(ts_test_daily) == TSFrames.TSFrame @test typeof(ts_test_daily.coredata) == DataFrame @test DataFrames.nrow(ts_test_daily.coredata) == 1 ts_test_daily = apply(ts_intraday_2, Dates.Day(1), first) -@test typeof(ts_test_daily) == TimeFrames.TimeFrame +@test typeof(ts_test_daily) == TSFrames.TSFrame @test typeof(ts_test_daily.coredata) == DataFrame @test DataFrames.nrow(ts_test_daily.coredata) == 1 ts_test_daily = apply(ts_intraday_2, Dates.Day(100), first) -@test typeof(ts_test_daily) == TimeFrames.TimeFrame +@test typeof(ts_test_daily) == TSFrames.TSFrame @test typeof(ts_test_daily.coredata) == DataFrame @test DataFrames.nrow(ts_test_daily.coredata) == 1 # Secondly -> Hourly ts_hourly = apply(ts_intraday_2, Dates.Hour(1), first) -@test typeof(ts_hourly) == TimeFrames.TimeFrame +@test typeof(ts_hourly) == TSFrames.TSFrame @test typeof(ts_hourly.coredata) == DataFrame @test DataFrames.nrow(ts_hourly.coredata) == 24 ts_hourly = apply(ts_intraday_2, Dates.Hour(1), last) -@test typeof(ts_hourly) == TimeFrames.TimeFrame +@test typeof(ts_hourly) == TSFrames.TSFrame @test typeof(ts_hourly.coredata) == DataFrame @test DataFrames.nrow(ts_hourly.coredata) == 24 ts_hourly = apply(ts_intraday_2, Dates.Hour(1), first) -@test typeof(ts_hourly) == TimeFrames.TimeFrame +@test typeof(ts_hourly) == TSFrames.TSFrame @test typeof(ts_hourly.coredata) == DataFrame @test DataFrames.nrow(ts_hourly.coredata) == 24 ts_hourly = apply(ts_intraday_2, Dates.Hour(2), first) -@test typeof(ts_hourly) == TimeFrames.TimeFrame +@test typeof(ts_hourly) == TSFrames.TSFrame @test typeof(ts_hourly.coredata) == DataFrame @test DataFrames.nrow(ts_hourly.coredata) == 12 ts_hourly = apply(ts_intraday_2, Dates.Hour(3), first) -@test typeof(ts_hourly) == TimeFrames.TimeFrame +@test typeof(ts_hourly) == TSFrames.TSFrame @test typeof(ts_hourly.coredata) == DataFrame @test DataFrames.nrow(ts_hourly.coredata) == 8 ts_hourly = apply(ts_intraday_2, Dates.Hour(12), first) -@test typeof(ts_hourly) == TimeFrames.TimeFrame +@test typeof(ts_hourly) == TSFrames.TSFrame @test typeof(ts_hourly.coredata) == DataFrame @test DataFrames.nrow(ts_hourly.coredata) == 2 ts_hourly = apply(ts_intraday_2, Dates.Hour(24), first) -@test typeof(ts_hourly) == TimeFrames.TimeFrame +@test typeof(ts_hourly) == TSFrames.TSFrame @test typeof(ts_hourly.coredata) == DataFrame @test DataFrames.nrow(ts_hourly.coredata) == 1 ts_hourly = apply(ts_intraday_2, Dates.Hour(100), first) -@test typeof(ts_hourly) == TimeFrames.TimeFrame +@test typeof(ts_hourly) == TSFrames.TSFrame @test typeof(ts_hourly.coredata) == DataFrame @test DataFrames.nrow(ts_hourly.coredata) == 1 ts_hourly = apply(ts_intraday_2, Dates.Hour(1), Statistics.mean) t = ts_intraday_2[:, "data"] -@test typeof(ts_hourly) == TimeFrames.TimeFrame +@test typeof(ts_hourly) == TSFrames.TSFrame @test typeof(ts_hourly.coredata) == DataFrame @test DataFrames.nrow(ts_hourly.coredata) == 24 @test typeof(ts_hourly[:, "data_mean"]) == Vector{Float64} @@ -416,7 +416,7 @@ t = ts_intraday_2[:, "data"] ts_hourly = apply(ts_intraday_2, Dates.Hour(1), sum) t = ts_intraday_2[:, "data"] -@test typeof(ts_hourly) == TimeFrames.TimeFrame +@test typeof(ts_hourly) == TSFrames.TSFrame @test typeof(ts_hourly.coredata) == DataFrame @test DataFrames.nrow(ts_hourly.coredata) == 24 @test typeof(ts_hourly[:, "data_sum"]) == Vector{Float64} @@ -424,7 +424,7 @@ t = ts_intraday_2[:, "data"] ts_hourly = apply(ts_intraday_2, Dates.Hour(1), sum, last) t = ts_intraday_2[:, "data"] -@test typeof(ts_hourly) == TimeFrames.TimeFrame +@test typeof(ts_hourly) == TSFrames.TSFrame @test typeof(ts_hourly.coredata) == DataFrame @test DataFrames.nrow(ts_hourly.coredata) == 24 @test typeof(ts_hourly[:, "data_sum"]) == Vector{Float64} @@ -433,7 +433,7 @@ t = ts_intraday_2[:, "data"] ts_hourly = apply(ts_intraday_2, Dates.Hour(2), sum, last) t = ts_intraday_2[:, "data"] -@test typeof(ts_hourly) == TimeFrames.TimeFrame +@test typeof(ts_hourly) == TSFrames.TSFrame @test typeof(ts_hourly.coredata) == DataFrame @test DataFrames.nrow(ts_hourly.coredata) == 12 @test typeof(ts_hourly[:, "data_sum"]) == Vector{Float64} @@ -442,43 +442,43 @@ t = ts_intraday_2[:, "data"] # Secondly -> Minutely ts_minutely = apply(ts_intraday_2, Dates.Minute(1), first) -@test typeof(ts_minutely) == TimeFrames.TimeFrame +@test typeof(ts_minutely) == TSFrames.TSFrame @test typeof(ts_minutely.coredata) == DataFrame @test DataFrames.nrow(ts_minutely.coredata) == 1440 ts_minutely = apply(ts_intraday_2, Dates.Minute(1), last) -@test typeof(ts_minutely) == TimeFrames.TimeFrame +@test typeof(ts_minutely) == TSFrames.TSFrame @test typeof(ts_minutely.coredata) == DataFrame @test DataFrames.nrow(ts_minutely.coredata) == 1440 ts_minutely = apply(ts_intraday_2, Dates.Minute(1), first) -@test typeof(ts_minutely) == TimeFrames.TimeFrame +@test typeof(ts_minutely) == TSFrames.TSFrame @test typeof(ts_minutely.coredata) == DataFrame @test DataFrames.nrow(ts_minutely.coredata) == 1440 ts_minutely = apply(ts_intraday_2, Dates.Minute(2), first) -@test typeof(ts_minutely) == TimeFrames.TimeFrame +@test typeof(ts_minutely) == TSFrames.TSFrame @test typeof(ts_minutely.coredata) == DataFrame @test DataFrames.nrow(ts_minutely.coredata) == 720 ts_minutely = apply(ts_intraday_2, Dates.Minute(720), first) -@test typeof(ts_minutely) == TimeFrames.TimeFrame +@test typeof(ts_minutely) == TSFrames.TSFrame @test typeof(ts_minutely.coredata) == DataFrame @test DataFrames.nrow(ts_minutely.coredata) == 2 ts_minutely = apply(ts_intraday_2, Dates.Minute(1440), first) -@test typeof(ts_minutely) == TimeFrames.TimeFrame +@test typeof(ts_minutely) == TSFrames.TSFrame @test typeof(ts_minutely.coredata) == DataFrame @test DataFrames.nrow(ts_minutely.coredata) == 1 ts_minutely = apply(ts_intraday_2, Dates.Minute(100000), first) -@test typeof(ts_minutely) == TimeFrames.TimeFrame +@test typeof(ts_minutely) == TSFrames.TSFrame @test typeof(ts_minutely.coredata) == DataFrame @test DataFrames.nrow(ts_minutely.coredata) == 1 ts_minutely = apply(ts_intraday_2, Dates.Minute(1), Statistics.mean) t = ts_intraday_2[:, "data"] -@test typeof(ts_minutely) == TimeFrames.TimeFrame +@test typeof(ts_minutely) == TSFrames.TSFrame @test typeof(ts_minutely.coredata) == DataFrame @test DataFrames.nrow(ts_minutely.coredata) == 1440 @test typeof(ts_minutely[:, "data_mean"]) == Vector{Float64} @@ -486,7 +486,7 @@ t = ts_intraday_2[:, "data"] ts_minutely = apply(ts_intraday_2, Dates.Minute(1), sum) t = ts_intraday_2[:, "data"] -@test typeof(ts_minutely) == TimeFrames.TimeFrame +@test typeof(ts_minutely) == TSFrames.TSFrame @test typeof(ts_minutely.coredata) == DataFrame @test DataFrames.nrow(ts_minutely.coredata) == 1440 @test typeof(ts_minutely[:, "data_sum"]) == Vector{Float64} @@ -494,7 +494,7 @@ t = ts_intraday_2[:, "data"] ts_minutely = apply(ts_intraday_2, Dates.Minute(1), sum, last) t = ts_intraday_2[:, "data"] -@test typeof(ts_minutely) == TimeFrames.TimeFrame +@test typeof(ts_minutely) == TSFrames.TSFrame @test typeof(ts_minutely.coredata) == DataFrame @test DataFrames.nrow(ts_minutely.coredata) == 1440 @test typeof(ts_minutely[:, "data_sum"]) == Vector{Float64} @@ -503,7 +503,7 @@ t = ts_intraday_2[:, "data"] ts_minutely = apply(ts_intraday_2, Dates.Minute(2), sum, last) t = ts_intraday_2[:, "data"] -@test typeof(ts_minutely) == TimeFrames.TimeFrame +@test typeof(ts_minutely) == TSFrames.TSFrame @test typeof(ts_minutely.coredata) == DataFrame @test DataFrames.nrow(ts_minutely.coredata) == 720 @test typeof(ts_minutely[:, "data_sum"]) == Vector{Float64} @@ -513,44 +513,44 @@ t = ts_intraday_2[:, "data"] # Secondly -> Secondly ts_secondly = apply(ts_intraday_2, Dates.Second(1), first) -@test typeof(ts_secondly) == TimeFrames.TimeFrame +@test typeof(ts_secondly) == TSFrames.TSFrame @test typeof(ts_secondly.coredata) == DataFrame @test DataFrames.nrow(ts_secondly.coredata) == 86400 ts_secondly = apply(ts_intraday_2, Dates.Second(1), last) -@test typeof(ts_secondly) == TimeFrames.TimeFrame +@test typeof(ts_secondly) == TSFrames.TSFrame @test typeof(ts_secondly.coredata) == DataFrame @test DataFrames.nrow(ts_secondly.coredata) == 86400 ts_secondly = apply(ts_intraday_2, Dates.Second(1), first) -@test typeof(ts_secondly) == TimeFrames.TimeFrame +@test typeof(ts_secondly) == TSFrames.TSFrame @test typeof(ts_secondly.coredata) == DataFrame @test DataFrames.nrow(ts_secondly.coredata) == 86400 @test ts_secondly[:, "data_first"] == ts_intraday_2[:, "data"] ts_secondly = apply(ts_intraday_2, Dates.Second(2), first) -@test typeof(ts_secondly) == TimeFrames.TimeFrame +@test typeof(ts_secondly) == TSFrames.TSFrame @test typeof(ts_secondly.coredata) == DataFrame @test DataFrames.nrow(ts_secondly.coredata) == 43200 ts_secondly = apply(ts_intraday_2, Dates.Second(43200), first) -@test typeof(ts_secondly) == TimeFrames.TimeFrame +@test typeof(ts_secondly) == TSFrames.TSFrame @test typeof(ts_secondly.coredata) == DataFrame @test DataFrames.nrow(ts_secondly.coredata) == 2 ts_secondly = apply(ts_intraday_2, Dates.Second(86400), first) -@test typeof(ts_secondly) == TimeFrames.TimeFrame +@test typeof(ts_secondly) == TSFrames.TSFrame @test typeof(ts_secondly.coredata) == DataFrame @test DataFrames.nrow(ts_secondly.coredata) == 1 ts_secondly = apply(ts_intraday_2, Dates.Second(100000), first) -@test typeof(ts_secondly) == TimeFrames.TimeFrame +@test typeof(ts_secondly) == TSFrames.TSFrame @test typeof(ts_secondly.coredata) == DataFrame @test DataFrames.nrow(ts_secondly.coredata) == 1 ts_secondly = apply(ts_intraday_2, Dates.Second(1), Statistics.mean) t = ts_intraday_2[:, "data"] -@test typeof(ts_secondly) == TimeFrames.TimeFrame +@test typeof(ts_secondly) == TSFrames.TSFrame @test typeof(ts_secondly.coredata) == DataFrame @test DataFrames.nrow(ts_secondly.coredata) == 86400 @test typeof(ts_secondly[:, "data_mean"]) == Vector{Float64} @@ -558,7 +558,7 @@ t = ts_intraday_2[:, "data"] ts_secondly = apply(ts_intraday_2, Dates.Second(1), sum) t = ts_intraday_2[:, "data"] -@test typeof(ts_secondly) == TimeFrames.TimeFrame +@test typeof(ts_secondly) == TSFrames.TSFrame @test typeof(ts_secondly.coredata) == DataFrame @test DataFrames.nrow(ts_secondly.coredata) == 86400 @test typeof(ts_secondly[:, "data_sum"]) == Vector{Float64} @@ -566,7 +566,7 @@ t = ts_intraday_2[:, "data"] ts_secondly = apply(ts_intraday_2, Dates.Second(1), sum, last) t = ts_intraday_2[:, "data"] -@test typeof(ts_secondly) == TimeFrames.TimeFrame +@test typeof(ts_secondly) == TSFrames.TSFrame @test typeof(ts_secondly.coredata) == DataFrame @test DataFrames.nrow(ts_secondly.coredata) == 86400 @test typeof(ts_secondly[:, "data_sum"]) == Vector{Float64} @@ -575,7 +575,7 @@ t = ts_intraday_2[:, "data"] ts_secondly = apply(ts_intraday_2, Dates.Second(2), sum, last) t = ts_intraday_2[:, "data"] -@test typeof(ts_secondly) == TimeFrames.TimeFrame +@test typeof(ts_secondly) == TSFrames.TSFrame @test typeof(ts_secondly.coredata) == DataFrame @test DataFrames.nrow(ts_secondly.coredata) == 43200 @test typeof(ts_secondly[:, "data_sum"]) == Vector{Float64} @@ -583,4 +583,4 @@ t = ts_intraday_2[:, "data"] @test ts_secondly[DateTime(2000, 1, 1, 0, 0, 1)][1,1] ≈ sum(t[1:2]) # Check temp colname -@test !all(occursin.(["A", "B", "C"], TimeFrames.get_tmp_colname(["A", "B", "C"]))) +@test !all(occursin.(["A", "B", "C"], TSFrames.get_tmp_colname(["A", "B", "C"]))) diff --git a/test/broadcasting.jl b/test/broadcasting.jl index 640a469..4dc419a 100644 --- a/test/broadcasting.jl +++ b/test/broadcasting.jl @@ -1,10 +1,10 @@ as = rand(-10000:10000, 100) / 77 bs = rand(-10000:10000, 100) / 77 -ts = TimeFrame(DataFrame(Index = 1:100, A = as, B = bs)) +ts = TSFrame(DataFrame(Index = 1:100, A = as, B = bs)) # testing sin function sin_ts = sin.(ts) -@test typeof(sin_ts) == TimeFrame +@test typeof(sin_ts) == TSFrame @test ts[:, :Index] == sin_ts[:, :Index] for i in 1:100 @test sin_ts[i, :A_sin] == sin(ts[i, :A]) @@ -13,7 +13,7 @@ end # testing log function on one column log_ts_A = log.(Complex.(ts[:, [:A]])) -@test typeof(log_ts_A) == TimeFrame +@test typeof(log_ts_A) == TSFrame @test ts[:, :Index] == log_ts_A[:, :Index] for i in 1:100 @test log_ts_A[i, :A_Complex_log] == log(Complex(ts[i, :A])) diff --git a/test/diff.jl b/test/diff.jl index 0b863a4..f08cd32 100644 --- a/test/diff.jl +++ b/test/diff.jl @@ -1,4 +1,4 @@ -ts = TimeFrame(integer_data_vector, index_timetype) +ts = TSFrame(integer_data_vector, index_timetype) # when period is atmost DATA_SIZE for period in [1, Int(floor(DATA_SIZE/2)), DATA_SIZE] @@ -11,7 +11,7 @@ for period in [1, Int(floor(DATA_SIZE/2)), DATA_SIZE] @test isequal(Vector{Missing}(ts_diff[1:period, :x1]), fill(missing, period)) # the rest of the values must be the differences - @test isequal(ts_diff[(period + 1):TimeFrames.nrow(ts), :x1], ts[(period + 1):TimeFrames.nrow(ts), :x1] - ts[1:TimeFrames.nrow(ts) - period, :x1]) + @test isequal(ts_diff[(period + 1):TSFrames.nrow(ts), :x1], ts[(period + 1):TSFrames.nrow(ts), :x1] - ts[1:TSFrames.nrow(ts) - period, :x1]) end # when period is greater than DATA_SIZE diff --git a/test/endpoints.jl b/test/endpoints.jl index b9a80f4..a3533bc 100644 --- a/test/endpoints.jl +++ b/test/endpoints.jl @@ -4,35 +4,35 @@ ## ::Date dates = collect(Date(2007, 1, 2):Day(1):Date(2007, 7, 1)); -tsdaily = TimeFrame(random(length(dates)), dates); +tsdaily = TSFrame(random(length(dates)), dates); dates5 = collect(Date(2007, 1, 2):Day(5):Date(2007, 7, 1)); ## ::DateTime datetimes = collect(DateTime(2007, 1, 2):Day(1):DateTime(2007, 7, 1)); -tsdatetimes = TimeFrame(random(length(datetimes)), datetimes); +tsdatetimes = TSFrame(random(length(datetimes)), datetimes); ## ::Integer indexinteger = [-3, -2, -1, 0, 1, 2, 3]; -tsinteger = TimeFrame(collect(1:7), indexinteger); +tsinteger = TSFrame(collect(1:7), indexinteger); ## ::DateTime (Hour) datetimehours = collect(range(DateTime(today()), DateTime(today()) + Day(2), step = Hour(1))); -tshours = TimeFrame(random(length(datetimehours)), datetimehours); +tshours = TSFrame(random(length(datetimehours)), datetimehours); ## ::DateTime (Minute) datetimeminutes = collect(range(DateTime(today()) + Hour(9), DateTime(today()) + Hour(15) + Minute(29), step=Minute(1))); -tsminutes = TimeFrame(random(length(datetimeminutes)), datetimeminutes); +tsminutes = TSFrame(random(length(datetimeminutes)), datetimeminutes); ## ::DateTime (Second) datetimeseconds = collect(range(DateTime(today()) + Hour(9), DateTime(today()) + Hour(15) + Minute(29), step=Second(1))); -tsseconds = TimeFrame(random(length(datetimeseconds)), datetimeseconds); +tsseconds = TSFrame(random(length(datetimeseconds)), datetimeseconds); ## ::DateTime (Second(10)) datetime10seconds = collect(range(DateTime(today()) + Hour(9), @@ -43,7 +43,7 @@ datetime10seconds = collect(range(DateTime(today()) + Hour(9), datetimemilliseconds = collect(range(DateTime(today()) + Hour(9), DateTime(today()) + Hour(9) + Minute(59), step=Millisecond(500))); -tsmilliseconds = TimeFrame(random(length(datetimemilliseconds)), datetimemilliseconds); +tsmilliseconds = TSFrame(random(length(datetimemilliseconds)), datetimemilliseconds); ## ::Time (Hour) timestampshours = collect(Time(9, 0, 0):Hour(1):Time(15, 0, 0)) @@ -59,11 +59,11 @@ timestampsmilliseconds = collect(Time(9, 0, 0):Millisecond(1):Time(9, 0, 2)) ## ::Time (Microsecond) timestampsmicroseconds = collect(Time(9, 0, 0):Microsecond(1):Time(9, 0, 0, 2)) -tsmicroseconds = TimeFrame(1:length(timestampsmicroseconds), timestampsmicroseconds) +tsmicroseconds = TSFrame(1:length(timestampsmicroseconds), timestampsmicroseconds) ## ::Time (Nanosecond) timestampsnanoseconds = collect(Time(9, 0, 0):Nanosecond(1):Time(9, 0, 0, 0, 2)) -tsnanoseconds = TimeFrame(1:length(timestampsnanoseconds), timestampsnanoseconds) +tsnanoseconds = TSFrame(1:length(timestampsnanoseconds), timestampsnanoseconds) ## # endpoints(values::AbstractVector, on::Function, k::Int=1) @@ -77,7 +77,7 @@ tsnanoseconds = TimeFrame(1:length(timestampsnanoseconds), timestampsnanoseconds @test endpoints(indexinteger, i -> i, length(indexinteger)+1) == [lastindex(indexinteger)] ## -# endpoints(ts::TimeFrame, on::Function, k::Int=1) +# endpoints(ts::TSFrame, on::Function, k::Int=1) ## @test_throws DomainError endpoints(tsdaily, i -> dayofweek.(i), -1) @test_throws DomainError endpoints(tsdaily, i -> dayofweek.(i), 0) @@ -165,10 +165,10 @@ ep1 = endpoints(datetimes, Week(2))[1]; ## # endpoints(dates::AbstractVector{T}, on::Day) where {T<:Union{Date, DateTime}} ## -@test endpoints(dates, Day(1)) == collect(1:TimeFrames.nrow(tsdaily)) -@test endpoints(dates, Day(2)) == [collect(2:2:TimeFrames.nrow(tsdaily))..., 181] -@test endpoints(datetimes, Day(1)) == collect(1:TimeFrames.nrow(tsdaily)) -@test endpoints(datetimes, Day(2)) == [collect(2:2:TimeFrames.nrow(tsdaily))..., 181] +@test endpoints(dates, Day(1)) == collect(1:TSFrames.nrow(tsdaily)) +@test endpoints(dates, Day(2)) == [collect(2:2:TSFrames.nrow(tsdaily))..., 181] +@test endpoints(datetimes, Day(1)) == collect(1:TSFrames.nrow(tsdaily)) +@test endpoints(datetimes, Day(2)) == [collect(2:2:TSFrames.nrow(tsdaily))..., 181] @test endpoints(datetimehours, Day(1)) == [24, 48, 49] ## @@ -318,13 +318,13 @@ datetimesecondsrandom = sample(MersenneTwister(123), datetimeseconds, 20, replac ## -# endpoints(ts::TimeFrame, on::T) where {T<:Period} +# endpoints(ts::TSFrame, on::T) where {T<:Period} ## @test endpoints(tsdaily, Day(1)) == endpoints(dates, Day(1)) @test endpoints(tsdaily, Month(1)) == endpoints(dates, Month(1)) ## -# endpoints(ts::TimeFrame, on::Symbol, k::Int=1) +# endpoints(ts::TSFrame, on::Symbol, k::Int=1) ## @test_throws MethodError endpoints(tsinteger, :years, 1) # non TimeType index should throw an error @test_throws ArgumentError endpoints(tsdaily, :abc, 1) @@ -345,6 +345,6 @@ datetimesecondsrandom = sample(MersenneTwister(123), datetimeseconds, 20, replac @test endpoints(tsmicroseconds, :microseconds, 2) == endpoints(index(tsmicroseconds), Microsecond(2)) @test endpoints(tsnanoseconds, :nanoseconds, 2) == endpoints(index(tsnanoseconds), Nanosecond(2)) ## -# endpoints(ts::TimeFrame, on::String, k::Int=1) +# endpoints(ts::TSFrame, on::String, k::Int=1) ## @test endpoints(tsdaily, "days", 1) == endpoints(tsdaily, Day(1)) diff --git a/test/getindex.jl b/test/getindex.jl index db93b6d..1f721a0 100644 --- a/test/getindex.jl +++ b/test/getindex.jl @@ -1,9 +1,9 @@ -ts = TimeFrame(df_timetype_index) -ts_long = TimeFrame(df_timetype_index_long_columns) +ts = TSFrame(df_timetype_index) +ts_long = TSFrame(df_timetype_index_long_columns) datetimes = collect(DateTime(2007, 1, 2):Hour(1):DateTime(2007, 1, 10)); -tsdatetimes = TimeFrame(random(length(datetimes)), datetimes); +tsdatetimes = TSFrame(random(length(datetimes)), datetimes); -function test_types(obj::TimeFrame) +function test_types(obj::TSFrame) @test typeof(obj.coredata) == DataFrame end @@ -15,31 +15,31 @@ t = ts[i, j] @test typeof(t) == eltype(df_timetype_index[:, j+1]) @test t == df_timetype_index[i, j+1] -# getindex(ts::TimeFrame, i::Int, j::Symbol) +# getindex(ts::TSFrame, i::Int, j::Symbol) i = 1; j = :data t = ts[i, j] @test typeof(t) == typeof(df_timetype_index[i, j]) @test t == df_timetype_index[i, j] -# getindex(ts::TimeFrame, i::Int, j::String) +# getindex(ts::TSFrame, i::Int, j::String) i = 1; j = "data" t = ts[i, j] @test typeof(t) == typeof(df_timetype_index[i, j]) @test t == df_timetype_index[i, j] -# getindex(ts::TimeFrame, dt::T, j::Int) where {T<:TimeType} +# getindex(ts::TSFrame, dt::T, j::Int) where {T<:TimeType} dt = ts.coredata[:, :Index][1]; j = 1; t = ts[dt ,j] @test typeof(t) == typeof(df_timetype_index[1, j+1]) @test t == df_timetype_index[1, j+1] -# getindex(ts::TimeFrame, dt::T, j::Symbol) where {T<:TimeType} +# getindex(ts::TSFrame, dt::T, j::Symbol) where {T<:TimeType} dt = ts.coredata[:, :Index][1]; j = :data; t = ts[dt,j] @test typeof(t) == typeof(df_timetype_index[1, j]) @test t == df_timetype_index[1, j] -# getindex(ts::TimeFrame, dt::T, j::String) where {T<:TimeType} +# getindex(ts::TSFrame, dt::T, j::String) where {T<:TimeType} dt = ts.coredata[:, :Index][1]; j = "data"; t = ts[dt,j] @test typeof(t) == typeof(df_timetype_index[1, j]) @@ -47,7 +47,7 @@ t = ts[dt,j] ### Row Scalar, Column Vector -# getindex(ts::TimeFrame, i::Int, j::AbstractVector{Int}) +# getindex(ts::TSFrame, i::Int, j::AbstractVector{Int}) i = 1; n = 10; j = collect(1:n) t = ts_long[i, j] test_types(t) @@ -68,7 +68,7 @@ t = ts_long[i, j] test_types(t) @test t.coredata == DataFrame(df_timetype_index_long_columns[i, [1,2,101]]) -# getindex(ts::TimeFrame, i::Int, j::AbstractVector{T}) where {T<:Union{String, Symbol}} +# getindex(ts::TSFrame, i::Int, j::AbstractVector{T}) where {T<:Union{String, Symbol}} i = 1; n = 10; j = ["data$x" for x in 1:n] t = ts_long[i, j] test_types(t) @@ -89,7 +89,7 @@ t = ts_long[i, j] test_types(t) @test t.coredata == DataFrame(df_timetype_index_long_columns[i, Cols(:Index, j)]) -# getindex(ts::TimeFrame, dt::T, j::AbstractVector{Int}) where {T<:TimeType} +# getindex(ts::TSFrame, dt::T, j::AbstractVector{Int}) where {T<:TimeType} i = 1; dt = ts.coredata[:, :Index][i]; n = 10; j = collect(1:n) t = ts_long[dt, j] test_types(t) @@ -110,7 +110,7 @@ t = ts_long[dt, j] test_types(t) @test t.coredata == DataFrame(df_timetype_index_long_columns[i, [1,2,101]]) -# getindex(ts::TimeFrame, dt::D, j::AbstractVector{T}) where {D<:TimeType, T<:Union{String, Symbol}} +# getindex(ts::TSFrame, dt::D, j::AbstractVector{T}) where {D<:TimeType, T<:Union{String, Symbol}} i = 1; dt = ts.coredata[:, :Index][i]; n = 10; j = ["data$x" for x in 1:n] t = ts_long[dt, j] test_types(t) @@ -134,7 +134,7 @@ test_types(t) # Row Scalar, Column Unitrange -# getindex(ts::TimeFrame, i::Int, j::UnitRange) +# getindex(ts::TSFrame, i::Int, j::UnitRange) i = 1; n = 10; j = 1:n t = ts_long[i, j] test_types(t) @@ -153,7 +153,7 @@ test_types(t) # Row Vector, Column Scalar -# getindex(ts::TimeFrame, i::AbstractVector{Int}, j::Int) +# getindex(ts::TSFrame, i::AbstractVector{Int}, j::Int) n = 10; i = collect(1:n); j = 1 t = ts[i, j] @test typeof(t) == typeof(df_timetype_index[i, j+1]) @@ -174,7 +174,7 @@ t = ts[i, j] @test typeof(t) == typeof(df_timetype_index[i, j+1]) @test t == df_timetype_index[i, j+1] -# getindex(ts::TimeFrame, i::AbstractVector{Int}, j::Symbol) +# getindex(ts::TSFrame, i::AbstractVector{Int}, j::Symbol) n = 10; i = collect(1:n); j = :data t = ts[i, j] @test typeof(t) == typeof(df_timetype_index[i, :data]) @@ -195,7 +195,7 @@ t = ts[i, j] @test typeof(t) == typeof(df_timetype_index[i, :data]) @test t == df_timetype_index[i, :data] -# getindex(ts::TimeFrame, i::AbstractVector{Int}, j::String) +# getindex(ts::TSFrame, i::AbstractVector{Int}, j::String) n = 10; i = collect(1:n); j = "data" t = ts[i, j] @test typeof(t) == typeof(df_timetype_index[i, "data"]) @@ -216,17 +216,17 @@ t = ts[i, j] @test typeof(t) == typeof(df_timetype_index[i, "data"]) @test t == df_timetype_index[i, "data"] -# getindex(ts::TimeFrame, dt::AbstractVector{T}, j::Int) where {T<:TimeType} +# getindex(ts::TSFrame, dt::AbstractVector{T}, j::Int) where {T<:TimeType} dt = Date(2007, 1, 1):Day(1):Date(2007, 1, 15) t = ts_long[dt, 10] @test typeof(t) <: Vector @test t == data_array_long[:, 10][1:15] -# getindex(ts::TimeFrame, dt::AbstractVector{T}, j::Union{String, Symbol}) where {T<:TimeType} +# getindex(ts::TSFrame, dt::AbstractVector{T}, j::Union{String, Symbol}) where {T<:TimeType} # Row Vector, Column Vector -# getindex(ts::TimeFrame, i::AbstractVector{Int}, j::AbstractVector{Int}) +# getindex(ts::TSFrame, i::AbstractVector{Int}, j::AbstractVector{Int}) n = 10; m = 10; i = collect(1:n); j = collect(1:m) t = ts_long[i, j] @test typeof(t.coredata) == typeof(df_timetype_index_long_columns[i, collect(1:m+1)]) @@ -247,7 +247,7 @@ t = ts_long[i, j] @test typeof(t.coredata) == typeof(df_timetype_index_long_columns[i, [(2*x+1) for x in 0:50]]) @test t.coredata == df_timetype_index_long_columns[i, [(2*x+1) for x in 0:50]] -# getindex(ts::TimeFrame, i::AbstractVector{Int}, j::AbstractVector{T}) where {T<:Union{String, Symbol}} +# getindex(ts::TSFrame, i::AbstractVector{Int}, j::AbstractVector{T}) where {T<:Union{String, Symbol}} n = 10; m = 10; i = collect(1:n); j = insert!(["data$x" for x in 1:m], 1, "Index") t = ts_long[i, j] @test typeof(t.coredata) == typeof(df_timetype_index_long_columns[i, j]) @@ -268,7 +268,7 @@ t = ts_long[i, j] @test typeof(t.coredata) == typeof(df_timetype_index_long_columns[i, j]) @test t.coredata == df_timetype_index_long_columns[i, j] -# getindex(ts::TimeFrame, dt::AbstractVector{D}, j::AbstractVector{T}) where {D<:TimeType, T<:Union{String, Symbol}} +# getindex(ts::TSFrame, dt::AbstractVector{D}, j::AbstractVector{T}) where {D<:TimeType, T<:Union{String, Symbol}} dt = Date(2007, 1, 1):Day(1):Date(2007, 1, 15); j = 1:5 t = ts_long[dt, j] test_types(t) @@ -296,24 +296,24 @@ test_types(ts[ind]) # getindex(ts, d::Date) ind = Date(2007, 2, 1) test_types(ts[ind]) -@test TimeFrames.index(ts[ind]) == [ind] +@test TSFrames.index(ts[ind]) == [ind] # getindex(ts, y::Year) ind = Year(2007) test_types(ts[ind]) -@test unique(Dates.year.(TimeFrames.index(ts[ind]))) == [2007] +@test unique(Dates.year.(TSFrames.index(ts[ind]))) == [2007] # getindex(ts, y::Year, m::Month) y = Year(2007) m = Month(3) test_types(ts[y, m]) -@test unique(Dates.yearmonth.(TimeFrames.index(ts[y, m]))) == [(2007, 3)] +@test unique(Dates.yearmonth.(TSFrames.index(ts[y, m]))) == [(2007, 3)] # getindex(ts, i::String) ind = "2007-10-01" d = Date(2007, 10, 1) test_types(ts[ind]) -@test TimeFrames.index(ts[ind]) == [d] +@test TSFrames.index(ts[ind]) == [d] # getindex(ts, i::Int, j::Int) i = 1; j = 1 @@ -328,7 +328,7 @@ t = ts[i, j] @test typeof(t) <: Vector @test t == data_vector[i] -# getindex(ts::TimeFrame, i::Int, j::UnitRange) +# getindex(ts::TSFrame, i::Int, j::UnitRange) i = 2; j = 1:1 t = ts[i, j] test_types(t) @@ -337,37 +337,37 @@ test_types(t) @test t.coredata[!, :Index] == [df_timetype_index[i, :Index]] @test t.coredata[!, :data] == df_timetype_index[[i], :data] -# getindex(ts::TimeFrame, i::Int, j::Symbol) +# getindex(ts::TSFrame, i::Int, j::Symbol) i = 1; j = :data t = ts[i, [j]] test_types(t) @test t.coredata[!, j] == [df_timetype_index[i, j]] -# getindex(ts::TimeFrame, i::UnitRange, j::Symbol) +# getindex(ts::TSFrame, i::UnitRange, j::Symbol) i = 1:10; j = :data t = ts[i, [j]] test_types(t) @test t.coredata[!, j] == df_timetype_index[i, j] -# getindex(ts::TimeFrame, i::Int, j::String) +# getindex(ts::TSFrame, i::Int, j::String) i = 1; j = "data" t = ts[i, [j]] test_types(t) @test t.coredata[!, j] == [df_timetype_index[i, j]] -# getindex(ts::TimeFrame, i::UnitRange, j::String) +# getindex(ts::TSFrame, i::UnitRange, j::String) i = 1:10; j = "data" t = ts[i, [j]] test_types(t) @test t.coredata[!, j] == df_timetype_index[i, j] -# getindex(ts::TimeFrame, r::StepRange{T, V}) where {T<:TimeType, V<:Period} +# getindex(ts::TSFrame, r::StepRange{T, V}) where {T<:TimeType, V<:Period} ind = Date(2007, 1, 1):Day(1):Date(2007, 2, 1) t = ts[ind] test_types(ts[ind]) @test t.coredata[!, :Index] == collect(ind) -# getindex(ts::TimeFrame, y::Year, m::Month, w::Week) +# getindex(ts::TSFrame, y::Year, m::Month, w::Week) @test index(getindex(ts, Year(2007), Month(1), Week(6))) == [] @test index(getindex(ts, Year(2007), Month(1), Week(0))) == [] @test index(getindex(ts, Year(2007), Month(1), Week(-1))) == [] @@ -375,26 +375,26 @@ test_types(ts[ind]) Date(2007, 01, 08), Date(2007, 01, 09), Date(2007, 01, 10), Date(2007, 01, 11), Date(2007, 01, 12), Date(2007, 01, 13), Date(2007, 01, 14) ] -# getindex(ts::TimeFrame, y::Year, m::Month, d::Day, h::Hour) +# getindex(ts::TSFrame, y::Year, m::Month, d::Day, h::Hour) @test index(getindex(tsdatetimes, Year(2007), Month(1), Day(2), Hour(25))) == [] @test index(getindex(tsdatetimes, Year(2007), Month(1), Day(2), Hour(-1))) == [] @test index(getindex(tsdatetimes, Year(2007), Month(1), Day(2), Hour(3))) == [DateTime(2007, 01, 02, 03, 00, 00)] -# getindex(ts::TimeFrame, y::Year, m::Month, d::Day, h::Hour, min::Minute) +# getindex(ts::TSFrame, y::Year, m::Month, d::Day, h::Hour, min::Minute) @test index(getindex(tsdatetimes, Year(2007), Month(1), Day(2), Hour(1), Minute(-1))) == [] @test index(getindex(tsdatetimes, Year(2007), Month(1), Day(2), Hour(-1), Minute(0))) == [] @test index(getindex(tsdatetimes, Year(2007), Month(1), Day(2), Hour(-1), Minute(61))) == [] @test index(getindex(tsdatetimes, Year(2007), Month(1), Day(2), Hour(0), Minute(0))) == [DateTime(2007, 01, 02, 0, 0, 0)] -# getindex(ts::TimeFrame, y::Year, m::Month, d::Day, h::Hour, min::Minute, sec::Second) +# getindex(ts::TSFrame, y::Year, m::Month, d::Day, h::Hour, min::Minute, sec::Second) @test index(getindex(tsdatetimes, Year(2007), Month(1), Day(2), Hour(1), Minute(0), Second(-1))) == [] @test index(getindex(tsdatetimes, Year(2007), Month(1), Day(2), Hour(-1), Minute(0), Second(61))) == [] @test index(getindex(tsdatetimes, Year(2007), Month(1), Day(2), Hour(0), Minute(0), Second(0))) == [DateTime(2007, 01, 02, 0, 0, 0)] -# getindex(ts::TimeFrame, y::Year, m::Month, d::Day, h::Hour, min::Minute, sec::Second, ms::Millisecond) +# getindex(ts::TSFrame, y::Year, m::Month, d::Day, h::Hour, min::Minute, sec::Second, ms::Millisecond) @test index(getindex(tsdatetimes, Year(2007), Month(1), Day(2), Hour(1), Minute(0), Second(0), Millisecond(-1))) == [] @test index(getindex(tsdatetimes, Year(2007), Month(1), Day(2), @@ -402,7 +402,7 @@ test_types(ts[ind]) @test index(getindex(tsdatetimes, Year(2007), Month(1), Day(2), Hour(0), Minute(0), Second(0), Millisecond(0))) == [DateTime(2007, 01, 02, 0, 0, 0)] -# getindex(ts::TimeFrame, y::Year, q::Quarter) +# getindex(ts::TSFrame, y::Year, q::Quarter) y = Year(2007); q = Quarter(2) dates = Date(2007, 4, 1):Day(1):Date(2007, 6, 30) start_index = (Date(2007, 4, 1) - Date(2007, 1, 1)).value + 1 @@ -424,9 +424,9 @@ test_types(t) y = Year(2007); q = Quarter(5) t = ts[y, q] test_types(t) -@test TimeFrames.nrow(t) == 0 +@test TSFrames.nrow(t) == 0 -# getindex(ts::TimeFrame, y::Year, m::Month, d::Day) +# getindex(ts::TSFrame, y::Year, m::Month, d::Day) y = Year(2007); m = Month(1); d = Day(1) t = ts[y, m, d] test_types(t) @@ -443,9 +443,9 @@ test_types(t) y = Year(2007); m = Month(13); d = Day(1) t = ts[y, m, d] test_types(t) -@test TimeFrames.nrow(t) == 0 +@test TSFrames.nrow(t) == 0 -# getindex(ts::TimeFrame, ::Colon, j::Int) +# getindex(ts::TSFrame, ::Colon, j::Int) j = 10 t = ts_long[:, j] @test typeof(t) <: Vector diff --git a/test/getproperty.jl b/test/getproperty.jl index cc7eb8f..d266148 100644 --- a/test/getproperty.jl +++ b/test/getproperty.jl @@ -1,2 +1,2 @@ -ts = TimeFrame(df_integer_index) +ts = TSFrame(df_integer_index) @test ts.data == ts[:, :data] diff --git a/test/index.jl b/test/index.jl index 90fbb37..2cc2e44 100644 --- a/test/index.jl +++ b/test/index.jl @@ -1,3 +1,3 @@ -ts = TimeFrame(df_timetype_index) +ts = TSFrame(df_timetype_index) @test index(ts) == df_timetype_index[!, :Index] diff --git a/test/join.jl b/test/join.jl index bba0b34..3b0956a 100644 --- a/test/join.jl +++ b/test/join.jl @@ -1,5 +1,5 @@ -ts1 = TimeFrame(rand(DATA_SIZE), index_timetype) -ts2 = TimeFrame(rand(Int(floor(DATA_SIZE/2))), index_timetype[1:Int(floor(DATA_SIZE/2))]) +ts1 = TSFrame(rand(DATA_SIZE), index_timetype) +ts2 = TSFrame(rand(Int(floor(DATA_SIZE/2))), index_timetype[1:Int(floor(DATA_SIZE/2))]) # testing JoinInner/JoinBoth ts_innerjoin = join(ts1, ts2, JoinInner) diff --git a/test/lag.jl b/test/lag.jl index d76a9eb..68d6fcc 100644 --- a/test/lag.jl +++ b/test/lag.jl @@ -1,4 +1,4 @@ -ts = TimeFrame(integer_data_vector, index_timetype) +ts = TSFrame(integer_data_vector, index_timetype) # lagging by something atmost DATA_SIZE for lagby in [0, 1, Int(floor(DATA_SIZE/2)), DATA_SIZE] @@ -22,5 +22,5 @@ lagged_ts = lag(ts, DATA_SIZE + 1) # lagging by a negative integer lagged_ts = lag(ts, -1) @test isequal(lagged_ts[:, :Index], ts[:, :Index]) -@test isequal(lagged_ts[TimeFrames.nrow(ts), :x1], missing) -@test isequal(lagged_ts[1:TimeFrames.nrow(ts) - 1, :x1], ts[2:TimeFrames.nrow(ts), :x1]) +@test isequal(lagged_ts[TSFrames.nrow(ts), :x1], missing) +@test isequal(lagged_ts[1:TSFrames.nrow(ts) - 1, :x1], ts[2:TSFrames.nrow(ts), :x1]) diff --git a/test/lead.jl b/test/lead.jl index f287f71..2089f98 100644 --- a/test/lead.jl +++ b/test/lead.jl @@ -1,4 +1,4 @@ -ts = TimeFrame(integer_data_vector, index_timetype) +ts = TSFrame(integer_data_vector, index_timetype) # when leading by somethng non-negative and atmost DATA_SIZE for leadby in [0, 1, Int(floor(DATA_SIZE/2)), DATA_SIZE] @@ -8,19 +8,19 @@ for leadby in [0, 1, Int(floor(DATA_SIZE/2)), DATA_SIZE] @test isequal(ts_lead[:, :Index], ts[:, :Index]) # The last lead values must be missing - @test isequal(Vector{Missing}(ts_lead[TimeFrames.nrow(ts) - (leadby - 1):TimeFrames.nrow(ts), :x1]), fill(missing, leadby)) + @test isequal(Vector{Missing}(ts_lead[TSFrames.nrow(ts) - (leadby - 1):TSFrames.nrow(ts), :x1]), fill(missing, leadby)) # The rest of the values must be shifted - @test isequal(ts_lead[1:TimeFrames.nrow(ts) - leadby, :x1], ts[leadby + 1:TimeFrames.nrow(ts), :x1]) + @test isequal(ts_lead[1:TSFrames.nrow(ts) - leadby, :x1], ts[leadby + 1:TSFrames.nrow(ts), :x1]) end # when leading by something greater than DATA_SIZE ts_lead = lead(ts, DATA_SIZE + 1) @test isequal(ts_lead[:, :Index], ts[:, :Index]) -@test isequal(Vector{Missing}(ts_lead[1:TimeFrames.nrow(ts), :x1]), fill(missing, DATA_SIZE)) +@test isequal(Vector{Missing}(ts_lead[1:TSFrames.nrow(ts), :x1]), fill(missing, DATA_SIZE)) # when leading by something negative ts_lead = lead(ts, -1) @test isequal(ts_lead[:, :Index], ts[:, :Index]) @test isequal(ts_lead[1, :x1], missing) -@test isequal(ts_lead[2:TimeFrames.nrow(ts), :x1], ts[1:TimeFrames.nrow(ts) - 1, :x1]) \ No newline at end of file +@test isequal(ts_lead[2:TSFrames.nrow(ts), :x1], ts[1:TSFrames.nrow(ts) - 1, :x1]) \ No newline at end of file diff --git a/test/matrix.jl b/test/matrix.jl index b537f99..1a8b4c7 100644 --- a/test/matrix.jl +++ b/test/matrix.jl @@ -1,4 +1,4 @@ -ts = TimeFrame([random(100) random(100)]) +ts = TSFrame([random(100) random(100)]) matrix = Matrix(ts) @test isequal(matrix[:, 1], ts[:, :x1]) diff --git a/test/pctchange.jl b/test/pctchange.jl index 7dad38c..0a95ad1 100644 --- a/test/pctchange.jl +++ b/test/pctchange.jl @@ -1,4 +1,4 @@ -ts = TimeFrame(integer_data_vector, index_timetype) +ts = TSFrame(integer_data_vector, index_timetype) @test_throws ArgumentError pctchange(ts, 0) @test_throws ArgumentError pctchange(ts, -1) @@ -14,8 +14,8 @@ for periods in [1, Int(floor(DATA_SIZE/2))] @test isequal(Vector{Missing}(pctchange_ts[1:periods, :x1]), fill(missing, periods)) # other elements are pct changes - pctchange_output = pctchange_ts[(periods + 1):TimeFrames.nrow(ts), :x1] - correct_output = (ts[periods + 1:TimeFrames.nrow(ts), :x1] - ts[1:TimeFrames.nrow(ts) - periods, :x1]) ./ abs.(ts[1:TimeFrames.nrow(ts) - periods, :x1]) + pctchange_output = pctchange_ts[(periods + 1):TSFrames.nrow(ts), :x1] + correct_output = (ts[periods + 1:TSFrames.nrow(ts), :x1] - ts[1:TSFrames.nrow(ts) - periods, :x1]) ./ abs.(ts[1:TSFrames.nrow(ts) - periods, :x1]) @test floor.(pctchange_output .* 100) == floor.(correct_output .* 100) end diff --git a/test/rollapply.jl b/test/rollapply.jl index fec4ed9..2f86d75 100644 --- a/test/rollapply.jl +++ b/test/rollapply.jl @@ -3,33 +3,33 @@ index_timetype = Date(2000, 1,1) + Day.(0:(DATA_SIZE - 1)) vec1 = collect(1:DATA_SIZE) vec2 = collect(1:DATA_SIZE) vec3 = collect(1:DATA_SIZE) -ts = TimeFrame([vec1 vec2 vec3], index_timetype, colnames=[:A, :B, :C]) +ts = TSFrame([vec1 vec2 vec3], index_timetype, colnames=[:A, :B, :C]) windowsize = 0 @test_throws ArgumentError rollapply(first, ts, :A, windowsize) windowsize = 1 rp = rollapply(first, ts, :A, windowsize) -@test typeof(rp) == TimeFrames.TimeFrame # test type +@test typeof(rp) == TSFrames.TSFrame # test type @test occursin("A", names(rp)[1]) # test colname -@test TimeFrames.nrow(rp) == TimeFrames.nrow(ts) - windowsize + 1 # nrow +@test TSFrames.nrow(rp) == TSFrames.nrow(ts) - windowsize + 1 # nrow @test rp[1, 1] == ts[1, :A] # shift @test rp[2, 1] == ts[2, :A] # value windowsize = 5 rp = rollapply(sum, ts, :A, windowsize) -@test typeof(rp) == TimeFrames.TimeFrame +@test typeof(rp) == TSFrames.TSFrame @test first(index(rp)) == index(ts)[windowsize] -@test TimeFrames.nrow(rp) == TimeFrames.nrow(ts) - windowsize + 1 +@test TSFrames.nrow(rp) == TSFrames.nrow(ts) - windowsize + 1 @test rp[1, 1] == sum(ts[1:windowsize, :A]) @test occursin("A", names(rp)[1]) windowsize = DATA_SIZE -@test typeof(rp) == TimeFrames.TimeFrame +@test typeof(rp) == TSFrames.TSFrame rp = rollapply(sum, ts, :A, windowsize) @test first(index(rp)) == index(ts)[windowsize] -@test TimeFrames.nrow(rp) == 1 +@test TSFrames.nrow(rp) == 1 @test rp[1, 1] == sum(ts[1:windowsize, :A]) @test occursin("A", names(rp)[1]) diff --git a/test/runtests.jl b/test/runtests.jl index 168ae37..902638f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -5,12 +5,12 @@ using StatsBase using Statistics using Test using Tables -using TimeFrames +using TSFrames include("dataobjects.jl") -@testset "TimeFrame()" begin - include("TimeFrame.jl") +@testset "TSFrame()" begin + include("TSFrame.jl") end @testset "getindex()" begin diff --git a/test/subset.jl b/test/subset.jl index 12e936b..a29d119 100644 --- a/test/subset.jl +++ b/test/subset.jl @@ -5,75 +5,75 @@ MIDDATE = Date(2008, 1, 1) LASTDATE = Date(2008, 2, 4) # testing for integer index -ts_integer = TimeFrame(rand(DATA_SIZE), index_integer) +ts_integer = TSFrame(rand(DATA_SIZE), index_integer) ## subsetting from -1 to 0 -ts_subset = TimeFrames.subset(ts_integer, -1, 0) -@test TimeFrames.nrow(TimeFrames.subset(ts_integer, -1, 0)) == 0 -@test TimeFrames.nrow(TimeFrames.subset(ts_integer, :, 0)) == 0 +ts_subset = TSFrames.subset(ts_integer, -1, 0) +@test TSFrames.nrow(TSFrames.subset(ts_integer, -1, 0)) == 0 +@test TSFrames.nrow(TSFrames.subset(ts_integer, :, 0)) == 0 ## subsetting from -1 to MIDPOINT -ts_subset = TimeFrames.subset(ts_integer, -1, MIDPOINT) -@test TimeFrames.nrow(ts_subset) == MIDPOINT +ts_subset = TSFrames.subset(ts_integer, -1, MIDPOINT) +@test TSFrames.nrow(ts_subset) == MIDPOINT @test ts_subset[:, :Index] == 1:MIDPOINT @test ts_subset[:, :x1] == ts_integer[1:MIDPOINT, :x1] -@test TimeFrames.subset(ts_integer, :, MIDPOINT)[:, :Index] == ts_subset[:, :Index] -@test TimeFrames.subset(ts_integer, :, MIDPOINT)[:, :x1] == ts_subset[:, :x1] +@test TSFrames.subset(ts_integer, :, MIDPOINT)[:, :Index] == ts_subset[:, :Index] +@test TSFrames.subset(ts_integer, :, MIDPOINT)[:, :x1] == ts_subset[:, :x1] ## subsetting from MIDPOINT to DATA_SIZE -ts_subset = TimeFrames.subset(ts_integer, MIDPOINT, DATA_SIZE) -@test TimeFrames.nrow(ts_subset) == DATA_SIZE - MIDPOINT + 1 +ts_subset = TSFrames.subset(ts_integer, MIDPOINT, DATA_SIZE) +@test TSFrames.nrow(ts_subset) == DATA_SIZE - MIDPOINT + 1 @test ts_subset[:, :Index] == MIDPOINT:DATA_SIZE @test ts_subset[:, :x1] == ts_integer[MIDPOINT:DATA_SIZE, :x1] -@test TimeFrames.subset(ts_integer, MIDPOINT, :)[:, :Index] == ts_subset[:, :Index] -@test TimeFrames.subset(ts_integer, MIDPOINT, :)[:, :x1] == ts_subset[:, :x1] +@test TSFrames.subset(ts_integer, MIDPOINT, :)[:, :Index] == ts_subset[:, :Index] +@test TSFrames.subset(ts_integer, MIDPOINT, :)[:, :x1] == ts_subset[:, :x1] ## subsetting from MIDPOINT to DATA_SIZE + 1 -ts_subset = TimeFrames.subset(ts_integer, MIDPOINT, DATA_SIZE + 1) -@test TimeFrames.nrow(ts_subset) == DATA_SIZE - MIDPOINT + 1 +ts_subset = TSFrames.subset(ts_integer, MIDPOINT, DATA_SIZE + 1) +@test TSFrames.nrow(ts_subset) == DATA_SIZE - MIDPOINT + 1 @test ts_subset[:, :Index] == MIDPOINT:DATA_SIZE @test ts_subset[:, :x1] == ts_integer[MIDPOINT:DATA_SIZE, :x1] -@test TimeFrames.subset(ts_integer, MIDPOINT, :)[:, :Index] == ts_subset[:, :Index] -@test TimeFrames.subset(ts_integer, MIDPOINT, :)[:, :x1] == ts_subset[:, :x1] +@test TSFrames.subset(ts_integer, MIDPOINT, :)[:, :Index] == ts_subset[:, :Index] +@test TSFrames.subset(ts_integer, MIDPOINT, :)[:, :x1] == ts_subset[:, :x1] ## subsetting from DATA_SIZE + 1 to DATA_SIZE + 5 -ts_subset = TimeFrames.subset(ts_integer, DATA_SIZE + 1, DATA_SIZE + 5) -@test TimeFrames.nrow(ts_subset) == 0 -@test TimeFrames.nrow(TimeFrames.subset(ts_integer, DATA_SIZE + 1, :)) == 0 +ts_subset = TSFrames.subset(ts_integer, DATA_SIZE + 1, DATA_SIZE + 5) +@test TSFrames.nrow(ts_subset) == 0 +@test TSFrames.nrow(TSFrames.subset(ts_integer, DATA_SIZE + 1, :)) == 0 # testing for time index -ts_timetype = TimeFrame(rand(DATA_SIZE), index_timetype) +ts_timetype = TSFrame(rand(DATA_SIZE), index_timetype) ## subsetting from 2006-12-30 to 2006-12-31 -@test TimeFrames.nrow(TimeFrames.subset(ts_timetype, Date(2006, 12, 30), Date(2006, 12, 31))) == 0 -@test TimeFrames.nrow(TimeFrames.subset(ts_timetype, :, Date(2006, 12, 31))) == 0 +@test TSFrames.nrow(TSFrames.subset(ts_timetype, Date(2006, 12, 30), Date(2006, 12, 31))) == 0 +@test TSFrames.nrow(TSFrames.subset(ts_timetype, :, Date(2006, 12, 31))) == 0 ## subsetting from 2006-12-31 to MIDDATE -ts_subset = TimeFrames.subset(ts_timetype, Date(2006, 12, 31), MIDDATE) -@test TimeFrames.nrow(ts_subset) == length(FIRSTDATE:Day(1):MIDDATE) +ts_subset = TSFrames.subset(ts_timetype, Date(2006, 12, 31), MIDDATE) +@test TSFrames.nrow(ts_subset) == length(FIRSTDATE:Day(1):MIDDATE) @test ts_subset[:, :Index] == FIRSTDATE:Day(1):MIDDATE @test ts_subset[:, :x1] == ts_timetype[FIRSTDATE:Day(1):MIDDATE, :x1] -@test TimeFrames.subset(ts_timetype, :, MIDDATE)[:, :Index] == ts_subset[:, :Index] -@test TimeFrames.subset(ts_timetype, :, MIDDATE)[:, :x1] == ts_subset[:, :x1] +@test TSFrames.subset(ts_timetype, :, MIDDATE)[:, :Index] == ts_subset[:, :Index] +@test TSFrames.subset(ts_timetype, :, MIDDATE)[:, :x1] == ts_subset[:, :x1] ## subsetting from MIDDATE to LASTDATE -ts_subset = TimeFrames.subset(ts_timetype, MIDDATE, LASTDATE) -@test TimeFrames.nrow(ts_subset) == length(MIDDATE:Day(1):LASTDATE) +ts_subset = TSFrames.subset(ts_timetype, MIDDATE, LASTDATE) +@test TSFrames.nrow(ts_subset) == length(MIDDATE:Day(1):LASTDATE) @test ts_subset[:, :Index] == ts_timetype[MIDDATE:Day(1):LASTDATE, :Index] @test ts_subset[:, :x1] == ts_timetype[MIDDATE:Day(1):LASTDATE, :x1] -@test TimeFrames.subset(ts_timetype, MIDDATE, :)[:, :Index] == ts_subset[:, :Index] -@test TimeFrames.subset(ts_timetype, MIDDATE, :)[:, :x1] == ts_subset[:, :x1] +@test TSFrames.subset(ts_timetype, MIDDATE, :)[:, :Index] == ts_subset[:, :Index] +@test TSFrames.subset(ts_timetype, MIDDATE, :)[:, :x1] == ts_subset[:, :x1] ## subsetting from MIDDATE to LASTDATE + 5 -ts_subset = TimeFrames.subset(ts_timetype, MIDDATE, LASTDATE + Day(5)) -@test TimeFrames.nrow(ts_subset) == length(MIDDATE:Day(1):LASTDATE) +ts_subset = TSFrames.subset(ts_timetype, MIDDATE, LASTDATE + Day(5)) +@test TSFrames.nrow(ts_subset) == length(MIDDATE:Day(1):LASTDATE) @test ts_subset[:, :Index] == ts_timetype[MIDDATE:Day(1):LASTDATE, :Index] @test ts_subset[:, :x1] == ts_timetype[MIDDATE:Day(1):LASTDATE, :x1] -@test TimeFrames.subset(ts_timetype, MIDDATE, :)[:, :Index] == ts_subset[:, :Index] -@test TimeFrames.subset(ts_timetype, MIDDATE, :)[:, :x1] == ts_subset[:, :x1] +@test TSFrames.subset(ts_timetype, MIDDATE, :)[:, :Index] == ts_subset[:, :Index] +@test TSFrames.subset(ts_timetype, MIDDATE, :)[:, :x1] == ts_subset[:, :x1] ## subsetting from LASTDATE + 1 to LASTDATE + 5 -ts_subset = TimeFrames.subset(ts_timetype, LASTDATE + Day(1), LASTDATE + Day(5)) -@test TimeFrames.nrow(ts_subset) == 0 -@test TimeFrames.nrow(TimeFrames.subset(ts_timetype, LASTDATE + Day(1), :)) == 0 \ No newline at end of file +ts_subset = TSFrames.subset(ts_timetype, LASTDATE + Day(1), LASTDATE + Day(5)) +@test TSFrames.nrow(ts_subset) == 0 +@test TSFrames.nrow(TSFrames.subset(ts_timetype, LASTDATE + Day(1), :)) == 0 \ No newline at end of file diff --git a/test/tables.jl b/test/tables.jl index d427c45..7c8f209 100644 --- a/test/tables.jl +++ b/test/tables.jl @@ -4,7 +4,7 @@ MONTH = 1 DAYS = 15 dates = Date(YEAR, MONTH, 1):Day(1):Date(YEAR, MONTH, DAYS) -ts = TimeFrame(1:DAYS, dates) +ts = TSFrame(1:DAYS, dates) @test Tables.istable(ts) @@ -37,4 +37,4 @@ namedtuple = first(Tables.namedtupleiterator(ts)) @test Tables.schema(ts).types == (Date, Int64) # testing Tables.materializer -@test Tables.materializer(ts) == TimeFrame +@test Tables.materializer(ts) == TSFrame diff --git a/test/utils.jl b/test/utils.jl index 7e667d2..b8690b8 100644 --- a/test/utils.jl +++ b/test/utils.jl @@ -1,10 +1,10 @@ -using TimeFrames +using TSFrames -ts = TimeFrame(DataFrame(a=["a", "b", "c"], b=[1,2, missing]), [1, 2, 3]) ; +ts = TSFrame(DataFrame(a=["a", "b", "c"], b=[1,2, missing]), [1, 2, 3]) ; ### # describe() -dd = TimeFrames.describe(ts) ; +dd = TSFrames.describe(ts) ; @test names(dd) == [ "variable", "mean", "min", "median", "max", "nmissing", "eltype" ] ; @test dd[1, :variable] == :Index ; @test dd[1, :min] == 1 ; @@ -20,12 +20,12 @@ dd = TimeFrames.describe(ts) ; @test dd[3, :nmissing] == 1 ; @test dd[3, :eltype] == Union{Missing, Int64} ; -dd = TimeFrames.describe(ts, :mean) ; +dd = TSFrames.describe(ts, :mean) ; @test "mean" in names(dd) ; @test !("median" in names(dd)) ; @test dd[3, :mean] == 1.5 ; -dd = TimeFrames.describe(ts, :mean, cols=:b) ; +dd = TSFrames.describe(ts, :mean, cols=:b) ; @test DataFrames.nrow(dd) == 1 ; @test DataFrames.ncol(dd) == 2 ; @test dd[1, :variable] == :b ; @@ -71,10 +71,10 @@ dd = TimeFrames.describe(ts, :mean, cols=:b) ; ### # _check_consistency() -@test TimeFrames._check_consistency(ts) == true -@test TimeFrames._check_consistency(TimeFrame([:a, :b], [2,1])) == true; +@test TSFrames._check_consistency(ts) == true +@test TSFrames._check_consistency(TSFrame([:a, :b], [2,1])) == true; -ts = TimeFrame(data_vector, 1:length(data_vector)); +ts = TSFrame(data_vector, 1:length(data_vector)); ts.coredata.Index = sample(index_integer, length(data_vector), replace=true); -@test TimeFrames._check_consistency(ts) == false; +@test TSFrames._check_consistency(ts) == false; ### diff --git a/test/vcat.jl b/test/vcat.jl index 0f7b913..7f79345 100644 --- a/test/vcat.jl +++ b/test/vcat.jl @@ -8,9 +8,9 @@ index_timetype2 = Date(2007, 1, 1) + Day(DATA_SIZE_1) + Day.(0:(DATA_SIZE_2 - 1) # testing setequal and orderequal df1 = DataFrame(x1 = random(200), x2 = random(200)) df2 = DataFrame(x2 = random(200), x1 = random(200)) -ts1 = TimeFrame(df1, index_timetype1) -ts2 = TimeFrame(df2, index_timetype2) -ts_setequal = TimeFrames.vcat(ts1, ts2, colmerge=:setequal) +ts1 = TSFrame(df1, index_timetype1) +ts2 = TSFrame(df2, index_timetype2) +ts_setequal = TSFrames.vcat(ts1, ts2, colmerge=:setequal) @test propertynames(ts_setequal.coredata) == [:Index, :x1, :x2] @test ts_setequal[1:DATA_SIZE_1, :Index] == ts1[:, :Index] @@ -21,8 +21,8 @@ ts_setequal = TimeFrames.vcat(ts1, ts2, colmerge=:setequal) @test ts_setequal[DATA_SIZE_1 + 1:DATA_SIZE_1 + DATA_SIZE_2, :x2] == ts2[:, :x2] df2 = DataFrame(x1 = random(200), x2 = random(200)) -ts2 = TimeFrame(df2, index_timetype2) -ts_orderequal = TimeFrames.vcat(ts1, ts2, colmerge=:orderequal) +ts2 = TSFrame(df2, index_timetype2) +ts_orderequal = TSFrames.vcat(ts1, ts2, colmerge=:orderequal) @test propertynames(ts_orderequal.coredata) == [:Index, :x1, :x2] @test ts_orderequal[1:DATA_SIZE_1, :Index] == ts1[:, :Index] @@ -35,10 +35,10 @@ ts_orderequal = TimeFrames.vcat(ts1, ts2, colmerge=:orderequal) # testing union and intersection df1 = DataFrame(x1 = random(200), x2 = random(200)) df2 = DataFrame(x2 = random(200), x3 = random(200)) -ts1 = TimeFrame(df1, index_timetype1) -ts2 = TimeFrame(df2, index_timetype2) +ts1 = TSFrame(df1, index_timetype1) +ts2 = TSFrame(df2, index_timetype2) -ts_intersect = TimeFrames.vcat(ts1, ts2, colmerge=:intersect) +ts_intersect = TSFrames.vcat(ts1, ts2, colmerge=:intersect) @test propertynames(ts_intersect.coredata) == [:Index, :x2] @test ts_intersect[1:DATA_SIZE_1, :Index] == ts1[:, :Index] @@ -46,7 +46,7 @@ ts_intersect = TimeFrames.vcat(ts1, ts2, colmerge=:intersect) @test ts_intersect[1:DATA_SIZE_1, :x2] == ts1[:, :x2] @test ts_intersect[DATA_SIZE_1 + 1:DATA_SIZE_1 + DATA_SIZE_2, :x2] == ts2[:, :x2] -ts_union = TimeFrames.vcat(ts1, ts2, colmerge=:union) +ts_union = TSFrames.vcat(ts1, ts2, colmerge=:union) @test propertynames(ts_union.coredata) == [:Index, :x1, :x2, :x3] @test ts_union[1:DATA_SIZE_1, :Index] == ts1[:, :Index]