仅在 Pandas 中将日期时间转换为月份和年份 [重复]

Posted

技术标签:

【中文标题】仅在 Pandas 中将日期时间转换为月份和年份 [重复]【英文标题】:Convert datetime to month and year only in Pandas [duplicate] 【发布时间】:2021-12-18 13:33:03 【问题描述】:

我有一个这样格式的专栏:

开业日期/时间

2014-09-01 00:17:00

2014-09-18 18:55:00

我已使用以下函数将其转换为日期时间

df['Date/Time Opened'] = pd.to_datetime(df['Date/Time Opened'])

如何将其转换为“mm/yyyy”格式,以便按频率/计数将其绘制在图表中?

【问题讨论】:

我投票决定将其作为Extracting just Month and Year separately from Pandas Datetime column的副本关闭 【参考方案1】:

试试:

df['Date/Time Opened'].dt.to_period('M')

【讨论】:

以上是关于仅在 Pandas 中将日期时间转换为月份和年份 [重复]的主要内容,如果未能解决你的问题,请参考以下文章