简体   繁体   English

使用python和pandas将numpy的float64值数组转换为datetime64

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

If I have a NumPy array of float64 values.如果我有一个float64值的NumPy 数组 I know these values represent dates in format of datetime64[ns] .我知道这些值以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但是我得到一个ValueError: Inferred frequency 86400N from传递的值不符合传递的频率 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' .时间值必须是“2015-01-01T23:50:00.000000000”、“2015-01-02T23:50:00.000000000”、“2015-01-03T23:50:00.00000” How can i archive this?我该如何存档? Thanks!谢谢!

You can use pd.to_datetime() with unit s , as follows:您可以将pd.to_datetime()与单位s ,如下所示:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM