-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn if periodicity() called on < 2 observations
It can be confusing when periodicity() throws an error when called with an object that only has one observation, because the root cause of the error is usually not clear to the end user. Throw a warning and return a periodicity object with zero time difference and frequency. Fixes #230.
- Loading branch information
1 parent
16bed0f
commit ba2766e
Showing
2 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
P <- structure( | ||
list(difftime = structure(0, units = "secs", class = "difftime"), | ||
frequency = 0, | ||
start = structure(.POSIXct(1, "UTC"), tclass = c("POSIXct", "POSIXt")), | ||
end = structure(.POSIXct(1, "UTC"), tclass = c("POSIXct", "POSIXt")), | ||
units = "secs", | ||
scale = "seconds", | ||
label = "second"), | ||
class = "periodicity") | ||
|
||
test.periodicity_on_one_observation_warns <- function() { | ||
x <- xts(1, .POSIXct(1, "UTC")) | ||
p <- periodicity(x) | ||
checkIdentical(p, P) | ||
|
||
opt <- options(warn = 2) | ||
on.exit(options(warn = opt$warn)) | ||
|
||
checkException(p <- periodicity(x)) | ||
} | ||
test.periodicity_on_zero_observations_warns <- function() { | ||
x <- xts(, .POSIXct(numeric(0), "UTC")) | ||
p <- periodicity(x) | ||
P$start <- NA | ||
P$end <- NA | ||
checkIdentical(p, P) | ||
|
||
opt <- options(warn = 2) | ||
on.exit(options(warn = opt$warn)) | ||
|
||
checkException(p <- periodicity(x)) | ||
} |