如何在熊猫的日期列中添加一年

时间:2018-02-14 21:43:36

标签: python pandas datetime

我正在尝试将一年添加到pandas数据框中的日期列中,但是当我使用pd.to_timedelta时,我会获得额外的时间和时间。分钟。我知道我可以利用更新的时间并缩短工作时间,但我觉得必须有一种方法可以准确地添加一年。我的尝试如下:

import pandas as pd
dates = pd.DataFrame({'date':['20170101','20170102','20170103']})
dates['date'] = pd.to_datetime(dates['date'], format='%Y%m%d')
dates['date2'] = dates['date'] +  pd.to_timedelta(1, unit='y')
dates

的产率:

Out[1]: 
    date        date2
0   2017-01-01  2018-01-01 05:49:12
1   2017-01-02  2018-01-02 05:49:12
2   2017-01-03  2018-01-03 05:49:12

如何添加一年而不添加05:49:12 HH:mm:ss?

5 个答案:

答案 0 :(得分:6)

In [99]: dates['date'] + pd.offsets.DateOffset(years=1)
Out[99]:
0   2018-01-01
1   2018-01-02
2   2018-01-03
Name: date, dtype: datetime64[ns]

闰年检查:

In [100]: pd.to_datetime(['2011-02-28', '2012-02-29']) + pd.offsets.DateOffset(years=1)
Out[100]: DatetimeIndex(['2012-02-28', '2013-02-28'], dtype='datetime64[ns]', freq=None)

答案 1 :(得分:2)

您可以通过pd.Series.dt.normalize进行规范化:

dates['date2'] = (dates['date'] +  pd.to_timedelta(1, unit='y')).dt.normalize()

答案 2 :(得分:1)

或将datetime转换为date

dates['date'] = dates['date'].apply(lambda a: a.date())

答案 3 :(得分:0)

编辑:如果您不关心闰年等,这是有效的。否则请参阅jp_data_analysis的答案。

您可以使用365和<yyy:createJob xmlns:yyy="http://place.org/mmm/app"> <job> <id></id> <origin>AMS</origin> <metaId>2255202</metaId> <title>My Workorder Description</title> <dispatchGroup>FWSAMS</dispatchGroup> <priority>3</priority> <assignedTo>006325</assignedTo> <jobDocument> <![CDATA[<root><workorderNum>MWO-522</workorderNum><KUBROUNDENTRY><KUBROUNDENTRYID>267433</KUBROUNDENTRYID><LINEID>254735</LINEID><OBSERVATION/><KUBROUNDLINE><ASSETNUM>385691</ASSETNUM><METERNAME>VLOPSTAT</METERNAME><SEQUENCE>90</SEQUENCE><ASSET><ASSETID>388391</ASSETID><DESCRIPTION>Asset Description</DESCRIPTION><SERVICEADDRESS><STREETADDRESS>1234 Kingston Pike</STREETADDRESS></SERVICEADDRESS></ASSET><METER><DOMAINID>VLOPSTAT</DOMAINID><METERNAME>VLOPSTAT</METERNAME></METER></KUBROUNDLINE></KUBROUNDENTRY><KUBROUNDENTRY><KUBROUNDENTRYID>267432</KUBROUNDENTRYID><LINEID>254734</LINEID><OBSERVATION/><KUBROUNDLINE><ASSETNUM>385691</ASSETNUM><METERNAME>GLEAKGRD</METERNAME><SEQUENCE>80</SEQUENCE><ASSET><ASSETID>388391</ASSETID><DESCRIPTION>Asset2 Description</DESCRIPTION><SERVICEADDRESS><STREETADDRESS>2345 Kingston Pike</STREETADDRESS></SERVICEADDRESS></ASSET><METER><DOMAINID>CASEGRDE</DOMAINID><METERNAME>GLEAKGRD</METERNAME></METER></KUBROUNDLINE></KUBROUNDENTRY><skipReasonDomain><VALUE>TRAFFIC</VALUE><VALUE>NOT FOUND</VALUE><VALUE>SKIP ALL</VALUE></root>]]> </jobDocument> <completionDocument></completionDocument> </job> </yyy:createJob>

unit='d'

答案 4 :(得分:0)

您可以使用 dataframe["column"].dt.component 形式的代码访问日期的组成部分(年、月和日)。

例如,月份组件为 dataframe["column"].dt.month,年份组件为 dataframe["column"].dt.year