Skip to content

Commit

Permalink
BUG error importing quarterly data as ts
Browse files Browse the repository at this point in the history
ts objcects need specific frequency for quarterly data, this is not being handled.
Now it is correct.

* Jan/Feb/Mar refer to the first quarter
* Apr/May/Jun refer to the second quarter
* Jul/Aug/Sep refer to the third quarter
* Oct/Nov/Dec refer to the fourth quarter

Issue #61
  • Loading branch information
wilsonfreitas committed Jan 23, 2024
1 parent 2c52f4f commit f3549d2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
7 changes: 7 additions & 0 deletions R/sgs.R
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,20 @@ sgs_untidy <- function(x, tidydf, as = c("tibble", "xts", "ts")) {
freq <- if (is.null(x$info$frequency)) "D" else x$info$frequency
freq_ <- switch(freq,
"A" = 1,
"Q" = 4,
"M" = 12,
"D" = 366
)
start <- switch(freq,
"A" = {
as.integer(format(df$date[1], "%Y"))
},
"Q" = {
c(
as.integer(format(df$date[1], "%Y")),
as.integer(format(df$date[1], "%m")) %/% 3L + 1L
)
},
"M" = {
c(
as.integer(format(df$date[1], "%Y")),
Expand Down
23 changes: 18 additions & 5 deletions tests/testthat/test-get_series.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,29 @@ test_that("it should get series as ts", {
skip_if_offline()
}

x <- get_series(13521, start_date = "2012-01-01", as = "ts")
expect_equal(frequency(x), 1)
expect_s3_class(x, "ts")
expect_equal(start(x), c(2012, 1))

x <- get_series(28563, start_date = "2012-04-01", as = "ts")
expect_equal(frequency(x), 4)
expect_s3_class(x, "ts")
expect_equal(start(x), c(2012, 2))
x <- get_series(28563, start_date = "2022-02-01", as = "ts")
expect_equal(start(x), c(2022, 1))
x <- get_series(28563, start_date = "2022-05-01", as = "ts")
expect_equal(start(x), c(2022, 2))
x <- get_series(28563, start_date = "2022-08-01", as = "ts")
expect_equal(start(x), c(2022, 3))
x <- get_series(28563, start_date = "2022-11-01", as = "ts")
expect_equal(start(x), c(2022, 4))

x <- get_series(c(IPCA = 433), start_date = "2017-01-01", as = "ts")
expect_equal(frequency(x), 12)
expect_s3_class(x, "ts")
expect_equal(start(x), c(2017, 1))

x <- get_series(27569, start_date = "2012-01-01", as = "ts")
expect_equal(frequency(x), 1)
expect_s3_class(x, "ts")
expect_equal(start(x), c(2012, 1))

x <- get_series(1, last = 10, as = "ts")
expect_equal(frequency(x), 366)
expect_s3_class(x, "ts")
Expand Down

0 comments on commit f3549d2

Please sign in to comment.