我已经寻找了很长时间来找到解决我的问题的方法。
我使用下面的代码从我想要的列中获取数据
import pandas as pd
df = pd.read_excel("Live_data_test.xlsx","Sheet1")
number_of_entries = len(df.loc[:, 'Time'])
number_of_entries_last_3 = number_of_entries - 3
unix_x1 = df.loc[number_of_entries_last_:number_of_entries, 'Time']
print(unix_x1)
我得到了输出
10 1.513753e+09
11 1.513753e+09
12 1.513753e+09
Name: Time, dtype: float64
我想将此时间转换为可读时间,以便可以将其输入到 matplotlib 图形的 x 轴中。
real_x1 = datetime.datetime.strptime(str(unix_x1), '%Y-%m-%d %H:%M:%S')
我收到错误
ValueError: time data '10 1.513753e+09\n11 1.513753e+09\n12 1.513753e+09\nName: Time, dtype: float64' does not match format '%Y-%m-%d %H:%M:%S'
如何将这个 UNIX 时间输出为用户可读的格式?
我对代码有点陌生,所以如果您回答,能否请您解释一下原因?
最佳答案
Pandas 可以读取unix纪元时间,使用单位参数
pd.to_datetime('1.513753e+09', unit = 's')
Timestamp('2017-12-20 06:56:40')
您可以使用
传递您的列pd.to_datetime(df[<your_datetime_column>], unit = 's')
关于python - 将 Unix 纪元时间转换为 Pandas 中的日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49949244/