使用timedelta向datetime64添加一个月 [英] Adding one month to datetime64 with timedelta

查看:590
本文介绍了使用timedelta向datetime64添加一个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试此操作时:

>>> a = numpy.datetime64('1995-12-31')
>>> b = a + pandas.Timedelta(1, unit='M')
>>> print(b)

我希望看到

1996-01-31

但是我得到

1996-01-30 10:29:06.

知道为什么吗?非常感谢.

Any idea why? Many thanks.

推荐答案

一个月的时间增量是一年的长度除以12.

A time delta of one month is the length of a year divided by 12.

您需要检查日期并添加适当的天数. 或者,增加月份数(如果需要,可以滚动到下一年),并保持天数不变.

You need to examine your date and add the appropriate quantity of days. Alternately, increment the month number (rolling over to the next year, if needed), and leave the day number unchanged.

这篇关于使用timedelta向datetime64添加一个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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