Skip to content

as.IDate() drops names #7252

Closed
Closed

Description

@DavisVaughan

With data.table 1.16.2

library(data.table)

x <- c(a = "2019-01-01")
x
#>            a 
#> "2019-01-01"

as.IDate(x)
#> [1] "2019-01-01"

# data.table:::as.IDate.Date calls this
# as.integer() step loses names
as.integer(as.Date(x))
#> [1] 17897

Created on 2025-08-14 with reprex v2.1.1

Activity

  1. DavisVaughan commented on Aug 14, 2025

    @DavisVaughan
    ContributorAuthor

    Another test case for you is this particularly confusing case - due to c.IDate calling as.IDate()

    c(a = data.table::as.IDate("2019-01-01"))
    #> [1] "2019-01-01"

    Created on 2025-08-14 with reprex v2.1.1

  2. MichaelChirico commented on Aug 14, 2025

    @MichaelChirico
    Member

    Thanks...

    With data.table 1.16.2

    Confirmed on a more recent version too.

    Most importantly, this is not consistent with as.Date():

    as.Date(x)
    #            a 
    # "2019-01-01" 
    
    c(a = as.Date("2019-01-01"))
    #            a 
    # "2019-01-01" 

    I do wish R would make up its mind on whether as.* retains names 🙃

    Do you have a use case in mind to help set priority for a fix?

  3. DavisVaughan commented on Aug 14, 2025

    @DavisVaughan
    ContributorAuthor

    No rush on my end, I'm working on r-lib/vctrs#1961 and I noticed it when adding tests. I've got a little helper for tests that retains names, so no issues for me

  4. DavisVaughan commented on Aug 14, 2025

    @DavisVaughan
    ContributorAuthor

    I typically do take the stance that as.* should retain names because it (always?) retains length, and if you retain the length you can also map the names over.

  5. MichaelChirico commented on Aug 14, 2025

    @MichaelChirico
    Member

    I tend to agree but it's not quite so simple:

    as.integer(matrix(1:2, ncol=2L, dimnames = list('a', c('b', 'c'))))
    # [1] 1 2
  6. DavisVaughan commented on Aug 14, 2025

    @DavisVaughan
    ContributorAuthor

    I guess I have feelings about the shape of that return value too 😛

  7. ben-schwen commented on Aug 18, 2025

    @ben-schwen
    Member

    I do wish R would make up its mind on whether as.* retains names 🙃

    @MichaelChirico something we should bring up in the bug tracker? I understand that its not a pressing issue to the usual user but should be consistent at least.

  8. venom1204 commented on May 26, 2026

    @venom1204
    Contributor

    hi @joshhwuu , @MichaelChirico
    i think one way is to capture the names of the input and restore them setattr(res,''names'',nm), i plan to apply this to as.idate.date,as.idate.numeric,as.idate.POSIXctand theas.itime.numericto ensure teh consistency across bothidateand theitime```.

    if this sounds good i will be happy to proceed with the pr .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions