Python pd.to_datetime 小时和秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48444373/
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
pd.to_datetime Hours and Seconds
提问by Alessandro Ceccarelli
Good Evening,
晚上好,
I have a dataframe(Pandas), with a column representing dates, in the following format:
我有一个数据框(Pandas),其中一列表示日期,格式如下:
print(df["date"])
14/01/18 12:47
14/01/18 12:48
14/01/18 12:50
14/01/18 12:57
14/01/18 12:57
14/01/18 12:57
14/01/18 12:57
14/01/18 12:57
14/01/18 12:58
Specifically, I would like to: 1. Convertit to datetime, using pd.to_datetime2. Create the following additionalcolumns:
具体来说,我想: 1.使用pd.to_datetime将其转换为datetime2. 创建以下附加列:
df["month"]
df["day"]
df["year"]
df["hour"]
df["minute"]
I tried to run:
我试着跑:
df['date'] = pd.to_datetime(df['date'], format = "%d/%m/%Y %H/%M" )
But the following errorappears:
但是出现以下错误:
time data '02/01/18 08:41' does not match format '%d/%m/%Y %H/%M' (match)
回答by Orenshi
Alternatively to grovina's answer ... instead of using apply you can directly use the dtaccessor.
或者 grovina 的答案......而不是使用 apply 您可以直接使用dt访问器。
Here's a sample:
这是一个示例:
>>> data = [['2017-12-01'], ['2017-12-30'],['2018-01-01']]
>>> df = pd.DataFrame(data=data, columns=['date'])
>>> df
date
0 2017-12-01
1 2017-12-30
2 2018-01-01
>>> df.date
0 2017-12-01
1 2017-12-30
2 2018-01-01
Name: date, dtype: object
Note how df.date is an object? Let's turn it into a date like you want
请注意 df.date 如何是一个对象?让我们把它变成你想要的约会
>>> df.date = pd.to_datetime(df.date)
>>> df.date
0 2017-12-01
1 2017-12-30
2 2018-01-01
Name: date, dtype: datetime64[ns]
The format you want is for string formatting. I don't think you'll be able to convert the actual datetime64 to look like that format. For now, let's make a newly formatted string version of your date in a separate column
您想要的格式用于字符串格式。我认为您无法将实际的 datetime64 转换为该格式。现在,让我们在单独的列中创建一个新格式化的日期字符串版本
>>> df['new_formatted_date'] = df.date.dt.strftime('%d/%m/%y %H:%M')
>>> df.new_formatted_date
0 01/12/17 00:00
1 30/12/17 00:00
2 01/01/18 00:00
Name: new_formatted_date, dtype: object
Finally, since the df.date column is now of date datetime64... you can use the dtaccessor right on it. No need to use apply
最后,由于 df.date 列现在是 datetime64... 你可以dt在它上面使用访问器。无需使用申请
>>> df['month'] = df.date.dt.month
>>> df['day'] = df.date.dt.day
>>> df['year'] = df.date.dt.year
>>> df['hour'] = df.date.dt.hour
>>> df['minute'] = df.date.dt.minute
>>> df
date new_formatted_date month day year hour minute
0 2017-12-01 01/12/17 00:00 12 1 2017 0 0
1 2017-12-30 30/12/17 00:00 12 30 2017 0 0
2 2018-01-01 01/01/18 00:00 1 1 2018 0 0
回答by grovina
The formatyou want is '%d/%m/%y %H:%M'(lowercase y and colon between hour and minute). Take a look here.
您想要的格式是'%d/%m/%y %H:%M'(小时和分钟之间的小写 y 和冒号)。看看这里。
Then you can create the other columns:
然后您可以创建其他列:
df['month'] = df['date'].apply(lambda x: x.month)
df['day'] = df['date'].apply(lambda x: x.day)
df['year'] = df['date'].apply(lambda x: x.year)
df['hour'] = df['date'].apply(lambda x: x.hour)
df['minute'] = df['date'].apply(lambda x: x.minute)

