Reputation: 21
I want to add columns startDate
and x
, this columns dtype is int64. Below is my code.
B['startDate'] columsn dtype is datetime64[ns]
B['startDate']+datetime.timedelta(days=(30*B['x']))
But it gives me the following error:
TypeError: unsupported type for timedelta days component: Series
Upvotes: 0
Views: 177
Reputation: 863116
Use to_timedelta
here if performance is important:
df['newDate'] = df['startDate'] + pd.to_timedelta(df['x'] * 30, unit='d')
print (df)
startDate x newDate
0 2020-01-01 86 2027-01-24
1 2020-01-02 58 2024-10-07
2 2020-01-03 1 2020-02-02
3 2020-01-04 95 2027-10-24
4 2020-01-05 87 2027-02-27
5 2020-01-06 45 2023-09-17
6 2020-01-07 63 2025-03-11
7 2020-01-08 92 2027-07-30
8 2020-01-09 30 2022-06-27
9 2020-01-10 22 2021-10-31
For 1k rows it is 8times faster like loop solution with map
:
np.random.seed(2021)
N = 1000
df = pd.DataFrame({'startDate': pd.date_range('2020-1-1', periods=N),
'x': np.random.randint(1, 100, N),})
print (df)
In [97]: %timeit df['startDate'] + pd.to_timedelta(df['x'] * 30, unit='d')
505 µs ± 18.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [98]: %timeit df['startDate'] + df['x'].map(lambda x: datetime.timedelta(days=30 * x))
4.01 ms ± 450 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Upvotes: 1
Reputation: 3280
You have to modify your code slightly so that timedelta
takes a valid days
argument. Here is a toy example:
B = pd.DataFrame({'startDate': pd.date_range('2020-1-1', periods=10), 'x': np.random.randint(1, 100, 10),})
B['newDate'] = B['startDate'] + B['x'].map(lambda x: datetime.timedelta(days=30 * x))
Output:
startDate x newDate
0 2020-01-01 89 2020-03-30
1 2020-01-02 69 2020-03-11
2 2020-01-03 84 2020-03-27
3 2020-01-04 73 2020-03-17
4 2020-01-05 34 2020-02-08
5 2020-01-06 63 2020-03-09
6 2020-01-07 17 2020-01-24
7 2020-01-08 12 2020-01-20
8 2020-01-09 15 2020-01-24
9 2020-01-10 28 2020-02-07
Upvotes: 0