In this data analysis tutorial we will learn how to cast datetime objects to strings in general Python programming tasks and during Data Analysis.
Cast a date to string in Python
We will Let’s create a datetime object:
import datetime
my_date = datetime.datetime(2023,12,1)
print(my_date)
We will get the following date object:
2023-12-01 00:00:00
If we look at its type we’ll see that is datetime.datetime.
We can easily transform this variable to string using the strftime formatter, which casts dates to string using the format you specify. Make sure to specify the required time units that you would like to see.
my_date_str = my_date.strftime ('%d-%m-%y')
print ( my_date_str )
This returns : 01-12-23.
my_date_str = my_date.strftime('%Y%m%d')
print ( my_date_str )
Returns the a string in yyyymmdd format: 20231101
Obviously, if w check the the Python object type, we’ll see it is a string.
type(my_date_str )
Convert a pandas column containing dates to string
Next case that we’ll see is how to cast a pandas DataFrame column that contains date values to strings.
Create a DataFrame
# Initialize DataFrame
import pandas as pd
dates = pd.date_range(start='10/10/2023', periods = 5, freq = 'B' )
revenue = [381, 428, 239, 465, 564]
data = pd.DataFrame(dict(dates=dates, revenue=revenue))
data.head()
Let’s look into the DataFrame first rows:
dates | revenue | |
---|---|---|
0 | 2023-10-10 | 381 |
1 | 2023-10-11 | 428 |
2 | 2023-10-12 | 239 |
3 | 2023-10-13 | 465 |
4 | 2023-10-16 | 564 |
And if we look at the pandas data types we see that the dates column type is a datetime64.
data.dtypes
dates datetime64[ns] revenue int64 dtype: object
Using the strftime formatter
Converting to string is once again, very easy thanks to the pandas dt accessor function that you can use on a pandas column (Series object). To cast the column values use the following snippet
data['dates'] = data['dates'].dt.strftime('%Y%m%d')
This return the following values – note that the data type of the column is now an object which is used by pandas to represent strings (among other objects).
0 20231010 1 20231011 2 20231012 3 20231013 4 20231016 Name: dates, dtype: object
Using the pandas astype() Series method
Another option is to use the astype() function:
data['dates'] = data['dates'].astype('string')
Next learning
How to convert a string to an hexadecimal number with Python?