从python中的日期中提取月份 [英] extract month from date in python

查看:269
本文介绍了从python中的日期中提取月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一列日期格式为 2010-01-31.我可以使用

I have a column of dates in the format 2010-01-31. I can extract the year using

#extracting year
year = df["date"].values
year = [my_str.split("-")[0] for my_str in year]
df["year"] = year

我正在尝试获取月份,但我不明白如何在第二次拆分时获取它.

I'm trying to get the month, but I don't understand how to get it on the second split.

推荐答案

import datetime

a = '2010-01-31'

datee = datetime.datetime.strptime(a, "%Y-%m-%d")


datee.month
Out[9]: 1

datee.year
Out[10]: 2010

datee.day
Out[11]: 31

这篇关于从python中的日期中提取月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆