If your column is a string, you will need to first use `pd.to_datetime',

Copydf['Date'] = pd.to_datetime(df['Date'])

Then, use .dt datetime accessor with strftime:

Copydf = pd.DataFrame({'Date':pd.date_range('2017-01-01', periods = 60, freq='D')})

df.Date.dt.strftime('%Y%m%d').astype(int)

Or use lambda function:

Copydf.Date.apply(lambda x: x.strftime('%Y%m%d')).astype(int)

Output:

Copy0     20170101
1     20170102
2     20170103
3     20170104
4     20170105
5     20170106
6     20170107
7     20170108
8     20170109
9     20170110
10    20170111
11    20170112
12    20170113
13    20170114
14    20170115
15    20170116
16    20170117
17    20170118
18    20170119
19    20170120
20    20170121
21    20170122
22    20170123
23    20170124
24    20170125
25    20170126
26    20170127
27    20170128
28    20170129
29    20170130
30    20170131
31    20170201
32    20170202
33    20170203
34    20170204
35    20170205
36    20170206
37    20170207
38    20170208
39    20170209
40    20170210
41    20170211
42    20170212
43    20170213
44    20170214
45    20170215
46    20170216
47    20170217
48    20170218
49    20170219
50    20170220
51    20170221
52    20170222
53    20170223
54    20170224
55    20170225
56    20170226
57    20170227
58    20170228
59    20170301
Name: Date, dtype: int32
Answer from Scott Boston on Stack Overflow
Top answer
1 of 2
25

If your column is a string, you will need to first use `pd.to_datetime',

Copydf['Date'] = pd.to_datetime(df['Date'])

Then, use .dt datetime accessor with strftime:

Copydf = pd.DataFrame({'Date':pd.date_range('2017-01-01', periods = 60, freq='D')})

df.Date.dt.strftime('%Y%m%d').astype(int)

Or use lambda function:

Copydf.Date.apply(lambda x: x.strftime('%Y%m%d')).astype(int)

Output:

Copy0     20170101
1     20170102
2     20170103
3     20170104
4     20170105
5     20170106
6     20170107
7     20170108
8     20170109
9     20170110
10    20170111
11    20170112
12    20170113
13    20170114
14    20170115
15    20170116
16    20170117
17    20170118
18    20170119
19    20170120
20    20170121
21    20170122
22    20170123
23    20170124
24    20170125
25    20170126
26    20170127
27    20170128
28    20170129
29    20170130
30    20170131
31    20170201
32    20170202
33    20170203
34    20170204
35    20170205
36    20170206
37    20170207
38    20170208
39    20170209
40    20170210
41    20170211
42    20170212
43    20170213
44    20170214
45    20170215
46    20170216
47    20170217
48    20170218
49    20170219
50    20170220
51    20170221
52    20170222
53    20170223
54    20170224
55    20170225
56    20170226
57    20170227
58    20170228
59    20170301
Name: Date, dtype: int32
2 of 2
0

The error in the OP occurred because datetime.datetime.strftime was called without a datetime/date argument in apply(). The format= should be passed as a separate argument to apply(), which will be passed off to strftime() as the format.

Copyfrom datetime import datetime
x = dates.apply(datetime.strftime, format='%Y%m%d').astype(int)

If the date were strings (instead of datetime/date), then str.replace() should do the job.

Copyx = dates.str.replace('-', '').astype(int)

# using apply
x = dates.apply(lambda x: x.replace('-', '')).astype(int)

A mildly interesting(?) thing to note is that both .dt.strftime and str.replace of pandas are not optimized, so calling Python's strftime and str.replace via apply() is actually faster than the pandas counterparts (in the case of strftime, it is much faster).

Copydates = pd.Series(pd.date_range('2020','2200', freq='d'))

%timeit dates.dt.strftime('%Y%m%d')
# 719 ms ± 41.5 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

%timeit dates.apply(datetime.strftime, format='%Y%m%d')
# 472 ms ± 34.5 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

dates = dates.astype(str)

%timeit dates.str.replace('-', '')
# 30.9 ms ± 2.46 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

%timeit dates.apply(lambda x: x.replace('-', ''))
# 26 ms ± 183 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › convert-the-column-type-from-string-to-datetime-format-in-pandas-dataframe
Convert Column Type from String to Datetime Format in Pandas Dataframe - GeeksforGeeks
The “Date” column is converted to datetime64[ns]. When the column is already in yyyymmdd format, use pd.to_datetime() with the %Y%m%d format to convert it directly to datetime64[ns].
Published   November 1, 2025
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.to_datetime.html
pandas.to_datetime — pandas 3.0.3 documentation - PyData |
See also: pandas general documentation about timezone conversion and localization. ... The strftime to parse time, e.g. "%d/%m/%Y". See strftime documentation for more information on choices, though note that "%f" will parse all the way up to nanoseconds. You can also pass: “ISO8601”, to parse any ISO8601 time string (not necessarily in exactly the same format);
🌐
Educative
educative.io › answers › what-is-the-pandastodatetime-method
What is the pandas.to_datetime() method?
arg: an integer, float, string, list, or dict object to convert into a DateTime object. dayfirst: set it to true if the input contains the day first. yearfirst: set it to true if the input contains the year first. utc: returns the UTC DatetimeIndex if True. format: specifies the position of the day, month, and year in the date. import pandas as pd · # input in mm.dd.yyyy format ·
🌐
Statology
statology.org › home › pandas: how to convert date to yyyymmdd format
Pandas: How to Convert Date to YYYYMMDD Format
November 16, 2022 - You can use the following syntax ... pd.to_datetime(df['date_column']) #convert date to YYYYMMDD format df['date_column'] = df['date_column'].dt.strftime('%Y%m%d').astype(int)...
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › python-pandas-to_datetime
Pandas.to_datetime()-Python - GeeksforGeeks
June 24, 2025 - Explanation: pandas.to_datetime() expects dates in ISO format (YYYY-MM-DD), so we use 'format='%d/%m/%Y' to correctly parse day-first strings.
Find elsewhere
🌐
Saturn Cloud
saturncloud.io › blog › how-to-change-datetime-format-in-pandas
How to Change Datetime Format in Pandas | Saturn Cloud Blog
June 19, 2023 - In this example, we first create a DataFrame with a column called “date” that contains datetime strings in the format “YYYY-MM-DD HH:MM:SS”. We then convert this column to datetime format using the pd.to_datetime() method.
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › change-string-to-date-in-pandas-dataframe
Change String To Date In Pandas Dataframe - GeeksforGeeks
July 23, 2025 - Explanation:The code converts date strings to datetime using pd.to_datetime() and formats them as DD-MM-YYYY using .dt.strftime(), storing the result in 'Formatted_Date'. astype() method allows direct conversion of a column’s data type.
🌐
AskPython
askpython.com › home › how to change datetime format in pandas
How to Change Datetime Format in Pandas - AskPython
April 12, 2023 - The default format of the pandas datetime is set to YYYY-MM-DD, which implies that the year comes first, followed by the month and day values.
🌐
Python Guides
pythonguides.com › pandas-convert-string-to-datetime
How to Convert String to Datetime in Pandas
March 24, 2026 - While we usually use MM/DD/YYYY in the United States, you might receive data from international offices in DD/MM/YYYY format. If you don’t want to write out the full format string, you can use the dayfirst toggle. # International format: Day/Month/Year dates = ['31/01/2023', '15/02/2023'] df = pd.DataFrame({'Dates': dates}) # Telling Pandas that the day comes first df['Dates'] = pd.to_datetime(df['Dates'], dayfirst=True) print(df)
🌐
DataCamp
datacamp.com › tutorial › converting-strings-datetime-objects
Convert String to DateTime in Python: Complete Guide with Examples | DataCamp
February 4, 2026 - from datetime import datetime # Example with the standard date and time format date_str = '2023-02-28 14:30:00' date_format = '%Y-%m-%d %H:%M:%S' date_obj = datetime.strptime(date_str, date_format) print(date_obj) # Example with a different format date_str = '02/28/2023 02:30 PM' date_format = '%m/%d/%Y %I:%M %p' date_obj = datetime.strptime(date_str, date_format) print(date_obj) In the first example, we have a string representing a date and time in the format ‘YYYY-MM-DD HH:MM:SS’, and for the second example in a different format, ‘MM/DD/YYYY HH:MM AM/PM’.
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas change string to date in dataframe
Pandas Change String to Date in DataFrame - Spark By {Examples}
June 26, 2025 - To convert a pandas DataFrame column from string to date type (datetime64) format, you can use the pandas.to_datetime() function or the DataFrame.astype()
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-to-datetime-strptime
How To Convert a String to a datetime Object in Python | DigitalOcean
3 weeks ago - from datetime import datetime date_object = datetime.strptime("12 25 2024", "%m %d %Y") print(date_object.strftime("%Y-%m-%d")) # 2024-12-25 print(date_object.strftime("%d, %m, %Y")) # 25, 12, 2024 · Use this for yyyy-mm-dd filenames, SQL literals, or JSON fields.
Top answer
1 of 3
16

You can use the parse_dates and dayfirst arguments of pd.read_csv, see: the docs for read_csv()

df = pd.read_csv('myfile.csv', parse_dates=['Date'], dayfirst=True)

This will read the Date column as datetime values, correctly taking the first part of the date input as the day. Note that in general you will want your dates to be stored as datetime objects.

Then, if you need to output the dates as a string you can call dt.strftime():

df['Date'].dt.strftime('%d/%m/%Y')
2 of 3
1

When I use again this: df['Date'] = pd.to_datetime(df['Date']), it gets back to the previous format.

No, you cannot simultaneously have the string format of your choice and keep your series of type datetime. As remarked here:

datetime series are stored internally as integers. Any human-readable date representation is just that, a representation, not the underlying integer. To access your custom formatting, you can use methods available in Pandas. You can even store such a text representation in a pd.Series variable:

formatted_dates = df['datetime'].dt.strftime('%m/%d/%Y')

The dtype of formatted_dates will be object, which indicates that the elements of your series point to arbitrary Python times. In this case, those arbitrary types happen to be all strings.

Lastly, I strongly recommend you do not convert a datetime series to strings until the very last step in your workflow. This is because as soon as you do so, you will no longer be able to use efficient, vectorised operations on such a series.

🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.to_datetime.html
pandas.to_datetime — pandas 3.0.3 documentation
See also: pandas general documentation about timezone conversion and localization. ... The strftime to parse time, e.g. "%d/%m/%Y". See strftime documentation for more information on choices, though note that "%f" will parse all the way up to nanoseconds. You can also pass: “ISO8601”, to parse any ISO8601 time string (not necessarily in exactly the same format);
🌐
GeeksforGeeks
geeksforgeeks.org › python › converting-string-yyyy-mm-dd-into-datetime-in-python
Converting string into DateTime in Python - GeeksforGeeks
July 23, 2025 - import pandas as pd s = ['2021-05-25', '2020/05/25', '2019/02/15'] res = pd.to_datetime(s, format='mixed') print(res)
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-change-the-pandas-datetime-format-in-python
How to change the Pandas datetime format in Python? - GeeksforGeeks
July 23, 2025 - # importing pandas as pd import pandas as pd # change in date time format date_sr = pd.to_datetime(pd.Series("2020-12-08")) change_format = date_sr.dt.strftime('%d/%m/%Y') # Print the formatted date print(change_format)