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 OverflowIf 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
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)
Videos
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')
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:
datetimeseries 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 apd.Seriesvariable:formatted_dates = df['datetime'].dt.strftime('%m/%d/%Y')The
dtypeofformatted_dateswill beobject, 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
datetimeseries 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.
Use strptime and strftime of datetime.datetime methods:
from datetime import datetime
def change_format(val):
val = str(val)
date = datetime.strptime(val, "%Y%M%d")
new_date = date.strftime("%Y-%M-%d")
return new_date
Example :
change_format("20220622")
>>> "2022-06-22"
Then, you can apply this function to you dataframe series:
df['Valuation Date'].apply(change_format)
You can convert the date in Pandas dataFrame using pd.to_datetime module. For your solution kindly refer the Code Snippet mentioned below: -
# Import all the Important Modules
import pandas as pd
# Data Regeneration
df = pd.DataFrame({'Valuation Date': {0: '20160126', 1: '20160127'}})
# Date Conversion from 'YYYYMMDD' to 'MM-DD-YYYY'
df['Valuation Date'] = pd.to_datetime(df['Valuation Date']).dt.strftime('%m-%d-%Y')
# Print Result
print(df)
# Output of above Code Snippet
Valuation Date
0 01-26-2016
1 01-27-2016
To know more about pd.to_datetime : - Click Here !!!