Our task for today is to cast a datetime /timestamp pandas column to strings objects.
Create example data
Let’s start by importing the pandas module and creating our data. Feel free to use the snippet below to follow along with the example:
import pandas as pd
stamps = pd.Series(pd.date_range(start='1/1/24', periods=6, freq='6h'))
interviews = pd.Series ([24, 33, 26, 32, 27, 17] )
#Initialize the DataFrame
data = pd.DataFrame ((dict(stamps=stamps, interviews = interviews)))
print(data.head())
Let’s look into our data:
stamps | interviews | |
---|---|---|
0 | 2024-01-01 00:00:00 | 24 |
1 | 2024-01-01 06:00:00 | 33 |
2 | 2024-01-01 12:00:00 | 26 |
3 | 2024-01-01 18:00:00 | 32 |
4 | 2024-01-02 00:00:00 | 27 |
5 | 2024-01-02 06:00:00 | 17 |
Look into the stamps column – the data type is datatime64, but if we use the bracket notation we see that the elements are shown as timestamps.
stamps[0]
Will return:
Timestamp('2024-01-07 00:00:00')
Note: We can create a single pandas timestamp object also using the pd.Timestamp() method:
stamp_ts = pd.Timestamp ('2024-01-02')
Convert the datetime timestamps to objects in pandas
We use the datetime64 dt accessor and the very flexible strftime formatter to convert your datetime stamps to a string.
data['stamps_str'] = data['stamps'].dt.strftime('%d/%m/%y')
This convert the stamps datetime column to a string with format dd/mm/yyyy – note that after formatting with strftime, the Series / column dtype is object.
stamps | interviews | stamps_str | |
---|---|---|---|
0 | 2024-01-01 00:00:00 | 24 | 01/01/24 |
1 | 2024-01-01 06:00:00 | 33 | 01/01/24 |
2 | 2024-01-01 12:00:00 | 26 | 01/01/24 |
3 | 2024-01-01 18:00:00 | 32 | 01/01/24 |
4 | 2024-01-02 00:00:00 | 27 | 02/01/24 |
In a similar fashion you can convert to other formats such as yyyy-mm-dd (using strftime(‘%y-%m-%d).
If we look into the DataFrame pandas data types we’ll see that the new column has an object data type (representing a string in this case) type.
stamps datetime64[ns] interviews int64 stamps_str object dtype: object
We can verify the Python type by looking at the first cell in the stamps_str column:
type(data['stamps_str'][0])
This will return str – as the object is a Python string.
Cast a pandas timestamp to Python datetime
As we learnt before, if you have a single timestamp variable, you can easily convert it to a datetime using the following Python code:
stamp_ts = pd.Timestamp ('2024-01-02')
my_datetime = stamp_ts.to_pydatetime()
Convert a timestamp with timezone
In a similar fashion, we can als create a timestamp in a specific timezone and convert it to a Python timestamp:
import pandas as pd
stamp_ts = pd.Timestamp ('2024-01-02', tz = 'Europe/Lisbon')
my_datetime = stamp_ts.to_pydatetime()
print (my_datetime)
This will create a Python date with a specific time zone. Make sure to change your time zone according to your needs:
2024-01-02 00:00:00+00:00
Python timestamp to string with time zone
We can now cast the converted Python stamp to a string:
my_dt_str = my_datetime.strftime('%Y-%m-%d %H:%M:%S')
print(my_dt_str)
This will return the following formatted string:
2024-01-02 00:00:00