How can I make a python numpy arange of datetime

后端 未结 5 1963
挽巷
挽巷 2020-12-09 01:48

I have some input data, with timestamps in the input file in the form of hours from the date time specified in the filename.

This is a bit useless, so I need to conv

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 02:11

    As noted in another answer, for Numpy > 1.7, you can use Numpy's built-in datetime capability. The examples in the Numpy documentation don't include using np.arange with steps, so here's one:

    timearray = np.arange('2000-01-01', '2000-01-02',np.timedelta64(1,'h'), dtype='datetime64')

    Numpy sets the dtype of this result to datetime64[h]. You can set this explicitly to some smaller unit of time with dtype='datetime64[m]'.

    In version 1.8.1 (and I expect earlier), trying to add an offset to that result array that is smaller than an hour will have no effect.

    • timearray += np.timedelta64(10,'s') does not change timearray
    • timearray2 = timearray + np.timedelta64(10,'s') will add 10 seconds to timearray and converts the dtype of timearray2 to datetime64[s]

提交回复
热议问题