`as_independent` is not able to handle any type of container object in a mathematically meaningful way: if you want to remove `x0` dependence from  `r = y + Sum(x + x0, (i, 0, n))`, for example, the term independent of `x0` is `y + Sum(x, (i, 0, n))` (mathematically) but `as_independent(r)[0]` gives you `y`. This is a general problem with `as_independent` (and also with the previous code); I wrote a special case for `Piecewise` because handling this particular container type was necessary to avoid test failures, although I do not remember which test it was.