brohjoe
brohjoe

Reputation: 934

TypeError: only integer scalar arrays can be converted to scalar index while converting dataframe "to_datetime"

I'm sure it's something simple that I'm missing but I'm getting this typeError during the 'to_datetime' conversion for the date column which is formatted as 'object.' I have tried different variations of this code but I still get the same error. I'm using the PyCharm IDE.

import pandas as pd

df = pd.read_csv('data/myData.csv')
df.columns = [['date', 'open', 'high', 'low', 'close', 'volume']]
df['date'] = pd.to_datetime(df['date'], format='%m%d%Y:%H:%M:%S.%f')

# print(df.dtypes)
print(df.head())

Upvotes: 4

Views: 1044

Answers (1)

jezrael
jezrael

Reputation: 863291

Here is problem is convert nested list to columns get one level MultiIndex.

So for prevent it use:

df.columns = ['date', 'open', 'high', 'low', 'close', 'volume']

Or skip first row in file (original header) and set new header by names parameter:

df = pd.read_csv('data/myData.csv', 
                 skiprows=1,
                 names=['date', 'open', 'high', 'low', 'close', 'volume'])

Upvotes: 4

Related Questions