Convert Timestamp to String in Pandas

Convert Timestamp to String in Pandas

  1. Use the dt.stfrtime() Function to Convert Pandas Timestamp Series to String
  2. Use the astype() Function to Convert a Pandas Timestamp Series to a String

Pandas is an officially recognized Python library utilized for Data Analysis and Manipulation and usually uses a mutable data structure to store values called a Pandas DataFrame. Apart from the generic data, a Pandas DataFrame is an excellent option to store the datetime values.

There are two ways to convert a timestamp series stored in a Pandas DataFrame into a string in Python, both of which have been thoroughly explained below in this article.

Use the dt.stfrtime() Function to Convert Pandas Timestamp Series to String

The strftime() function converts a datetime object into a string. It is simply a string representation of any given datetime object.

When combined with the accessor dt in Python as a prefix, the dt.strftime() function can return a sequence of strings after converting them from the series of timestamps or datetime objects. The following code uses the dt.stfrtime() function to convert a pandas timestamp series to a string in Python.

Code:

import pandas as pd
dfx = pd.DataFrame({'date': pd.to_datetime(pd.Series(['20210101', '20210105','20210106', '20210109'])), 'tickets sold': [1080, 1574, 2279, 1910]})
dfx['date'] = dfx['date'].dt.strftime('%Y-%m-%d')
print(dfx)
print(dfx.dtypes)

Output:

         date  tickets sold
0  2021-01-01          1080
1  2021-01-05          1574
2  2021-01-06          2279
3  2021-01-09          1910
date            object
tickets sold     int64
dtype: object

Use the astype() Function to Convert a Pandas Timestamp Series to a String

The astype() function will convert the data type of a Pandas DataFrame columns. The astype() function works alike in cases of both a single column or a set of columns.

Syntax:

df.astype(dtype, copy=True, errors='raise')

The parameters for the functions mentioned above are explained in detail below.

  1. dtype - specifies the data type we want to convert the given timestamp series into.
  2. copy - When set to True, it creates a copy of the contents and then makes the necessary changes to it.
  3. errors - It specifies whether we want to allow the raising of exceptions or not. Its value can be either raise or ignore.

The following example uses the astype() function to convert a pandas timestamp series to a string in Python.

Code:

import pandas as pd
dfx = pd.DataFrame({'date': pd.to_datetime(pd.Series(['20210101', '20210105','20210106', '20210109'])), 'tickets sold': [1080, 1574, 2279, 1910]})
dfx['date']=dfx['date'].astype(str)
print(dfx)
print(dfx.dtypes)

Output:

         date  tickets sold
0  2021-01-01          1080
1  2021-01-05          1574
2  2021-01-06          2279
3  2021-01-09          1910
date            object
tickets sold     int64
dtype: object

Related Article - Pandas String

  • Filter Rows That Contain a Specific String in Pandas
  • Get Substring in Pandas
  • Split Strings Into Two List Columns Using str.split in Python Pandas