pandas 将字符串日期时间转换为熊猫日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41501726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:43:36  来源:igfitidea点击:

Convert string date time to pandas datetime

pythonpython-2.7csvpandasdatetime

提问by Sourabh Saxena

I am new to Pandas and Python. I want to do some date time operations in my script. I am getting date time information from a csv file in following format: 01APR2017 6:59

我是 Pandas 和 Python 的新手。我想在我的脚本中做一些日期时间操作。我从以下格式的 csv 文件中获取日期时间信息: 01APR2017 6:59

How to convert it into pandas datetime format? Something like: 2017-04-01 06:59:00

如何将其转换为Pandas日期时间格式?类似于: 2017-04-01 06:59:00

回答by jezrael

You can use to_datetimewith parameter format:

您可以使用to_datetime参数format

s = pd.Series(['01APR2017 6:59','01APR2017 6:59'])

print (s)
0    01APR2017 6:59
1    01APR2017 6:59
dtype: object

print (pd.to_datetime(s, format='%d%b%Y %H:%M'))
0   2017-04-01 06:59:00
1   2017-04-01 06:59:00
dtype: datetime64[ns]

Another possible solution is use date_parserin read_csv:

另一种可能的解决方案是使用date_parserread_csv

import pandas as pd
from pandas.compat import StringIO

temp=u"""date
01APR2017 6:59
01APR2017 6:59"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
parser = lambda x: pd.datetime.strptime(x, '%d%b%Y %H:%M')
df = pd.read_csv(StringIO(temp), parse_dates=[0], date_parser=parser)

print (df)
                 date
0 2017-04-01 06:59:00
1 2017-04-01 06:59:00

print (df.date.dtype)
datetime64[ns]

EDIT by comment:

通过评论编辑:

If values cannot be parsed to datetime, add parameter errors='coerce'for convert to NaT:

如果无法将值解析为datetime,请添加errors='coerce'用于转换为的参数NaT

s = pd.Series(['01APR2017 6:59','01APR2017 6:59', 'a'])
print (s)
0    01APR2017 6:59
1    01APR2017 6:59
2                 a
dtype: object

print (pd.to_datetime(s, format='%d%b%Y %H:%M', errors='coerce'))
0   2017-04-01 06:59:00
1   2017-04-01 06:59:00
2                   NaT
dtype: datetime64[ns]