I am trying to parse dates from an Excel file following a given format %d/%m/%y
. I am not able to set the format, I am always getting %m/%d/%Y
.
The source format is %d/%m/%y
so that the first date should be first of June 2016. Any ideas on how to do it?
import pandas as pd
url = 'https://www.dropbox.com/s/8gqmq3jx27unsta/example_dates.xlsx?dl=1'
file = pd.ExcelFile(url, parse_date=True,
date_parser = (lambda x: pd.to_datetime(x, format ='%d/%m/%y')))
df = file.parse(0)
df
date variable
0 2016-01-06 1
1 2016-06-07 2
2 2016-12-10 3
3 2016-12-29 4
Something like this might work but it doesn't:
df = file.parse(0, converters={'date' : lambda x: pd.to_datetime(x, dayfirst=True)}
date variable
0 2016-01-06 1
1 2016-06-07 2
2 2016-12-10 3
3 2016-12-29 4
Actually pandas have a format to display datetime object.
So it will display in that format till you change that
meanwhile you can do this:
df['date1'] = df['date'].dt.strftime('%d/%m/%y')
output:
date variable date1
0 2016-01-06 1 06/01/16
1 2016-06-07 2 07/06/16
2 2016-12-10 3 10/12/16
3 2016-12-29 4 29/12/16
You can use converters
:
>>> file.parse(0, parse_dates=True, index_col=0, date_parser=lambda x: pd.to_datetime(x).strftime("%d/%m/%Y"))
variable
date
2016-06-01 1
2016-07-06 2
2016-10-12 3
2016-12-29 4
You can use pandas.read_excel
in combination with datetime.datetime.strptime
.
import pandas as pd
from datetime import datetime
URL = 'https://www.dropbox.com/s/8gqmq3jx27unsta/example_dates.xlsx?dl=1'
file = (
pd
.read_excel(
io=URL,
parse_date=True,
date_parser=lambda x: datetime.strptime(x, '%d/%m/%Y'),
)
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.