Abraham Reinhardt
Abraham Reinhardt

Reputation: 107

Pandas.read_csv(,parse_dates,) forces to make a column of float data into string

I tried to read a csv file

Test.csv

using

df = pd.read_csv('Test.csv',index_col=0,parse_dates=[0],header=None,names=['Open','Close','High','Low','Vol','Mon'])
df['Open']

But for unknown reason the result of column 'Open' is a list of string data. The other columns such as 'High' 'Low' are fine, just the 'Open' is wrong. So I tried add dtype=floatat the end.

1

Still the data of 'Open' are string. And I figured out it's the problem of parse_dates cause without it it's fine.

without parse_dates

But I still need to make date's type datetime64 so I have to do

df = pd.read_csv('Test.csv',index_col=[0],header=None,names=['Open','Close','High','Low','Vol','Mon'])
df.index = pd.to_datetime(df.index)
df['Open'] 

to make things right. But I wonder what causes it? it seems to be a bug to me. So I post it out see if anyone had seen the same problem.

Upvotes: 3

Views: 977

Answers (1)

Chrstfer CB
Chrstfer CB

Reputation: 65

Perhaps using df = pd.read_csv('Test.csv',infer_date_format=True,...) from the pandas docs. Also, in the same section under the dtype option it looks like you can pass converter functions, so perhaps also try:

df = pd.read_csv('Test.csv', dtype={'Open': pd.to_datetime},...)`

Not sure the underlying reason, without the text pasted to somewhere like bpaste.net i cant test it.

Upvotes: 1

Related Questions