python - add a 'now' timestamp column to a pandas df -
i have following code:
s1 = pd.dataframe(np.random.uniform(-1,1,size=10)) s2 = pd.dataframe(np.random.normal(-1,1, size=10)) s3 = pd.concat([s1, s2], axis=1) s3.columns = ['s1','s2']
which generates df looks this:
s1 s2 0 -0.841204 -1.857014 1 0.961539 -1.417853 2 0.382173 -1.332674 3 -0.535656 -2.226776 4 -0.854898 -0.644856 5 -0.538241 -2.178466 6 -0.761268 -0.662137 7 0.935139 0.475334 8 -0.622293 -0.612169 9 0.872111 -0.880220
how can add column (or replace index 0-9), timestamp time? np array not have size 10
you can use datetime's now
method create time stamp , either assign new column like: s3['new_col'] = dt.datetime.now()
or assign direct index:
in [9]: import datetime dt s3.index = pd.series([dt.datetime.now()] * len(s3)) s3 out[9]: s1 s2 2014-08-17 23:59:35.766968 0.916588 -1.868320 2014-08-17 23:59:35.766968 0.139161 -0.939818 2014-08-17 23:59:35.766968 -0.486001 -2.524608 2014-08-17 23:59:35.766968 0.739789 -0.609835 2014-08-17 23:59:35.766968 -0.822114 -0.304406 2014-08-17 23:59:35.766968 -0.050685 -1.295435 2014-08-17 23:59:35.766968 -0.196441 -1.715921 2014-08-17 23:59:35.766968 -0.421514 -1.618596 2014-08-17 23:59:35.766968 -0.695084 -1.241447 2014-08-17 23:59:35.766968 -0.541561 -0.997481
note going lot of duplicate values in index due resolution , speed of assignment, not sure how useful is, better have separate column in opinion.
Comments
Post a Comment