0

I'm trying to read data from a csv and parsing dates but I found this issue in which only one of the columns gets a datetime format and the other one still remains an object.

dtype = {'col1':'category','col2':'category','Start Date':'str','End Date':'str'}
dates = ['col3','col4']
df = pd.read_csv(filepath,dtype=dtype,parse_dates=dates,dayfirst=False)

Both date columns have same format.

when I do df.info() I get the following: df.info() output

I tried using dayfirst input and the formatter but it didn't help.

I expect that both columns in the list would get datetime object but for some reason they aren't.

Update: tried to recreate a minimal reproducible data set by doing the code block below but this is behaving as expected, producing both Start Date and End Date columns as datetime.

import pandas as pd

df = pd.DataFrame({'col1':['ABC','ABC','DCF','DCF'],
                   'Start Date':['12-31-2022','12-31-2022','12-31-2022','12-31-2022'],
                   'End Date':['12-31-2023','12-31-2023','12-31-2023','12-31-2023']
                  })

df.to_csv('test.csv',index=0)

df2 = pd.read_csv('test.csv',
                  dtype={'col1':'category','Start Date':'str','End Date':'str'},
                  parse_dates = ['Start Date','End Date'],
                  dayfirst=False
                 )
df2.info()
  • Ciao Gian Flavio, do you mind providing a full [mcve]? – rpanai Dec 06 '22 at 12:14
  • I tried making a minimal reproducible example but it turns out that when I try to create synthetic data to mimic the error I get the expected result. Please have a look at the main post to see what I tried to do for creating the minimal reproducible example. – Gian Flavio Violi Bravo Dec 06 '22 at 13:40
  • 1
    See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for best practices related to Pandas questions. – itprorh66 Dec 06 '22 at 13:59
  • Are you sure that you shouldn't use `dates=['Start Date', 'End Date']`? – rpanai Dec 06 '22 at 14:36
  • I get an error that read_csv get unexpected argument dates. Lastime I check it was supposed to be parse_dates since I'm passing the dtype as str. – Gian Flavio Violi Bravo Dec 07 '22 at 08:23

0 Answers0