Pandas Read_Excel Datetime Converter

36,559

Solution 1

Typically reading excel sheets will use the dtypes defined in the excel sheets but you cannot specify the dtypes like in read_csv for example. You can provide a converters arg for which you can pass a dict of the column and func to call to convert the column:

df1 = pd.read_excel(file, converters= {'COLUMN': pd.to_datetime})

Solution 2

read_excel supports dtype, just as read_csv, as of this writing:

import datetime

import pandas as pd


xlsx = pd.ExcelFile('path...')
df = pd.read_excel(xlsx, dtype={'column_name': datetime.datetime})

https://pandas.pydata.org/docs/reference/api/pandas.read_excel.html

Share:
36,559
MattR
Author by

MattR

I am a Data Analyst who uses Python to solve various data-centered problems. I also create powerful programs using Python. I use SQL daily along side SSIS and Tableau

Updated on July 27, 2021

Comments

  • MattR
    MattR almost 3 years

    Using Python 3.6 and Pandas 0.19.2: How do you read in an excel file and change a column to datetime straight from read_excel? Similar to This Question about converters and dtypes. But I want to read in a certain column as datetime

    I want to change this:

    import pandas as pd
    import datetime
    import numpy as np
    
    file = 'PATH_HERE'
    
    df1 = pd.read_excel(file)
    
    df1['COLUMN'] = pd.to_datetime(df1['COLUMN']) # <--- Line to get rid of
    

    into something like: df1 = pd.read_excel(file, dtypes= {'COLUMN': datetime})

    The code does not error, but in my example, COLUMN is still a dtype of int64 after calling print(df1['COLUMN'].dtype)

    I have tried using np.datetime64 instead of datetime. I have also tried using converters= instead of dtypes= but to no avail. This may be nit picky, but would be a nice feature to implement in my code.