Skip to content

[R] month() should allow integer inputs #31153

@asfimport

Description

@asfimport

Conclusion: we will implement this in the R bindings - month will allow integer input: month(int) will return int as long as int is between 1 and 12.

In R, more specifically in {}lubridate{}, month() can be used both to get and set the corresponding component of a date. This means month() accepts integer inputs.

suppressPackageStartupMessages(library(lubridate)) 
month(1:12)
#>  [1]  1  2  3  4  5  6  7  8  9 10 11 12
month(1:13)
#> Error in month.numeric(1:13): Values are not in 1:12

Solving this would allow us to implement bindings such as semester() in a manner closer to {}lubridate{}.

suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(lubridate))

test_df <- tibble(
  month_as_int = c(1:12, NA),
  month_as_char_pad = ifelse(month_as_int < 10, paste0("0", month_as_int), month_as_int),
  dates = as.Date(paste0("2021-", month_as_char_pad, "-15"))
)

test_df %>%
  mutate(
    sem_date = semester(dates),
    sem_month_as_int = semester(month_as_int))
#> # A tibble: 13 × 5
#>    month_as_int month_as_char_pad dates      sem_date sem_month_as_int
#>           <int> <chr>             <date>        <int>            <int>
#>  1            1 01                2021-01-15        1                1
#>  2            2 02                2021-02-15        1                1
#>  3            3 03                2021-03-15        1                1
#>  4            4 04                2021-04-15        1                1
#>  5            5 05                2021-05-15        1                1
#>  6            6 06                2021-06-15        1                1
#>  7            7 07                2021-07-15        2                2
#>  8            8 08                2021-08-15        2                2
#>  9            9 09                2021-09-15        2                2
#> 10           10 10                2021-10-15        2                2
#> 11           11 11                2021-11-15        2                2
#> 12           12 12                2021-12-15        2                2
#> 13           NA <NA>              NA               NA               NA

Currently attempts to use month() with integer inputs errors with:

Function 'month' has no kernel matching input types (array[int32])

Reporter: Dragoș Moldovan-Grünfeld / @dragosmg
Assignee: Dragoș Moldovan-Grünfeld / @dragosmg
Watchers: Rok Mihevc / @rok

Related issues:

PRs and other links:

Note: This issue was originally created as ARROW-15701. Please see the migration documentation for further details.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions