I have been searching for a long time to find a solution to my problem.
我一直在寻找解决问题的方法。
I get the data from the column I want using the below code
我使用下面的代码从我想要的列中获取数据
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)
I get the output
我得到了输出
10 1.513753e+09
11 1.513753e+09
12 1.513753e+09
Name: Time, dtype: float64
I want to convert this time into readable time so I can input it into the x axis of a matplotlib graph.
我想将此时间转换为可读时间,以便将其输入到matplotlib图的x轴中。
real_x1 = datetime.datetime.strptime(str(unix_x1), '%Y-%m-%d %H:%M:%S')
I get the error
我收到了错误
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'
how do I get this unix time to output into a readable format for a user?
如何将此unix时间输出为用户的可读格式?
I am a little new to code so if you answer, could you please explain the reasoning if you can?
我对代码有点新意,所以如果你回答,如果可以的话,请你解释一下推理吗?
3 个解决方案
#1
0
Your problem has to do with converting the values that you've read (looks like seconds after Unix epoch, i.e. January 1, 1970) into datetime
objects. The error you are getting is because your times are just a floating-point number, but that is not how you are trying to handle them.
您的问题与将您已阅读的值(看起来像Unix纪元后的秒数,即1970年1月1日)转换为datetime对象有关。您得到的错误是因为您的时间只是一个浮点数,但这不是您尝试处理它们的方式。
Assuming these are seconds after Unix epoch, you need to create your datetimes using a timedelta
from a start point defined as the Unix epoch:
假设这些是在Unix纪元之后的几秒钟,您需要使用定义为Unix纪元的起点的timedelta创建日期时间:
from datetime import datetime, timedelta
start = datetime(1970, 1, 1) # Unix epoch start time
df['datetime'] = df.Time.apply(lambda x: start + timedelta(seconds=x))
The last line creates a new column in your dataframe called 'datetime'
and populates it by reading the 'Time'
column in as x
, and calculating the time x
seconds after Unix epoch.
最后一行在数据框中创建一个名为“datetime”的新列,并通过读取x中的“Time”列并计算Unix纪元后的x秒来填充它。
Note: if you want to convert these datetime
objects into the time string that you specified, we can do this by creating a new column with strftime()
:
注意:如果要将这些日期时间对象转换为您指定的时间字符串,我们可以通过使用strftime()创建一个新列来完成此操作:
df['string_time'] = df.datetime.apply(lambda x: x.strftime('%Y-%m-%d %H:%M:%S'))
#2
1
Pandas can read unix epoch time, use unit parameter
熊猫可以读取unix纪元时间,使用单位参数
pd.to_datetime('1.513753e+09', unit = 's')
Timestamp('2017-12-20 06:56:40')
You can pass your column using
您可以使用传递列
pd.to_datetime(df[<your_datetime_column>], unit = 's')
#3
0
you could try df['Time'] = pd.to_datetime(df['Time'], format='%Y%m%d.0')
你可以尝试df ['时间'] = pd.to_datetime(df ['时间'],格式='%Y%m%d.0')
#1
0
Your problem has to do with converting the values that you've read (looks like seconds after Unix epoch, i.e. January 1, 1970) into datetime
objects. The error you are getting is because your times are just a floating-point number, but that is not how you are trying to handle them.
您的问题与将您已阅读的值(看起来像Unix纪元后的秒数,即1970年1月1日)转换为datetime对象有关。您得到的错误是因为您的时间只是一个浮点数,但这不是您尝试处理它们的方式。
Assuming these are seconds after Unix epoch, you need to create your datetimes using a timedelta
from a start point defined as the Unix epoch:
假设这些是在Unix纪元之后的几秒钟,您需要使用定义为Unix纪元的起点的timedelta创建日期时间:
from datetime import datetime, timedelta
start = datetime(1970, 1, 1) # Unix epoch start time
df['datetime'] = df.Time.apply(lambda x: start + timedelta(seconds=x))
The last line creates a new column in your dataframe called 'datetime'
and populates it by reading the 'Time'
column in as x
, and calculating the time x
seconds after Unix epoch.
最后一行在数据框中创建一个名为“datetime”的新列,并通过读取x中的“Time”列并计算Unix纪元后的x秒来填充它。
Note: if you want to convert these datetime
objects into the time string that you specified, we can do this by creating a new column with strftime()
:
注意:如果要将这些日期时间对象转换为您指定的时间字符串,我们可以通过使用strftime()创建一个新列来完成此操作:
df['string_time'] = df.datetime.apply(lambda x: x.strftime('%Y-%m-%d %H:%M:%S'))
#2
1
Pandas can read unix epoch time, use unit parameter
熊猫可以读取unix纪元时间,使用单位参数
pd.to_datetime('1.513753e+09', unit = 's')
Timestamp('2017-12-20 06:56:40')
You can pass your column using
您可以使用传递列
pd.to_datetime(df[<your_datetime_column>], unit = 's')
#3
0
you could try df['Time'] = pd.to_datetime(df['Time'], format='%Y%m%d.0')
你可以尝试df ['时间'] = pd.to_datetime(df ['时间'],格式='%Y%m%d.0')