Student
Student

Reputation: 1197

What Is the Most Efficient Way to Infer Dates When Reading a CSV Into Pandas?

Objective: to read in a csv file into a Pandas dataframe and not have to manually call at date columns

Here is the code I am using currently,which provides a dtype of 'O' on my date column:

df = pd.read_csv(r"D:\time_stamp_ex.csv",
                parse_dates=False, 
                infer_datetime_format=True,
                date_parser=pd.to_datetime)
df

This isn't super helpful since you don't have the file, so here is a dataframe snippet that produces exactly what I have in my csv file:

d = {'time_stamp': ['9/22/2017','9/15/2018','8/7/2017'],
     'category': ['A1','A2','B1'],
     'number':[12345678,12345678,12345678]}
df = pd.DataFrame(data=d)
df

Reading the Pandas documentation I don't see that I have missed anything, but I am open to this being a simple answer.

Upvotes: 2

Views: 249

Answers (1)

T Burgis
T Burgis

Reputation: 1435

Pandas will only attempt to infer_datetime_format for what you specify in parse_dates. You currently have this set to False. You need to tell it which columns in your csv file contain dates.

I'm not sure if this answers your question as I'm unsure what 'not have to manually call at date columns' means. Do you mean you don't want to have to convert the date columns separately from reading in the file or that you don't want to tell read_csv which columns contain dates?

Upvotes: 1

Related Questions