0

I am using the below code but 'Start Date' and 'End Date' remain as strings even after being I attempt to convert their datatypes when importing the data. How can I convert them to dates in my dataframe?

import pandas as pd
import numpy as np
import datetime as dt 

df = pd.read_excel('sfdc_churn_report.xltx', 
                   index_col = None, 
                   dtype = {'Start Date': dt.date, 
                            'End Date': dt.date}
                  )

2 Answers 2

1

Instead of dtype parameter, pass parse_dates=['Start Date', 'End Date'].

Assuming that your both date columns are formatted the standard way, it should be enough.

If you have some "weird" date formatting, you can insted specify a converter function, using converters parameter.

Passing index_col is not needed, as its default value is just None.

Sign up to request clarification or add additional context in comments.

Comments

1
df = pd.read_excel('sfdc_churn_report.xltx', index_col="Date",parse_dates=True)

parse_dates=True will try its best to convert the string date to an actual date object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.