39

I am new to python (coming from R), and I am trying to understand how I can convert a timestamp series in a pandas dataframe (in my case this is called df['timestamp']) into what I would call a string vector in R. is this possible? How would this be done?

I tried df['timestamp'].apply('str'), but this seems to simply put the entire column df['timestamp'] into one long string. I'm looking to convert each element into a string and preserve the structure, so that it's still a vector (or maybe this a called an array?)

M--
  • 25,431
  • 8
  • 61
  • 93
StatsStudent
  • 1,384
  • 2
  • 10
  • 28
  • 1
    Is this what you are looking for? `df['timestamp'].apply(lambda x: x.strftime(%Y-%m-%d %H:%M:%S))` – geo Nov 04 '17 at 02:53

3 Answers3

52

Consider the dataframe df

df = pd.DataFrame(dict(timestamp=pd.to_datetime(['2000-01-01'])))

df

   timestamp
0 2000-01-01

Use the datetime accessor dt to access the strftime method. You can pass a format string to strftime and it will return a formatted string. When used with the dt accessor you will get a series of strings.

df.timestamp.dt.strftime('%Y-%m-%d')

0    2000-01-01
Name: timestamp, dtype: object

Visit strftime.org for a handy set of format strings.

piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • 1
    `dt` is the `datetime` accessor. Whenever your column values are Timestamps or Timedeltas, Pandas makes the `dt` accessor available. From that accessor, you can use many other datetime specific methods. In this case, I used `strftime`. – piRSquared Aug 24 '18 at 04:06
  • No, I didn't import the `datetime` module. – piRSquared Aug 24 '18 at 04:07
  • Are all directives listed on http://strftime.org supported by `pandas.Timestamp.strftime`? – quant_dev Jun 10 '19 at 21:22
  • `AttributeError: 'DataFrame' object has no attribute 'timestamp'` – huang Feb 20 '21 at 03:44
  • timestamp is a column name in the example I gave. Use the name for your column instead – piRSquared Feb 20 '21 at 12:00
14

Use astype

>>> import pandas as pd
>>> df = pd.to_datetime(pd.Series(['Jul 31, 2009', '2010-01-10', None])) 
>>> df.astype(str)
0    2009-07-31
1    2010-01-10
2           NaT
dtype: object

returns an array of strings

VinceP
  • 2,058
  • 2
  • 19
  • 29
5

Following on from VinceP's answer, to convert a datetime Series in-place do the following:

df['Column_name']=df['Column_name'].astype(str)

MarMat
  • 790
  • 8
  • 12