简体   繁体   中英

PYTHON: Pandas DF column as datetime

I am trying to get pandas to recognise a datetime when reading a tab delimited txt file.

data = pd.read_csv('1.txt', header=0, parse_dates=True, 
                   date_parser=lambda x: pd.to_datetime(x, format='%Y%m%d%H%M'))

The format of my datetime is always '%Y%m%j%H%M' and I have tried also using

infer_datetime_format=True

The data is as:

,id,date,mmhr

0,1,200709270000,0

How can I get pandas.read_csv to recognise datetime on import?

Many thanks

Your date columns looks like an int to pandas. pandas expects dates to look like strings, as in 1990-1-1 , not 1990101 .

You need to use parse_dates = [2]

pd.read_csv('1.txt', parse_dates=[2])

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