# Finding Missing Values in TimeSeries

With TimeSeries it is all too common to end up with something like this:

    ts = TimeSeries[
Flatten@Join[{RandomReal[1, 3], 2 + Missing[],
RandomReal[1, 2]}], {Range[6]}]

ts["Values"]

{0.108378, 0.126124, 0.890195, 2 + Missing[], 0.908709, 0.405934}


I want to find a way to locate values that contain missing and either remove them or replace them with a simple Missing[].

As far as I know, Pick, Cases and Select don't work directly on timeseries.

So my solution looks like this:

goodvals = Flatten@Position[ts["Values"],
x_ /; Not@ResourceFunction["ContainsMissing"][x], 1]

{0, 1, 2, 3, 5, 6}


Note that the ContainsMissing function also works on the Head of the argument (List in this case).

So then we would do

ts2 = TimeSeries[ts["DatePath"][[Rest@goodvals]]]


So my questions are:

1. Am I correct that there isn't a way to use Pick, Select, or Cases directly on TimeSeries?

2. If there a more efficient solution to the problem than mine?

3. More generally, how can I remove/replace any values in a list that contain missing, using pattern matching?

For instance, how would you remove the 2+Missing[] value from the list of ts values, using pattern matching rules in a way that will generalize to any expression that contains Missing?

• DeleteMissing[ts]["Values"] ?
– Syed
Jan 8, 2023 at 10:32

You may use "ReplaceAll". Here are some examples:

To remove the missing entry entirely:

ts = TimeSeries[Flatten@Join[{RandomReal[1, 3], 2 + Missing[],
RandomReal[1, 2]}], {Range[6]}]

tmp = ts /. c___ + Missing[] -> Nothing;
tmp["Values"]

(* {0.141638, 0.901978, 0.943813, 0.705432, 0.798243} *)


To replace the missing item by: Missing[]:

tmp = ts /. c___ + Missing[] -> Missing[];
tmp["Values"]

(* {0.141638, 0.901978, 0.943813, Missing[], 0.705432, 0.798243} *)

• That's a pretty good solution, but its very specific to the form taken by the expression containing Missing. But it wouldn't work for timeseries containing values such as Missing[] + 3, 1.7*Missing[]^2, Exp[Missing[]], etc, etc. Can you generalize your solution? Jan 8, 2023 at 10:25
• Try e.g.: ts /. (_ : 0) + Missing[]^( _ : 1) -> Nothing Jan 8, 2023 at 12:21

This is based on Daniel Huber's solution and attempts to generalize it, using repeated replacement rules. It works for a fairly general class of expressions that contain Missing.

xlist = {RandomReal[1, 3], 2 + Exp[Missing[]], RandomReal[1, 2]}

{{0.0291775, 0.935745, 0.278923},
2 + E^Missing[], {0.645253, 0.140903}}

xlist //. {f___[c___, Missing[]] -> Missing[],
f___[Missing[], c___] -> Missing[],
Missing[] -> Nothing} // Flatten

{0.0291775, 0.935745, 0.278923, 0.645253, 0.140903}