I'm using Pandas to read a .xlsx file as follows:
client_df = pd.read_excel(
client_file,
header = 1,
parse_dates = ['Event Date'],
date_parser = lambda x: datetime.strftime(x, '%Y-%m-%d')
)
This works fine, but I'm getting this warning from Pandas:
.venv/lib/python3.8/site-packages/pandas/io/parsers.py:3339: FutureWarning:
Use pd.to_datetime instead.
return generic_parser(date_parser, *date_cols)
I read somewhere that Pandas is deprecating datetime.
- Can anyone help me convert the date_parser expression to use pd.to_datetime?
EDIT: apologies for the lack of clarity. The client_file datetime objects is as follows:
YYYY-MM-DD HH:MM:SS
I need to extract the following, as a str object:
YYYY-MM-DD
EDIT: @jezrael's answer (below) working perfectly - thank you jezrael!
client_df = pd.read_excel(
client_file,
header = 1,
parse_dates = ['Event Date'],
converters={'Event Date': lambda x: pd.to_datetime(x).strftime('%Y-%m-%d')}
)
EDIT 2: @MrFuppes pointed out that I didn't need to parse dates at all. His solution is probably better in this instance, as follows:
client_df = pd.read_excel(
client_file,
header = 1,
converters = {'Event Date': lambda x: x.strftime('%Y-%m-%d')}
)