Matt Warren
Matt Warren

Reputation: 696

Octave convert dataframe column of datetime strings to secs

Using Octave I have created a dataframe as the following;

>> df=dataframe("example.csv")
df = dataframe with 10 rows and 3 columns
Src: example.csv
_1                         X1     X2      X3
Nr                       char double  double
 1 2016-11-01 11:35:33.285127   1000 1205531
 2 2016-11-01 11:35:54.666606   2000 1205531
 3 2016-11-01 11:37:17.775407   3000 1205531
 4 2016-11-01 11:37:47.436518   4000 1205531
 5 2016-11-01 11:38:16.246998   5000 1205531
 6 2016-11-01 11:38:38.371844   6000 1205531
 7 2016-11-01 11:38:59.806459   7000 1205531
 8 2016-11-01 11:39:20.543602   8000 1205531
 9 2016-11-01 11:39:47.920399   9000 1205531
10 2016-11-01 11:41:24.370811  10000 1205531

I am looking to convert column X1 to seconds-since-the epoch (or a similar sequential measure of time, total seconds since 'the beginning of time' would also be fine)

I can get this to work with a single element (I'm assuming the ans is correct);

>> mktime(strptime(df.array(1,1),"%Y-%m-%e %T"))
ans =    1.4780e+09

But I'm not having much luck trying to convert the whole column; I have tried various permutations along the lines of;

>> mktime(strptime(df(:,1),"%Y-%m-%e %T"))
error: wrong type argument 'class'
error: strptime: argument STR must be a string


>> mktime(strptime(df(:,1).array(1,1),"%Y-%m-%e %T"))
error: Invalid call to numel.  Correct usage is:-- Overloaded Function:  numel (A)
error: called from
    numel at line 33 column 7


>> mktime(strptime(df(:,1).array(),"%Y-%m-%e %T"))
error: Invalid call to numel.  Correct usage is:-- Overloaded Function:  numel (A)
error: called from
    numel at line 33 column 7


>> mktime(strptime(df.array(:,1),"%Y-%m-%e %T"))
ans =    1.4780e+09

I am looking for an expression that will let me do an assignment like;

df(:,1)=expression()

Upvotes: 0

Views: 1742

Answers (1)

Andy
Andy

Reputation: 8091

I would use:

format long   #to see the differences     
x = cellfun (@(x) mktime (strptime(x, "%Y-%m-%e %T")),
             cellstr (df.array(:,1)))
x =

   1477996533
   1477996554
   1477996637
   1477996667
   1477996696
   1477996718
   1477996739
   1477996760
   1477996787
   1477996884

Upvotes: 1

Related Questions