tidyverts / tsibble

Tidy Temporal Data Frames and Tools
https://tsibble.tidyverts.org
GNU General Public License v3.0
527 stars 49 forks source link

yearmonth without delimiter #264

Closed arnaud-feldmann closed 2 years ago

arnaud-feldmann commented 2 years ago

There is a potential confusion with the usage of the yearmonth function on number strings without a delimiter

yearmonth("202002")
<yearmonth[1]>
[1] "2020 févr."

gives the expected result. But

yearmonth("202012")
<yearmonth[1]>
[1] "2020 févr."

doesn't work.

I think either yearmonth should refuse this format and throwing an error, or stick with a default delimiter-less format (while maybe throwing a warning or a message). The expected behaviour, I think, doesn't involve interpreting a number as a delimiter.

Note that this can be a bit dangerous otherwise, as making some correct calcs on the begining of the year can make you think the package is ok with this format

earowang commented 2 years ago

you're spot on. without delimiter, an error should be expected. Will fix this soon.

earowang commented 2 years ago

actually, users can supply any additional formats in format argument in yearmonth(). I should have documented format argument. I can throw a warning if all numbers without delimiter.

library(tsibble)
#> 
#> Attaching package: 'tsibble'
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, union
yearmonth("202002", format = "%Y%m")
#> <yearmonth[1]>
#> [1] "2020 Feb"
yearmonth("202012", format = "%Y%m")
#> <yearmonth[1]>
#> [1] "2020 Dec"

Created on 2021-10-18 by the reprex package (v2.0.1)

earowang commented 2 years ago

Now:

library(tsibble)
#> 
#> Attaching package: 'tsibble'
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, union
yearmonth(c("202012", "202002"))
#> Warning: `yearmonth()` may yield unexpected results.
#> ℹ Please use arg `format` to supply formats.
#> <yearmonth[2]>
#> [1] "2020 Feb" "2020 Feb"
yearmonth(c("202012", "202002"), format = "%Y%m")
#> <yearmonth[2]>
#> [1] "2020 Dec" "2020 Feb"

Created on 2021-10-18 by the reprex package (v2.0.1)

arnaud-feldmann commented 2 years ago

Thanks :D warning is ok I think @earowang, and documenting the format arg is even better ! I noticed that because somebody "I know" made a silly mistake 😇