简体   繁体   English

熊猫中datetime系列的增量属性

[英]Increment attributes of a datetime Series in pandas

I have a Series containing datetime64[ns] elements called series , and would like to increment the months. 我有一个包含名为series datetime64[ns]元素的series ,并且想要增加月份。 I thought the following would work fine, but it doesn't: 我认为以下方法可以正常工作,但不能:

    series.dt.month += 1

The error is 错误是

    ValueError: modifications to a property of a datetimelike object are not supported. Change values on the original.

Is there a simple way to achieve this without needing to redefine things? 有没有一种简单的方法可以实现这一目标而无需重新定义内容?

First, I created timeseries date example: 首先,我创建了时间序列日期示例:

import datetime
t = [datetime.datetime(2015,4,18,23,33,58),datetime.datetime(2015,4,19,14,32,8),datetime.datetime(2015,4,20,18,42,44),datetime.datetime(2015,4,20,21,41,19)]
import pandas as pd
df = pd.DataFrame(t,columns=['Date'])

Timeseries: 时间序列:

df
Out[]: 
                 Date
0 2015-04-18 23:33:58
1 2015-04-19 14:32:08
2 2015-04-20 18:42:44
3 2015-04-20 21:41:19

Now increment part, you can use offset option. 现在增加部分,您可以使用偏移选项。

df['Date']+pd.DateOffset(days=30)

Output: 输出:

df['Date']+pd.DateOffset(days=30)
Out[66]: 
0   2015-05-18 23:33:58
1   2015-05-19 14:32:08
2   2015-05-20 18:42:44
3   2015-05-20 21:41:19
Name: Date, dtype: datetime64[ns]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM