0

I have a timestamp Matrix with (72500, 1) dimension in MATLAB. I have written such a statement in MATLAB:

returnMatrix = datestr(M(:,1)/86400 + datenum(1970,1,1)- 4/24);

And I successfully get the output date matrix back as a return Matrix.

returnMatrix = ['29/06/2015',...,'06/07/2015']

In order to get the same output, I have written such a statement in Python:

returnMatrix = (M[:, 0] / 86400 + date.toordinal(date(1970, 1, 1)) + 366 - (4/24))
returnMatrix = np.apply_along_axis((lambda x:[date.fromtimestamp(x[0]).strftime("%d/%m/%y")]),1,returnMatrix)

But as an output, I get this result:

returnMatrix = ['09/01/1970',...,'09/01/1970']

How should I write the statement in order to get the output as the one in MATLAB?

2 Answers 2

1

What I understand from this question is that you have data in seconds which you want to add to the base date which is 1/1/1970 and get the new date. Python 'timedelta' method from 'datetime' module will be handy to solve this problem.

Here I present my solution to your problem

    import datetime
    base_date = datetime.datetime(1970,1,1,0,0,0)
    return_matrix = []
    for time in M:
        return_matrix.append(base_date + datetime.timedelta(seconds=time))


The return matrix will contain all the dates. No need for additional calculation.

I assume the data is saved in list M. I don't understand the way you are using python list. The way you have accessed the list i.e M[:0] will actually split the list from zeroth element to zeroth element excluding the zeroth. If it is a one dimensional data then you can directly use M the way I have used. I think you got confused there coming from a MATLAB background.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer Sharad. I will let you know after trying your solution. But there is no other way to write it by using lambda? Because for loop takes so much time to process.
I can't say with surety about lambda because I haven't used it.
list comprehension can be another solution for the problem. you can create a list in this way: return_matrix = [base_date + datetime.timedelta(seconds=time) for time in M]
@Sharad It seems like OP is using a two-dimensional NumPy array for M by analogy to how it would work in Matlab. The indexing in his example is correct.
Thanks for the info. I haven't used numpy so had no idea
0

Given M is a numpy array containing seconds, you can do this, if you can live with having your date strings in ISO8601 format

dates = (M*numpy.timedelta64(1, 's') + 
         numpy.datetime64('1970-01-01T00:00') -
         numpy.timedelta64(4, 'h').astype('timedelta64[s]')).astype('datetime64[D]')

(that .astype is there to squelch a warning about implicit conversion).

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.