1
$\begingroup$

I deleted my previous post since I believe it had unnecessary information and the direction of the question seemed to be straying away from what I wanted. I apologize to anyone that looked into it.

I was able to boil the question down to a simple function I am looking for:

How am I able to look at a time series data set and then look at the first date in the data and then insert a new date that is the closest beginning of a quarter of that dataset

For example, my data might be something like:

data={{{2015, 8, 10, 0, 0, 0.}, 21.1}, {{2015, 8, 21, 0, 0, 
0.},20.7}, {{2015, 8, 24, 0, 0, 0.},18.8}, {{2015, 8, 25, 0, 0, 
0.}, 22.0}, {{2016, 2, 3, 0, 0, 0.},13.8}, {{2016, 2, 4, 0, 0, 0.}, 
15.3}, {{2016, 2, 5, 0, 0, 0.},12.6}}

I want to be able to find the quarter it is in and start inserting data2 (see below) where it starts.

I want to join this data with another dataset that inserts "0" values into months that aren't present:

data2=Transpose[{#, Array[0 &, {Length@#}]}] &[
DateRange[{data[[1, 1, 1]]}, {data[[-1, 1, 1]]}, "Month"]]

From the example above, looking at data, I want to be able to write data2 such that it will start at quarter 3 {{2015,7,1,0,0,0.},XX},{{2015, 8, 10, 0, 0, 0.}, 21.1}, {{2015, 8, 21, 0, 0, 0.},20.7}, ...}}

Thank you!

$\endgroup$

2 Answers 2

1
$\begingroup$

Does this do what you want?

roundback[{y_, m_, ___}] := {y, Ceiling[m, 3] - 2, 1, 0, 0, 0`}

roundback[{2015, 8, 10, 0, 0, 0.}]
{2015, 7, 1, 0, 0, 0.}
{{roundback @ #[[1]], "XX"}, ##} & @@ data
{{{2015, 7, 1, 0, 0, 0.}, "XX"},
 {{2015, 8, 10, 0, 0, 0.}, 21.1},
 {{2015, 8, 21, 0, 0, 0.}, 20.7},
 {{2015, 8, 24, 0, 0, 0.}, 18.8},
 {{2015, 8, 25, 0, 0, 0.}, 22.},
 {{2016, 2, 3, 0, 0, 0.}, 13.8},
 {{2016, 2, 4, 0, 0, 0.}, 15.3},
 {{2016, 2, 5, 0, 0, 0.}, 12.6}}
$\endgroup$
1
  • $\begingroup$ Thanks for such a quick response Mr. Wizard! This is absolutely what I needed. Thank you so much :) Will accept in 1 min! $\endgroup$
    – jeff
    Commented Aug 7, 2017 at 16:04
3
$\begingroup$

In M11.1, there is CurrentDate:

CurrentDate[DateList[], "Quarter"] //DateList
CurrentDate[{2015,3,3}, "Quarter"] //DateList

{2017, 7, 1, 0, 0, 0.}

{2015, 1, 1, 0, 0, 0.}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.