python read_excel converters_在Python中Pandas Read_Excel Datetime Converter

使用

Python 3.6和Pandas 0.19.2:如何读取excel文件并将列直接从read_excel更改为datetime?与

This Question about converters and dtypes类似.但我想在某个列中读取日期时间

我想改变这个:

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

变成这样的东西:

df1 = pd.read_excel(file,dtypes = {‘COLUMN’:datetime})

代码没有错误,但在我的例子中,COLUMN在调用print(df1 [‘COLUMN’]后仍然是int64的类型.dtype)

我尝试过使用np.datetime64而不是datetime.我也尝试过使用converter =而不是dtypes =但无济于事.这可能是挑剔,但在我的代码中实现将是一个很好的功能.

通常,读取Excel工作表将使用Excel工作表中定义的dtypes,但您不能像

read_csv中那样指定dtypes.您可以提供一个

converters arg,您可以为其传递列的dict和func以调用以转换列:

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

你可能感兴趣的:(python,read_excel,converters)