I have a dataframe with two columns for time (BgnDate and EndDate). Currently the format of these columns is float64, but I need them in the pandas datetime format. I tried running pd.to_datetime with a format of %Y-%m-%d but I always get seconds included, which I don't want. In addition, Pandas is computing the column values incorrectly. Below is an example of my input, script, and output:
BgnDate = 19700907
input_data_df[['BgnDate', 'EndDate']] = input_data_df[['BgnDate', 'EndDate']].apply(lambda x: pd.to_datetime(x,format='%Y-%m-%d', errors='coerce'))
BgnDate = 1970-01-01 00:00:00.019990907
What I want my output to look like is: '1999-09-07'
^^ Notice that the month and date are incorrect for the output.
Any idea where I'm going wrong here?
CodePudding user response:
I think your format is incorrect. Try %Y%m%d instead of %Y-%m-%d:
df[['BgnDate', 'EndDate']] = df[['BgnDate', 'EndDate']].apply(lambda x: pd.to_datetime(x,format='%Y%m%d', errors='coerce'))
