Behavior of select when trying to remove the index from a tsibble

I wonder: Why does removing the index from a tsibble fail silently instead of giving an error? I just had a case where the error would have helped my debugging.
See this code:

library(tsibble)

ts <- tsibble(
  Time = seq(as.Date("2020-01-01"), by = "1 months", length.out = 3),
  Series1 = c(3, 1, 2), index = Time
)
print(ts)

library(dplyr)
ts_sel <- select(ts, -Time)
print(ts_sel)

with output

# A tsibble: 3 x 2 [1D]
  Time       Series1
  <date>       <dbl>
1 2020-01-01       3
2 2020-02-01       1
3 2020-03-01       2
# A tsibble: 3 x 2 [1D]
  Series1 Time      
    <dbl> <date>    
1       3 2020-01-01
2       1 2020-02-01
3       2 2020-03-01

(on R 4.0.4, tsibble 1.1.4, dplyr 1.1.4).

Hi Robert,

I think it's because it didn't fail: It seems a tsibble requires an index column, so Time continues to be an index column until it's been replaced by another index column. This is in keeping with the principle that subsetting an object should produce a subobject (i.e., an object of the same type), so that any subsetting of a tsibble leaves the index column intact.

:+1: I see. Maybe a warning could be reasonable?

1 Like

Sounds reasonable to me — maybe a request to the developers is in order. I'd be curious to see how select() is implemented for tsibbles.

I don't know if you've tried the debug() function, but if you run debug(select) and then run your code, you can walk through how select() works in this context. (Use n to step over statements and s to step into statements.) After you're done, make sure to run undebug(select) so that you don't go into debug mode the next time you run select().

If you do this, you can see that there's a hidden function called bind_tsibble() that explicitly passes on the index column as the last column if it there's an attempt to remove it with select().

1 Like

Just a quick follow-up: I opened the issue add warning that alerts user when they inadvertently attempt to remove index from tsibble? , so we'll see if a warning is in order.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.