Search code examples
pythonexcelpython-3.xpandasxlrd

Convert excel file with timestamp to python date and time


How can I convert a large .xlsx file which contains a lot of timestamps (i.e. 1537892885364) into date and time ( in python and then save it as a new .xlsx file?

I am new to python, and I tried lots of ways to achieve this today, but I did not find a solution.

Below is the code I used, but it gives me '[Errno 13] Permission denied'. I tried different ways which also gave problems.

from __future__ import absolute_import, division, print_function
import os
import pandas as pd

def main(path, filename, absolute_path_organisation_structure):
    absolute_filepath = os.path.join(path,filename)
    #Relevant list formed with 4th, 5th and 6th columns
    df = pd.read_excel(absolute_filepath, header=None, parse_cols=[4,5,6])
    # Transform column 0 and 2 to datetime
    df[0] = pd.to_datetime(df[0])
    df[2] = pd.to_datetime(df[2])
    print(df)

path = open(r'C:\\Users\\****\\data')
MISfile  = 'filename.xlsx'
main(path, MISfile,None)

Solution

  • Hope this helps:

    # requires the following packages:
    # - pandas
    # - xlrd
    # - openpyxl
    
    import pandas as pd
    
    # reading in the excel file timestamps.xlsx
    # this file contains a column 'epoch' with the unix epoch timestamps
    df = pd.read_excel('timestamps.xlsx')
    
    # translate epochs into human readable and write into newly created column
    # note, your timestamps are in ms, hence the unit
    df['timestamp'] = pd.to_datetime(df['epoch'],unit='ms')
    
    # write to excel file 'new_timestamps.xlsx'
    # index=False prevents pandas to add the indices as a new column
    df.to_excel('new_timestamps.xlsx', index=False)