4
$\begingroup$

From the following list I wish to extract those cases for which the magnitude of a quantity is greater than zero for which I would like to then create a DateListPlot.

 list = {{DateObject[{2017, 01}, "Month"], Quantity[0.00, "Dollars"]},
         {DateObject[{2017, 02}, "Month"], Quantity[681.00, "Dollars"]},
         {DateObject[{2017, 03}, "Month"], Quantity[681.00, "Dollars"]},
         {DateObject[{2017, 04}, "Month"], Quantity[704.00, "Dollars"]},
         {DateObject[{2017, 05}, "Month"], Quantity[0.00, "Dollars"]},
         {DateObject[{2017, 06}, "Month"], Quantity[0.00, "Dollars"]},
         {DateObject[{2017, 07}, "Month"], Quantity[0.00, "Dollars"]},
         {DateObject[{2017, 08}, "Month"], Quantity[704.00, "Dollars"]},
         {DateObject[{2017, 09}, "Month"], Quantity[0.00, "Dollars"]},
         {DateObject[{2017, 10}, "Month"], Quantity[970.80, "Dollars"]},
         {DateObject[{2017, 11}, "Month"], Quantity[0.00, "Dollars"]},
         {DateObject[{2017, 12}, "Month"], Quantity[704.00, "Dollars"]}}

However, using the following expression and variations I can't seem to create an appropriate syntax. Instead creating a DateListPlot for a sublist with those elements for which the QuantityMagnitude is greater than zero, I get the error: "Quantity::unkunit: Unable to interpret unit specification {Automatic,Automatic}".

 DateListPlot[Cases[list, {_, x_} && QuantityMagnitude[x] > 0]]

The following doesn't work either:

 DateListPlot[Cases[list, {_, QuantityMagnitude[x_] > 0}]]

How can I structure the pattern for cases to obtain the desired result to include only non-zero values of the magnitude?

$\endgroup$
1
  • $\begingroup$ @creidhne - QuantityMagnitude[x] > 0 does not require x to be a Quantity. A positive scalar would also evaluate to True $\endgroup$
    – Bob Hanlon
    Commented Oct 1, 2023 at 3:34

3 Answers 3

4
$\begingroup$
$Version

(* "13.3.1 for Mac OS X ARM (64-bit) (July 24, 2023)" *)

Clear["Global`*"]

list = {{DateObject[{2017, 01}, "Month"], 
    Quantity[0.00, "Dollars"]}, {DateObject[{2017, 02}, "Month"], 
    Quantity[681.00, "Dollars"]}, {DateObject[{2017, 03}, "Month"], 
    Quantity[681.00, "Dollars"]}, {DateObject[{2017, 04}, "Month"], 
    Quantity[704.00, "Dollars"]}, {DateObject[{2017, 05}, "Month"], 
    Quantity[0.00, "Dollars"]}, {DateObject[{2017, 06}, "Month"], 
    Quantity[0.00, "Dollars"]}, {DateObject[{2017, 07}, "Month"], 
    Quantity[0.00, "Dollars"]}, {DateObject[{2017, 08}, "Month"], 
    Quantity[704.00, "Dollars"]}, {DateObject[{2017, 09}, "Month"], 
    Quantity[0.00, "Dollars"]}, {DateObject[{2017, 10}, "Month"], 
    Quantity[970.80, "Dollars"]}, {DateObject[{2017, 11}, "Month"], 
    Quantity[0.00, "Dollars"]}, {DateObject[{2017, 12}, "Month"], 
    Quantity[704.00, "Dollars"]}};

list2 = Cases[list, {_, _Quantity?(QuantityMagnitude[#] > 0 &)}, 
   Infinity];

DateListPlot[list2, PlotRange -> All]

enter image description here

$\endgroup$
3
$\begingroup$
  DateListPlot[
    list /. {_, _[_?NonPositive, _]} :> Nothing,
    PlotRange -> All]

enter image description here

$\endgroup$
2
$\begingroup$
listnew = Cases[list, {_, Quantity[_?Positive, _]}]

or

listnew = Cases[list, {_, Quantity[_?(# > 0 &), _]}]
DateListPlot[listnew, PlotRange -> All]

enter image description here

$\endgroup$
1
  • $\begingroup$ All approaches informative for handling lists containing explicitly specified quanties. DateListPlot[list /. {, _[?NonPositive, _]} :> Nothing, PlotRange -> All], however, plots missing values as gaps in the data. The rest exclude missing values. Thanks for the insight gentlemen. $\endgroup$ Commented Oct 1, 2023 at 18:59

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.