Question

I am trying to write a small script on Mathematica that graphs a particular set of data which I obtain using Python. The data output is as follows: It is a standard datetime object being echoed onto a tsv file.

2014-03-29 08:39:37.080834  04  0.403980970383  0.2
2014-03-29 08:39:39.449200  04  0.383723974228  0.2
2014-03-29 08:39:42.603058  04  0.475341081619  0.2
2014-03-29 08:39:44.882941  04  0.303984165192  0.2
2014-03-29 08:39:47.021998  04  0.312470912933  0.2
2014-03-29 08:39:48.951891  04  0.312346935272  0.2

The first column is the datetime object, time and date are separated by a space not a \t. The next 3 columns are the ones separated by a \t, but are irrelevant to the question at hand (although I'd be happy to tell what they mean if anyone is interested).

So what I am trying to do is import the first column into Mathematica so that I can graph the first and second column (i.e the date and time (x axis) with the 04s (y axis, representing events)). To do so I did this:

data = Import["/home/pi/Desktop/data.tsv"]
dates = data[[;;,1]]
DateList[dates]

But on the last line is where I get an error. I know for a fact that if I call the DateList function and I give it one of the date objects in "quotes" it works, but since for some reason these strings are not in quotes it returns an error. The error indicates that the input cannot be interpreted as a date or time input.

I've tried using something like DateList[dates//InputForm[%]], but it still does not work.

Please help me understand how strings work in Mathematica! :)

Fed

Was it helpful?

Solution

The string format for your dates includes "-" and " " and ":". The documentation for Datelist claims you are supposed to be able to give it a format string and get it to extract a date from a string, but repeated attempts to get that to work have failed. Carefully study the details of the documentation and see if you can give it a string format that works. OR just do a little string hacking and extract your own fields and be done with it.

In[1]:= data = Import["data.tsv"];
dates = data[[;; , 1]];
fixeddates = Map[ToExpression["{" <> StringReplace[#,
   {"-" -> ",", " " -> ",", ":" -> ","}] <> "}"] &, dates]

Out[3]= {{2014, 3, 29, 8, 39, 37.0808}, {2014, 3, 29, 8, 39, 39.4492},
   {2014, 3, 29, 8, 39, 42.6031}, {2014, 3, 29, 8, 39, 44.8829},
   {2014, 3, 29, 8, 39, 47.022}, {2014, 3, 29, 8, 39, 48.9519}}

where each date is now a list of numbers.

Ah, a little progress on getting formatting strings to work. This works

In[18]:= DateList[{"2014-03-29 08:39:48", {"Year", "-", "Month",
  "-", "Day", " ", "Hour", ":", "Minute", ":", "Second"}}]

Out[18]= {2014, 3, 29, 8, 39, 48.}

but this doesn't

In[19]:= DateList[{"2014-03-29 08:39:48.951891", {"Year", "-", "Month",
   "-", "Day", " ", "Hour", ":", "Minute", ":", "Second"}}]

During evaluation of In[19]:= DateString::str: String 2014-03-29 08:39:48.951891 cannot be interpreted as a date in format {Year,-,Month,-,Day, ,Hour,:,Minute,:,Second}. >>

Out[19]= DateList[{"2014-03-29 08:39:48.951891", {"Year", "-", 
 "Month", "-", "Day", " ", "Hour", ":", "Minute", ":", "Second"}}]

So Datelist will extract Integer seconds but not Real seconds even though the documentation says "Day, hour, minute, and second values can be nonintegers:"

Unless someone else can come up with a good explanation that really looks like a bug.

Edit showing how to then plot the data

data = Import["data.tsv"];
points = data[[All, {1, 3}]]; (*extract date and 3rd column*)
fixedpoints = Map[{ToExpression["{" <> StringReplace[First[#],
   {"-" -> ",", " " -> ",", ":" -> ","}] <> "}"], Last[#]} &, points];
DateListPlot[fixedpoints, PlotStyle -> PointSize[0.02]]

...PlotSnipped...

OTHER TIPS

I don't have any experience with mathematica, but it should be easy enough to make sure that each of the dates has quotes around it:

data = Import["/home/pi/Desktop/data.tsv"]
dates = data[[;;,1]]
dates = ['"' + date + '"' for date in dates]
DateList[dates]

FYI I'm assuming that each date in dates is a string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top