如何将Unix日期列表转换为可读格式

时间:2019-03-28 13:58:35

标签: python

我知道如何将unix日期转换为可读日期,但是我很难转换整个unix日期列表。

这是我当前的代码:

time = df['Time']
list(np.float_(time))
print(datetime.datetime.utcfromtimestamp(time/1000).strftime('%Y-%m-%d %H:%M:%S'))

输出:

TypeError                                 Traceback (most recent call last)
<ipython-input-52-6532ae588a4d> in <module>()
     45 time = df['Time']
     46 list(np.float_(time))
---> 47 print(datetime.datetime.utcfromtimestamp(time/1000).strftime('%Y-%m-%d %H:%M:%S'))
     48 
     49 

/usr/local/lib/python3.5/dist-packages/pandas/core/series.py in wrapper(self)
    110             return converter(self.iloc[0])
    111         raise TypeError("cannot convert the series to "
--> 112                         "{0}".format(str(converter)))
    113 
    114     return wrapper

TypeError: cannot convert the series to <class 'float'>

3 个答案:

答案 0 :(得分:1)

如果您的pandas DataFrame已经具有一列UNIX时间戳记浮动,请使用pandas.to_datetime直接将其转换为时间戳记字符串。

$(function () {
  $("#btnQueryString").bind("click", function () {
    var url = "BPT.html?marquecl=" + encodeURIComponent($(".MarqueCl").val()) + 
      "&serie=" + encodeURIComponent($(".Serie").val()) +
      "&category=" + encodeURIComponent($(".Category").val()) + 
      "&kyword=" + encodeURIComponent($("#kword").val());
    window.location.href = url;
  });
});
import pandas as pd

df = pd.DataFrame({'unix_ts': [140000000000, 15000000000]})
print(df)
print(pd.to_datetime(df.unix_ts))

答案 1 :(得分:0)

Pandas内置了对此类事物的支持:

time = pandas.to_datetime(df['Time'])

根据Time列的格式,您将需要指定format的{​​{1}}参数。参见https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html

然后,很容易使用to_datetimetime系列(包含Datetime个对象)转换为字符串系列。

答案 2 :(得分:0)

尝试一下

time = df['Time'].astype(float).tolist()
相关问题