How would you add a time stamp column to a Pandas dataframe?

13,617

I think simpliest is use insert only:

df = pd.DataFrame({'A': list('AAA'), 'B': range(3)}, index=list('xyz'))
print (df)
   A  B
x  A  0
y  A  1
z  A  2

df.insert(0, 'TimeStamp', pd.to_datetime('now').replace(microsecond=0))
print (df)
            TimeStamp  A  B
x 2018-02-15 07:35:35  A  0
y 2018-02-15 07:35:35  A  1
z 2018-02-15 07:35:35  A  2

Your working version - change range(df.shape[0]) to df.index for same indices in Series and in DataFrame:

t = [datetime.datetime.utcnow().replace(microsecond=0) for i in df.index]
s = pd.Series(t, name = 'TimeStamp')
df.insert(0, 'TimeStamp', s)
Share:
13,617

Related videos on Youtube

Murchak
Author by

Murchak

Updated on June 04, 2022

Comments

  • Murchak
    Murchak almost 2 years

    Every time I append a dataframe to a text file, I want it to contain a column with the same timestamp for each row. The timestamp could be any arbitrary time as long as its different from next time when I append a new dataframe to the existing text file. Below code inserts a column named TimeStamp, but doesn't actually insert datetime values. The column is simply empty. I must be overlooking something simple. What am I doing wrong?

    t = [datetime.datetime.now().replace(microsecond=0) for i in range(df.shape[0])]
    s = pd.Series(t, name = 'TimeStamp')
    df.insert(0, 'TimeStamp', s)
    
    • Alex
      Alex about 6 years
      You just have some typos (see edits)... It works fine for me.
    • Murchak
      Murchak about 6 years
      I don't see the typo in my code as no error was raised. The code ran fine, but I just get empty values
    • Clock Slave
      Clock Slave about 6 years
      @Murchak, your code works fine for me. No empty values.
    • Paul H
      Paul H about 6 years
      does df.insert operate in-place or return a copy? if it returns a copy, you'll need to capture the result
  • Murchak
    Murchak about 6 years
    Thanks jezrael! Your solution above worked. However, changing my code to what your recommended (i in df.index) did not. It still gave me empty entries.
  • jezrael
    jezrael about 6 years
    I have better solution. Give me a sec.
  • Joseph Juhnke
    Joseph Juhnke over 3 years
    One quick note, df.datetime.now is being deprecated. Instead you could use datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")