I have a data frame that contains 2 columns, one is Date and other is float number. I would like to add those 2 to get the following:
我有一个包含2列的数据框,一个是Date,另一个是float number。我想添加这些2以获得以下内容:
Index Date Days NewDate
0 20-04-2016 5 25-04-2016
1 16-03-2015 3.7 20-03-2015
As you can see if there is decimal it is converted as int as 3.1--> 4 (days). I have some weird questions so I appreciate any help. Thank you !
如您所见,如果有小数,则将其转换为int - 3.1 - > 4(天)。我有一些奇怪的问题,所以我感谢任何帮助。谢谢 !
2 个解决方案
#1
4
First, ensure that the Date
column is a datetime object:
首先,确保Date列是datetime对象:
df['Date'] = pd.to_datetime(df['Date'])
Then, we can convert the Days
column to int by ceiling it and the converting it to a pandas Timedelta:
然后,我们可以将Days列转换为int by ceiling并将其转换为pandas Timedelta:
temp = df['Days'].apply(np.ceil).apply(lambda x: pd.Timedelta(x, unit='D'))
Datetime objects and timedeltas can be added:
可以添加日期时间对象和时间点:
df['NewDate'] = df['Date'] + temp
#2
#1
4
First, ensure that the Date
column is a datetime object:
首先,确保Date列是datetime对象:
df['Date'] = pd.to_datetime(df['Date'])
Then, we can convert the Days
column to int by ceiling it and the converting it to a pandas Timedelta:
然后,我们可以将Days列转换为int by ceiling并将其转换为pandas Timedelta:
temp = df['Days'].apply(np.ceil).apply(lambda x: pd.Timedelta(x, unit='D'))
Datetime objects and timedeltas can be added:
可以添加日期时间对象和时间点:
df['NewDate'] = df['Date'] + temp