Reputation: 8950
I have a pandas dataframe with a column for years and one for months. How can I create a new date column based on these two (I can assume day = 15).
I tried the following:
import pandas as pd
import numpy as np
import datetime
df = pd.DataFrame()
df['year'] = np.arange(2000,2010)
df['mydate']= datetime.date( df['year'].apply(lambda x: int(x)) , 1 , 1)
but I get this error message:
df['mydate']= datetime.date( df['year'].apply(lambda x: int(x)) , 1 , 1) File "C:\Anaconda\lib\site-packages\pandas\core\series.py",
line 77, in wrapper "cannot convert the series to {0}".format(str(converter))) TypeError: cannot convert the series to
which I don't understand because I explictly convert x to int.
Thanks!
Upvotes: 0
Views: 5623
Reputation: 4983
You can build another column based on the existing columns by using df.apply(fnc, axis=1)
.
In your case this becomes:
df = pd.DataFrame()
df['year'] = np.arange(2000,2010)
df['month'] = 6
df['date_time']= df.apply(lambda row :
datetime.date(row.year,row.month,15),
axis=1)
Upvotes: 2