Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Parsing date in pandas.read_csv

I am trying to read a CSV file which has in its first column date values specified in this format:

"Dec 30, 2021","1.1","1.2","1.3","1"

While I can define the types for the remaining columns using dtype= clause, I do not know how to handle the Date.

I have tried the obvious np.datetime64 without success.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there any way to specify a format to parse this date directly using read_csv method?

>Solution :

Just specify a list of columns that should be convert to dates in the parse_dates= of pd.read_csv:

>>> df = pd.read_csv('file.csv', parse_dates=['date'])
>>> df
        date    a    b    c  d
0 2021-12-30  1.1  1.2  1.3  1

>>> df.dtypes
date    datetime64[ns]
a              float64
b              float64
c              float64
d                int64
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading