简体   繁体   中英

Convert Unix epoch time to datetime in Pandas

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.

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?

I am a little new to code so if you answer, could you please explain the reasoning if you can?

Pandas can read unix epoch time, use unit parameter

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')

Your problem has to do with converting the values that you've read (looks like seconds after Unix epoch, ie 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.

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:

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.

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() :

df['string_time'] = df.datetime.apply(lambda x: x.strftime('%Y-%m-%d %H:%M:%S'))

你可以尝试df['Time'] = pd.to_datetime(df['Time'], format='%Y%m%d.0')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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