How can I convert DateTime to String in Pandas?

How can I convert DateTime to String in Pandas?

Converting DateTime to String in Pandas is a process that allows users to change the format of a date and time data into a string format. This can be achieved by using the “to_string” function in Pandas, which converts the DateTime object into a string object. This conversion is useful for data analysis and visualization, as it provides a more readable and customizable representation of the date and time data. Additionally, converting DateTime to String allows for easier manipulation and sorting of the data. Overall, this feature in Pandas provides users with more flexibility and control over their data analysis tasks.

Convert DateTime to String in Pandas (With Examples)


You can use the following basic syntax to convert a column from DateTime to string in pandas:

df['column_name'].dt.strftime('%Y-%m-%d')

The following example shows how to use this syntax in practice.

Example: Convert DateTime to String in Pandas

Suppose we have the following pandas DataFrame that shows the sales made by some store on four different days:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'day': pd.to_datetime(pd.Series(['20210101', '20210105',
                                                    '20210106', '20210109'])),
                   'sales': [1440, 1845, 2484, 2290]})

#view DataFrame
df

	       day     sales
0	2021-01-01	1440
1	2021-01-05	1845
2	2021-01-06	2484
3	2021-01-09	2290

We can use the dtypes function to view the data type of each column in the DataFrame:

#view data type of each column
df.dtypes

day      datetime64[ns]
sales             int64
dtype: object

We can see that the “day” column has a DateTime class.

To convert “day” into a string, we can use the following syntax:

#convert 'day' column to string
df['day'] = df['day'].dt.strftime('%Y-%m-%d')

#view updated DataFrame
df

	daysales
0	2021-01-01	1440
1	2021-01-05	1845
2	2021-01-06	2484
3	2021-01-09	2290

We can use the dtypes function again to verify that the “day” column is now a string:

#view data type of each column
df.dtypes

day      object
sales     int64
dtype: object

Note: You can find the complete documentation for the dt.strftime() function .

Additional Resources

The following tutorials explain how to perform other common conversions in Python:

Cite this article

stats writer (2024). How can I convert DateTime to String in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-datetime-to-string-in-pandas/

stats writer. "How can I convert DateTime to String in Pandas?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-datetime-to-string-in-pandas/.

stats writer. "How can I convert DateTime to String in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-datetime-to-string-in-pandas/.

stats writer (2024) 'How can I convert DateTime to String in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-datetime-to-string-in-pandas/.

[1] stats writer, "How can I convert DateTime to String in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can I convert DateTime to String in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top