Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a date column in pandas df using constant value in str

i have a table in pandas df

    product_id_x    product_id_y    count
0   2727846            7872456       1
1   29234              2932348       2
2   29346              9137500       1
3   29453              91365738      1
4   2933666            91323494      1

i want to add a new column 'dates' which i have defined in a str.

dateSelect = "'2016-11-06'"

so i added a new constant column

df['dates'] = dateSelect 

but i am getting result as

   product_id_x   product_id_y    count   dates
0   2727846          7872456         1  '2016-11-06'
1   29234            2932348         2  '2016-11-06'
2   29346            9137500         1  '2016-11-06'
3   29453            91365738        1  '2016-11-06'
4   2933666          91323494        1  '2016-11-06'

the values in the dates are coming in quotes. and

type(df['dates']) = str

but i want it in date format, because further i am going to store this table in my mysql database. and i want the type to be date.

from sqlalchemy import create_engine
engine = create_engine('mysql+mysqldb://name:[email protected]/dbname', echo=False)
df.to_sql(name='tablename', con=engine, if_exists = 'append', index=False)
like image 961
Shubham R Avatar asked Nov 29 '16 06:11

Shubham R


2 Answers

most direct route

df['dates'] = pd.Timestamp('2016-11-06')
df

   product_id_x  product_id_y  count      dates
0       2727846       7872456      1 2016-11-06
1         29234       2932348      2 2016-11-06
2         29346       9137500      1 2016-11-06
3         29453      91365738      1 2016-11-06
4       2933666      91323494      1 2016-11-06
like image 84
piRSquared Avatar answered Oct 14 '22 17:10

piRSquared


I think you can use first replace ' by empty space and then to_datetime:

dateSelect = pd.to_datetime("'2016-11-06'".replace("'",""))
print (dateSelect)
2016-11-06 00:00:00

print (type(dateSelect))
<class 'pandas.tslib.Timestamp'>

df['dates'] = pd.to_datetime("'2016-11-06'".replace("'",""))

print (df)
   product_id_x  product_id_y  count      dates
0       2727846       7872456      1 2016-11-06
1         29234       2932348      2 2016-11-06
2         29346       9137500      1 2016-11-06
3         29453      91365738      1 2016-11-06
4       2933666      91323494      1 2016-11-06

print (df.dtypes)
product_id_x             int64
product_id_y             int64
count                    int64
dates           datetime64[ns]
dtype: object
like image 26
jezrael Avatar answered Oct 14 '22 16:10

jezrael