Reputation: 533
I have to convert a column of dates from the integer/date format to the date format d-m-Y. Example:
import pandas as pd
col1 = [737346, 737346, 737346, 737346, 737059, 737346]
col2 = ['cod1', 'cod2', 'cod3', 'cod4', 'cod1', 'cod2']
dict = {'V1' : col1, 'V2' : col2}
df = pd.DataFrame.from_dict(dict)
df
V1 V2
0 737346 cod1
1 737346 cod2
2 737346 cod3
3 737346 cod4
4 737059 cod1
5 737346 cod2
expected:
df
V1 V2
0 14-10-2019 cod1
1 14-10-2019 cod2
2 14-10-2019 cod3
3 14-10-2019 cod4
4 31-12-2018 cod1
5 14-10-2019 cod2
Upvotes: 0
Views: 3600
Reputation: 42916
You can use date.fromordinal
for this.
from datetime import datetime as dt
df['V1'] = df.V1.apply(lambda x: dt.fromordinal(x)).dt.strftime('%d-%m-%Y')
print(df)
V1 V2
0 14-10-2019 cod1
1 14-10-2019 cod2
2 14-10-2019 cod3
3 14-10-2019 cod4
4 31-12-2018 cod1
5 14-10-2019 cod2
Upvotes: 1
Reputation: 323326
Just pandas
Timestamp.fromordinal
df.V1.map(pd.Timestamp.fromordinal)
Out[511]:
0 2019-10-14
1 2019-10-14
2 2019-10-14
3 2019-10-14
4 2018-12-31
5 2019-10-14
Name: V1, dtype: datetime64[ns]
Upvotes: 2
Reputation: 1422
datetime fromordinal
should help.
import datetime as dt
col1 = [737346, 737346, 737346, 737346, 737059, 737346]
col2 = ['cod1', 'cod2', 'cod3', 'cod4', 'cod1', 'cod2']
dd = {'V1' : col1, 'V2' : col2}
df = pd.DataFrame.from_dict(dd)
df['V1'] = df['V1'].apply(dt.datetime.fromordinal)
Upvotes: 6