Search code examples
pythonpandasdatetime64time-frequency

convert numpy array of float64 values to datetime64 with python and pandas


If I have a NumPy array of float64 values. I know these values represent dates in format of datetime64[ns]. I try to convert them with pandas. But I get an ValueError: Inferred frequency 86400N from passed values does not conform to passed frequency N

time = np.array([1420156200.0,1420242600.0,1420329000.0], dtype='float64')
pd.DatetimeIndex(time, freq='ns')

The time values must be '2015-01-01T23:50:00.000000000', '2015-01-02T23:50:00.000000000', '2015-01-03T23:50:00.000000000'. How can i archive this? Thanks!


Solution

  • You can use pd.to_datetime() with unit s, as follows:

    time = np.array([1420156200.0,1420242600.0,1420329000.0], dtype='float64')
    pd.to_datetime(time, unit='s')
    

    Result:

    DatetimeIndex(['2015-01-01 23:50:00', '2015-01-02 23:50:00',
                   '2015-01-03 23:50:00'],
                  dtype='datetime64[ns]', freq=None)