Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unnest errors when data.table's IDate class is combined with integers #1507

Closed
lpiep opened this issue Jul 3, 2023 · 7 comments
Closed

unnest errors when data.table's IDate class is combined with integers #1507

lpiep opened this issue Jul 3, 2023 · 7 comments
Labels
bug an unexpected problem or unintended behavior nesting 🐦 nesting, chopping, and packing

Comments

@lpiep
Copy link

lpiep commented Jul 3, 2023

unnest errors when data.table's IDate class is combined with integers. unnest is capable of handling situations where nested tibbles contain dates and integers, but it fails when it attempts the same thing with data.table's IDate class.

library(tidyr)
library(data.table)

df_idates <- tibble(
  ids= c(1, 2),
  data = list(
    tibble(
      dates = c(as.IDate('2012-01-01'), as.IDate('2013-12-03'), as.IDate('2023-06-03')),
    ),
    tibble(
      numbers = 1:3
    )
  )
)

df_dates <- tibble(
  ids= c(1, 2),
  data = list(
    tibble(
      dates = c(as.Date('2012-01-01'), as.Date('2013-12-03'), as.Date('2023-06-03')),
    ),
    tibble(
      numbers = 1:3
    )
  )
)

unnest(df_idates, 'data')
#> Error in as.Date.numeric(e): 'origin' must be supplied
unnest(df_dates, 'data')
#> # A tibble: 6 × 3
#>     ids dates      numbers
#>   <dbl> <date>       <int>
#> 1     1 2012-01-01      NA
#> 2     1 2013-12-03      NA
#> 3     1 2023-06-03      NA
#> 4     2 NA               1
#> 5     2 NA               2
#> 6     2 NA               3

Created on 2023-11-01 by the reprex package (v2.0.1)

I am using the latest GH version of tidyr.

@hadley
Copy link
Member

hadley commented Nov 1, 2023

Could you please rework your reproducible example to use the reprex package ? That makes it easier to see both the input and the output, formatted in such a way that I can easily re-run in a local session.

@hadley hadley added the reprex needs a minimal reproducible example label Nov 1, 2023
@lpiep
Copy link
Author

lpiep commented Nov 1, 2023

@hadley done. thanks!

@hadley
Copy link
Member

hadley commented Nov 1, 2023

Hmmm, that works for me:

library(tidyr)
library(data.table)

df_idates <- tibble(
  ids= c(1, 2),
  data = list(
    tibble(
      dates = c(as.IDate('2012-01-01'), as.IDate('2013-12-03'), as.IDate('2023-06-03')),
    ),
    tibble(
      numbers = 1:3
    )
  )
)

df_dates <- tibble(
  ids= c(1, 2),
  data = list(
    tibble(
      dates = c(as.Date('2012-01-01'), as.Date('2013-12-03'), as.Date('2023-06-03')),
    ),
    tibble(
      numbers = 1:3
    )
  )
)

unnest(df_idates, 'data')
#> # A tibble: 6 × 3
#>     ids dates      numbers
#>   <dbl> <IDate>      <int>
#> 1     1 2012-01-01      NA
#> 2     1 2013-12-03      NA
#> 3     1 2023-06-03      NA
#> 4     2 1970-01-02       1
#> 5     2 1970-01-03       2
#> 6     2 1970-01-04       3
unnest(df_dates, 'data')
#> # A tibble: 6 × 3
#>     ids dates      numbers
#>   <dbl> <date>       <int>
#> 1     1 2012-01-01      NA
#> 2     1 2013-12-03      NA
#> 3     1 2023-06-03      NA
#> 4     2 NA               1
#> 5     2 NA               2
#> 6     2 NA               3

Created on 2023-11-01 with reprex v2.0.2

@lpiep
Copy link
Author

lpiep commented Nov 1, 2023

Hmm. Those 1970 dates in the IDate result shouldn't be there though, right?

@hadley
Copy link
Member

hadley commented Nov 1, 2023

Oh yeah, that is weird! Slightly more minimal reprex:

library(tidyr)
library(data.table)

df <- tibble(
  ids = c(1, 2),
  date = list(tibble(x = as.Date('2012-01-01')), tibble(y = 1)),
  idate = list(tibble(x = as.IDate('2012-01-01')), tibble(y = 1)),
)
unnest(df, c(date, idate), names_sep = "_")
#> # A tibble: 2 × 5
#>     ids date_x     date_y idate_x    idate_y
#>   <dbl> <date>      <dbl> <IDate>      <dbl>
#> 1     1 2012-01-01     NA 2012-01-01      NA
#> 2     2 NA              1 1970-01-02       1

unnest_wider(df, c(date, idate), names_sep = "_")
#> # A tibble: 2 × 5
#>     ids date_x     date_y idate_x    idate_y
#>   <dbl> <date>      <dbl> <IDate>      <dbl>
#> 1     1 2012-01-01     NA 2012-01-01      NA
#> 2     2 NA              1 NA               1

Created on 2023-11-01 with reprex v2.0.2

Interesting that unnest_wider() doesn't have the same probelm.

@hadley hadley added bug an unexpected problem or unintended behavior nesting 🐦 nesting, chopping, and packing and removed reprex needs a minimal reproducible example labels Nov 1, 2023
@hadley
Copy link
Member

hadley commented Nov 1, 2023

Looks like something is going wrong deep in the bowels of chop().

@DavisVaughan
Copy link
Member

Tracking in r-lib/vctrs#1892, it's a vctrs issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior nesting 🐦 nesting, chopping, and packing
Projects
None yet
Development

No branches or pull requests

3 participants