I've loaded a csv using pd.read_csv in the following format -
obj = pd.read_csv('usd_brl_date.csv', sep=';', usecols=[1,2,3,4,5,6])
In [34]: obj
Out [34]:
Date Price Open High Low Change %
0 18/Mar/2016 3.6128 3.6241 3.6731 3.6051 -0.31%
1 17/Mar/2016 3.6241 3.7410 3.7449 3.6020 -3.16%
2 16/Mar/2016 3.7422 3.7643 3.8533 3.7302 -0.62%
In [35]: usdbrl.dtypes
Out [35]
Date object
Price float64
Open float64
High float64
Low float64
Change % object
dtype: object
I need to convert the column Date type object to data type. Or if it possible to set dtype for the first column within pd.read_csv.
You can use to_datetime
:
df['Date'] = pd.to_datetime(df['Date'], format="%d/%b/%Y")
print df
Date Price Open High Low Change %
0 2016-03-18 3.6128 3.6241 3.6731 3.6051 -0.31%
1 2016-03-17 3.6241 3.7410 3.7449 3.6020 -3.16%
2 2016-03-16 3.7422 3.7643 3.8533 3.7302 -0.62%
Or add parameter parse_dates
to read_csv
, as mentioned MaxU:
obj = pd.read_csv('usd_brl_date.csv', sep=';', usecols=[1,2,3,4,5,6], parse_dates=['Date'])
print df
Date Price Open High Low Change %
0 2016-03-18 3.6128 3.6241 3.6731 3.6051 -0.31%
1 2016-03-17 3.6241 3.7410 3.7449 3.6020 -3.16%
2 2016-03-16 3.7422 3.7643 3.8533 3.7302 -0.62%