2
$\begingroup$

I've been stuck in this problem for 1 week. I just want to make a graph (Log-Return vs time (in year, month, day)).

The code is:

datanikkei = Import["C:\\Users\\Rodrigo\\Desktop\\Nikkei.csv"] #Here I import the data from excel
close = datanikkei[[All, 5]]; #take the close price column
close = Delete[close, 1]; #Delete Title
ret = Differences[Log[close]*100.0] #calculate the returns

date = datanikkei[[All, 1]]; #Take the dates column
date = Delete[date, {{1}, {2}}]; #Delete title and first date of the list
date = Partition[date, 1]
list = Riffle[date, ret]
list = Partition[list, 2]
DateListPlot[list]

I get this:

{{{"1984,1,5"}, 0.201268}, {{"1984,1,6"}, 
  0.140647}, {{"1984,1,7"}, -0.0702988}, {{"1984,1,8"}, 
  0.620933}, {{"1984,1,9"}, 0.567476}, {{"1984,1,10"}, 
  0.307281}, {{"1984,1,11"}, 0.464084}, {{"1984,1,12"}...

The different functions that I had used (like DateListPlot) to plot the returns vs the dates gave an error. But if I errase the quotation marks, it works. The error is:

DateListPlot::dtvals: "Unable to automatically determine horizontal coordinates for the given data and DataRange. !(*ButtonBox[\">>\", Appearance->{Automatic, None}, BaseStyle->\"Link\", ButtonData:>\"paclet:ref/DateListPlot\", ButtonNote->\"DateListPlot::dtvals\"])"

Please, help me Rodrigo

$\endgroup$
  • $\begingroup$ I've partially formatted your question but note that comments in Mathematica are written like: (* #Delete title and first date of the list *) $\endgroup$ – Mike Honeychurch Jan 29 '15 at 3:38
  • $\begingroup$ Notwithstanding the fix in the answer below, your output is unusual {"1984,1,5"}. Normally dates are converted to date lists when imported from CSV or Excel so this problem shouldn't arise (??) $\endgroup$ – Mike Honeychurch Jan 29 '15 at 3:40
3
$\begingroup$
mydata = {{{"1984,1,5"}, 0.201268}, {{"1984,1,6"}, 0.140647}, 
         {{"1984,1,7"}, -0.0702988}, {{"1984,1,8"}, 0.620933}, 
         {{"1984,1,9"}, 0.567476}, {{"1984,1,10"}, 0.307281}, 
         {{"1984,1,11"}, 0.464084}};

fixedData = ToExpression /@ ({StringSplit[#[[1]][[1]], ","], #[[2]]} & /@ mydata)

DateListPlot[fixedData]

enter image description here

The trick was splitting the string using the criterion of a comma, then converting each individual string numeral into an integer via ToExpression.

$\endgroup$
  • 2
    $\begingroup$ +1 -- FromDigits is a bit safer than ToExpression however as it is less general, avoiding possibly unwanted code execution. $\endgroup$ – Mr.Wizard Jan 29 '15 at 3:47
  • $\begingroup$ Ohh... a subtlety. Yes. Thanks. $\endgroup$ – David G. Stork Jan 29 '15 at 4:01
0
$\begingroup$

Another formulation:

list = {{{"1984,1,5"}, 0.201268},
        {{"1984,1,6"}, 0.140647},
        {{"1984,1,7"}, -0.0702988},
        {{"1984,1,8"}, 0.620933},
        {{"1984,1,9"}, 0.567476},
        {{"1984,1,10"}, 0.307281},
        {{"1984,1,11"}, 0.464084}};

list /. {s_String} :> ImportString[s, "CSV"][[1]]
{{{1984, 1, 5}, 0.201268}, {{1984, 1, 6}, 0.140647},
 {{1984, 1, 7}, -0.0702988}, {{1984, 1, 8}, 0.620933},
 {{1984, 1, 9}, 0.567476}, {{1984, 1, 10}, 0.307281},
 {{1984, 1, 11}, 0.464084}}
$\endgroup$
  • $\begingroup$ @MathLind It is the result of a code block inside a quote block. Each line is started with > followed by five spaces. You can select the output and use the buttons in the editor toolbar. Not everyone thinks it's nice, but it works for me. $\endgroup$ – Mr.Wizard Jan 29 '15 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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